import { jsx } from 'react/jsx-runtime';
import { lazy, Suspense } from 'react';
import { coreExtensionData } from '@backstage/frontend-plugin-api';
import { defaultConfigLoaderSync } from './core-app-api/src/app/defaultConfigLoader.esm.js';
import { overrideBaseUrlConfigs } from './core-app-api/src/app/overrideBaseUrlConfigs.esm.js';
import { ConfigReader } from '@backstage/config';
import { createSpecializedApp } from '@backstage/frontend-app-api';
import appPlugin from '@backstage/plugin-app';
import { discoverAvailableFeatures } from './discovery.esm.js';
import { resolveAsyncFeatures } from './resolution.esm.js';

function createApp(options) {
  let suspenseFallback = options?.loadingComponent;
  if (suspenseFallback === void 0) {
    suspenseFallback = "Loading...";
  }
  async function appLoader() {
    const config = await options?.configLoader?.().then((c) => c.config) ?? ConfigReader.fromConfigs(
      overrideBaseUrlConfigs(defaultConfigLoaderSync())
    );
    const { features: discoveredFeaturesAndLoaders } = discoverAvailableFeatures(config);
    const { features: loadedFeatures } = await resolveAsyncFeatures({
      config,
      features: [...discoveredFeaturesAndLoaders, ...options?.features ?? []]
    });
    const app = createSpecializedApp({
      config,
      features: [appPlugin, ...loadedFeatures],
      bindRoutes: options?.bindRoutes,
      extensionFactoryMiddleware: options?.extensionFactoryMiddleware
    });
    const rootEl = app.tree.root.instance.getData(
      coreExtensionData.reactElement
    );
    return { default: () => rootEl };
  }
  return {
    createRoot() {
      const LazyApp = lazy(appLoader);
      return /* @__PURE__ */ jsx(Suspense, { fallback: suspenseFallback, children: /* @__PURE__ */ jsx(LazyApp, {}) });
    }
  };
}

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