"use client";
import {
  RESERVED_AI_UI_PART_IDS,
  isReservedAiUiPartId
} from "./ui-part-slots.js";
import { PendingPhase3Placeholder } from "./ui-parts/pending-phase3-placeholder.js";
import { AI_MUTATION_APPROVAL_CARDS } from "./parts/approval-cards-map.js";
function seedReservedSlots(store, options = { seedLive: false }) {
  for (const reservedId of RESERVED_AI_UI_PART_IDS) {
    if (store.has(reservedId)) continue;
    if (options.seedLive && AI_MUTATION_APPROVAL_CARDS[reservedId]) {
      store.set(
        reservedId,
        AI_MUTATION_APPROVAL_CARDS[reservedId]
      );
      continue;
    }
    store.set(
      reservedId,
      PendingPhase3Placeholder
    );
  }
}
function createAiUiPartRegistry(options) {
  const seedReserved = options?.seedReservedPlaceholders !== false;
  const seedLive = options?.seedLiveApprovalCards === true;
  const store = /* @__PURE__ */ new Map();
  if (seedReserved) {
    seedReservedSlots(store, { seedLive });
  }
  const registry = {
    register(componentId, component) {
      if (!componentId) {
        throw new Error("registerAiUiPart requires a non-empty componentId");
      }
      store.set(componentId, component);
    },
    unregister(componentId) {
      store.delete(componentId);
    },
    resolve(componentId) {
      const found = store.get(componentId);
      if (!found) return null;
      return found;
    },
    has(componentId) {
      return store.has(componentId);
    },
    list() {
      const entries = [];
      for (const componentId of store.keys()) {
        entries.push({
          componentId,
          reserved: isReservedAiUiPartId(componentId)
        });
      }
      return entries;
    },
    clear() {
      store.clear();
      if (seedReserved) {
        seedReservedSlots(store, { seedLive });
      }
    }
  };
  return registry;
}
const REGISTRY_GLOBAL_KEY = "__openMercatoAiUiPartRegistry";
function getDefaultRegistry() {
  const scope = globalThis;
  if (!scope[REGISTRY_GLOBAL_KEY]) {
    scope[REGISTRY_GLOBAL_KEY] = createAiUiPartRegistry({ seedLiveApprovalCards: true });
  }
  return scope[REGISTRY_GLOBAL_KEY];
}
const defaultAiUiPartRegistry = new Proxy(
  {},
  {
    get(_target, prop) {
      const impl = getDefaultRegistry();
      const value = impl[prop];
      return typeof value === "function" ? value.bind(impl) : value;
    }
  }
);
function registerAiUiPart(componentId, component) {
  defaultAiUiPartRegistry.register(componentId, component);
}
function resolveAiUiPart(componentId) {
  return defaultAiUiPartRegistry.resolve(componentId);
}
function unregisterAiUiPart(componentId) {
  defaultAiUiPartRegistry.unregister(componentId);
}
function resetAiUiPartRegistryForTests() {
  defaultAiUiPartRegistry.clear();
}
function listAiUiParts() {
  return defaultAiUiPartRegistry.list();
}
export {
  RESERVED_AI_UI_PART_IDS,
  createAiUiPartRegistry,
  defaultAiUiPartRegistry,
  isReservedAiUiPartId,
  listAiUiParts,
  registerAiUiPart,
  resetAiUiPartRegistryForTests,
  resolveAiUiPart,
  unregisterAiUiPart
};
//# sourceMappingURL=ui-part-registry.js.map
