import { cloneElement, Children, isValidElement, Fragment } from 'react';
import { createExtension, createExtensionInput, coreExtensionData, createFrontendModule } from '@backstage/frontend-plugin-api';
import { getComponentData } from '@backstage/core-plugin-api';
import { collectLegacyRoutes } from './collectLegacyRoutes.esm.js';
import { compatWrapper } from './compatWrapper/compatWrapper.esm.js';

function selectChildren(rootNode, selector, strictError) {
  return Children.toArray(rootNode).flatMap((node) => {
    if (!isValidElement(node)) {
      return [];
    }
    if (node.type === Fragment) {
      return selectChildren(node.props.children, selector);
    }
    if (selector === void 0 || selector(node)) {
      return [node];
    }
    return selectChildren(node.props.children, selector);
  });
}
function convertLegacyApp(rootElement, options = {}) {
  if (getComponentData(rootElement, "core.type") === "FlatRoutes") {
    return collectLegacyRoutes(rootElement, options?.entityPage);
  }
  const appRouterEls = selectChildren(
    rootElement,
    (el) => getComponentData(el, "core.type") === "AppRouter"
  );
  if (appRouterEls.length !== 1) {
    throw new Error(
      "Failed to convert legacy app, AppRouter element could not been found. Make sure it's at the top level of the App element tree"
    );
  }
  const rootEls = selectChildren(
    appRouterEls[0].props.children,
    (el) => Boolean(el.props.children) && selectChildren(
      el.props.children,
      (innerEl) => getComponentData(innerEl, "core.type") === "FlatRoutes"
    ).length === 1
  );
  if (rootEls.length !== 1) {
    throw new Error(
      "Failed to convert legacy app, Root element containing FlatRoutes could not been found. Make sure it's within the AppRouter element of the App element tree"
    );
  }
  const [rootEl] = rootEls;
  const routesEls = selectChildren(
    rootEls[0].props.children,
    (el) => getComponentData(el, "core.type") === "FlatRoutes"
  );
  if (routesEls.length !== 1) {
    throw new Error(
      "Unexpectedly failed to find FlatRoutes in app element tree"
    );
  }
  const [routesEl] = routesEls;
  const CoreLayoutOverride = createExtension({
    name: "layout",
    attachTo: { id: "app/root", input: "children" },
    inputs: {
      content: createExtensionInput([coreExtensionData.reactElement], {
        singleton: true
      })
    },
    output: [coreExtensionData.reactElement],
    factory({ inputs }) {
      return [
        coreExtensionData.reactElement(
          compatWrapper(
            cloneElement(
              rootEl,
              void 0,
              inputs.content.get(coreExtensionData.reactElement)
            )
          )
        )
      ];
    }
  });
  const CoreNavOverride = createExtension({
    name: "nav",
    attachTo: { id: "app/layout", input: "nav" },
    output: [],
    factory: () => [],
    disabled: true
  });
  const collectedRoutes = collectLegacyRoutes(routesEl, options?.entityPage);
  return [
    ...collectedRoutes,
    createFrontendModule({
      pluginId: "app",
      extensions: [CoreLayoutOverride, CoreNavOverride]
    })
  ];
}

export { convertLegacyApp };
//# sourceMappingURL=convertLegacyApp.esm.js.map
