import { getModules } from "../lib/modules/registry.js";
let cachedRegistry = null;
let cachedModulesRef = null;
function buildRegistry(modules) {
  const enabledModuleIds = modules.map((mod) => mod.id);
  const enabledModuleSet = new Set(enabledModuleIds);
  const featureToModule = /* @__PURE__ */ new Map();
  const prefixToModule = /* @__PURE__ */ new Map();
  const concreteFeatureIds = [];
  const concreteFeatureSet = /* @__PURE__ */ new Set();
  const addConcreteFeature = (featureId, owningModule, authoritative) => {
    if (!featureId || featureId === "*" || featureId.endsWith(".*")) return;
    if (!concreteFeatureSet.has(featureId)) {
      concreteFeatureSet.add(featureId);
      concreteFeatureIds.push(featureId);
    }
    if (authoritative || !featureToModule.has(featureId)) {
      featureToModule.set(featureId, owningModule);
    }
    const dot = featureId.indexOf(".");
    if (dot > 0) {
      const prefix = featureId.slice(0, dot);
      if (!prefixToModule.has(prefix)) prefixToModule.set(prefix, owningModule);
    }
  };
  for (const mod of modules) {
    const features = mod.features;
    if (Array.isArray(features)) {
      for (const feature of features) {
        if (!feature || typeof feature.id !== "string" || !feature.id) continue;
        const declared = typeof feature.module === "string" && feature.module.length > 0 ? feature.module : mod.id;
        addConcreteFeature(feature.id, declared, true);
      }
    }
    const customerDefaults = mod.setup?.defaultCustomerRoleFeatures;
    if (customerDefaults) {
      for (const roleFeatures of Object.values(customerDefaults)) {
        if (!Array.isArray(roleFeatures)) continue;
        for (const featureId of roleFeatures) {
          if (typeof featureId === "string") addConcreteFeature(featureId, mod.id, false);
        }
      }
    }
    if (Array.isArray(mod.frontendRoutes)) {
      for (const route of mod.frontendRoutes) {
        if (!Array.isArray(route.requireCustomerFeatures)) continue;
        for (const featureId of route.requireCustomerFeatures) {
          if (typeof featureId === "string") addConcreteFeature(featureId, mod.id, false);
        }
      }
    }
  }
  return {
    enabledModuleIds,
    enabledModuleSet,
    featureToModule,
    prefixToModule,
    concreteFeatureIds
  };
}
function getRegistry() {
  try {
    const modules = getModules();
    if (cachedRegistry && cachedModulesRef === modules) return cachedRegistry;
    cachedModulesRef = modules;
    cachedRegistry = buildRegistry(modules);
    return cachedRegistry;
  } catch {
    return null;
  }
}
function getOwningModuleId(featureId) {
  const registry = getRegistry();
  if (registry) {
    const direct = registry.featureToModule.get(featureId);
    if (direct) return direct;
    if (featureId.endsWith(".*")) {
      const prefix = featureId.slice(0, -2);
      const fromPrefix = registry.prefixToModule.get(prefix);
      if (fromPrefix) return fromPrefix;
      return prefix;
    }
  }
  const dot = featureId.indexOf(".");
  return dot === -1 ? featureId : featureId.slice(0, dot);
}
function getEnabledModuleIds() {
  const registry = getRegistry();
  return registry ? [...registry.enabledModuleIds] : [];
}
function getConcreteFeatureIds() {
  const registry = getRegistry();
  return registry ? [...registry.concreteFeatureIds] : [];
}
function hasEnabledModulesRegistry() {
  return getRegistry() !== null;
}
function filterGrantsByEnabledModules(granted) {
  const registry = getRegistry();
  if (!registry) return [...granted];
  const { enabledModuleIds, enabledModuleSet, prefixToModule } = registry;
  const result = [];
  for (const grant of granted) {
    if (grant === "*") {
      for (const id of enabledModuleIds) result.push(`${id}.*`);
      for (const [prefix, owningModule] of prefixToModule) {
        if (!enabledModuleSet.has(prefix) && enabledModuleSet.has(owningModule)) {
          result.push(`${prefix}.*`);
        }
      }
      continue;
    }
    if (enabledModuleSet.has(getOwningModuleId(grant))) result.push(grant);
  }
  return result;
}
export {
  filterGrantsByEnabledModules,
  getConcreteFeatureIds,
  getEnabledModuleIds,
  getOwningModuleId,
  hasEnabledModulesRegistry
};
//# sourceMappingURL=enabledModulesRegistry.js.map
