import { registerOrmEntities } from "../db/mikro.js";
import { registerAppDiRegistrar, registerDiRegistrars } from "../di/container.js";
import { registerModules } from "../modules/registry.js";
import { registerEntityIds } from "../encryption/entityIds.js";
import { registerEntityFields } from "../encryption/entityFields.js";
import { registerSearchModuleConfigs } from "../../modules/search.js";
import { registerAnalyticsModuleConfigs } from "../../modules/analytics.js";
import { registerCodeWorkflowEntries } from "../../modules/workflows/code-registry.js";
import { registerResponseEnrichers } from "../crud/enricher-registry.js";
import { registerApiInterceptors } from "../crud/interceptor-registry.js";
import { registerComponentOverrides } from "../../modules/widgets/component-registry.js";
import { registerMutationGuards } from "../crud/mutation-guard-store.js";
import { registerCommandInterceptors } from "../commands/command-interceptor-store.js";
import { registerCommandLoaders } from "../commands/registry.js";
import { registerNotificationHandlers } from "../notifications/handler-registry.js";
import { clearRegisteredIntegrations, registerBundles, registerIntegrations } from "../../modules/integrations/types.js";
import { applyComponentOverridesToEntries } from "../../modules/overrides.js";
const _bootstrappedKeys = /* @__PURE__ */ new Set();
let _asyncRegistrationPromise = null;
function createBootstrap(data, options = {}) {
  return function bootstrap() {
    const registrationKey = options.registrationKey ?? "default";
    if (options.appDiRegistrar) {
      registerAppDiRegistrar(options.appDiRegistrar);
    }
    if (_bootstrappedKeys.has(registrationKey) && process.env.NODE_ENV !== "development") return;
    _bootstrappedKeys.add(registrationKey);
    registerOrmEntities(data.entities);
    registerDiRegistrars(data.diRegistrars.filter((r) => r != null));
    registerModules(data.modules);
    clearRegisteredIntegrations();
    for (const module of data.modules) {
      if (module.integrations?.length) {
        registerIntegrations(module.integrations);
      }
      if (module.bundles?.length) {
        registerBundles(module.bundles);
      }
    }
    registerEntityIds(data.entityIds);
    if (data.entityFieldsRegistry) {
      registerEntityFields(data.entityFieldsRegistry);
    }
    if (data.searchModuleConfigs) {
      registerSearchModuleConfigs(data.searchModuleConfigs);
    }
    if (data.analyticsModuleConfigs) {
      registerAnalyticsModuleConfigs(data.analyticsModuleConfigs);
    }
    if (data.codeWorkflows?.length) {
      registerCodeWorkflowEntries(data.codeWorkflows);
    }
    if (data.enricherEntries) {
      registerResponseEnrichers(data.enricherEntries);
    }
    if (data.interceptorEntries) {
      registerApiInterceptors(data.interceptorEntries);
    }
    if (data.componentOverrideEntries) {
      const finalEntries = applyComponentOverridesToEntries(data.componentOverrideEntries);
      const allOverrides = finalEntries.flatMap((entry) => entry.componentOverrides ?? []);
      registerComponentOverrides(allOverrides);
    }
    if (data.guardEntries) {
      registerMutationGuards(data.guardEntries);
    }
    if (data.commandInterceptorEntries) {
      registerCommandInterceptors(data.commandInterceptorEntries);
    }
    if (data.commandLoaderEntries) {
      registerCommandLoaders(data.commandLoaderEntries);
    }
    if (data.notificationHandlerEntries) {
      registerNotificationHandlers(data.notificationHandlerEntries);
    }
    _asyncRegistrationPromise = registerWidgetsAndOptionalPackages(data, options);
    void _asyncRegistrationPromise;
    options.onRegistrationComplete?.();
  };
}
async function waitForAsyncRegistration() {
  if (_asyncRegistrationPromise) {
    await _asyncRegistrationPromise;
  }
}
async function registerWidgetsAndOptionalPackages(data, options) {
  try {
    const coreInjection = await import("@open-mercato/core/modules/widgets/lib/injection");
    if (!options.skipCoreInjectionWidgets) {
      coreInjection.registerCoreInjectionWidgets(data.injectionWidgetEntries);
    }
    coreInjection.registerCoreInjectionTables(data.injectionTables);
    coreInjection.registerEnabledModuleIds(
      data.modules.map((module) => module.id).filter((id) => typeof id === "string" && id.length > 0)
    );
    if (!options.skipUiRegistries) {
      const [dashboardRegistry, injectionRegistry] = await Promise.all([
        import("@open-mercato/ui/backend/dashboard/widgetRegistry"),
        import("@open-mercato/ui/backend/injection/widgetRegistry")
      ]);
      dashboardRegistry.registerDashboardWidgets(data.dashboardWidgetEntries);
      injectionRegistry.registerInjectionWidgets(data.injectionWidgetEntries);
    }
  } catch {
  }
}
function isBootstrapped() {
  return _bootstrappedKeys.size > 0;
}
function resetBootstrapState() {
  _bootstrappedKeys.clear();
}
export {
  createBootstrap,
  isBootstrapped,
  resetBootstrapState,
  waitForAsyncRegistration
};
//# sourceMappingURL=factory.js.map
