import { createLogger } from "../../lib/logger/index.js";
const logger = createLogger("events").child({ component: "factory" });
const GLOBAL_EVENT_BUS_KEY = "__openMercatoGlobalEventBus__";
let globalEventBus = null;
function setGlobalEventBus(bus) {
  globalEventBus = bus;
  try {
    ;
    globalThis[GLOBAL_EVENT_BUS_KEY] = bus;
  } catch {
  }
}
function getGlobalEventBus() {
  try {
    const sharedBus = globalThis[GLOBAL_EVENT_BUS_KEY];
    if (sharedBus && typeof sharedBus === "object" && typeof sharedBus.emit === "function") {
      return sharedBus;
    }
  } catch {
  }
  return globalEventBus;
}
const allDeclaredEventIds = /* @__PURE__ */ new Set();
const allDeclaredEvents = [];
function addDeclaredEvent(event) {
  allDeclaredEventIds.add(event.id);
  if (!allDeclaredEvents.find((e) => e.id === event.id)) {
    allDeclaredEvents.push(event);
  }
}
function isEventDeclared(eventId) {
  return allDeclaredEventIds.has(eventId);
}
function getAllDeclaredEventIds() {
  return Array.from(allDeclaredEventIds);
}
function getDeclaredEvents() {
  return [...allDeclaredEvents];
}
function isBroadcastEvent(eventId) {
  const event = allDeclaredEvents.find((e) => e.id === eventId);
  return event?.clientBroadcast === true;
}
function isPortalBroadcastEvent(eventId) {
  const event = allDeclaredEvents.find((e) => e.id === eventId);
  return event?.portalBroadcast === true;
}
let _registeredEventConfigs = null;
function registerEventModuleConfigs(configs) {
  if (_registeredEventConfigs !== null && process.env.NODE_ENV === "development") {
    logger.debug("Event module configs re-registered (this may occur during HMR)");
  }
  _registeredEventConfigs = configs;
  for (const config of configs) {
    for (const event of config.events) {
      addDeclaredEvent(event);
    }
  }
}
function getEventModuleConfigs() {
  return _registeredEventConfigs ?? [];
}
function createModuleEvents(options) {
  const { moduleId, events, strict = false } = options;
  const validEventIds = new Set(events.map((e) => e.id));
  const fullEvents = events.map((e) => ({
    ...e,
    module: moduleId
  }));
  for (const event of fullEvents) {
    addDeclaredEvent(event);
  }
  const emit = async (eventId, payload, emitOptions) => {
    if (!validEventIds.has(eventId)) {
      const message = `[events] Module "${moduleId}" tried to emit undeclared event "${eventId}". Add it to the module's events.ts file first.`;
      if (strict) {
        throw new Error(message);
      } else {
        logger.error("Module tried to emit undeclared event \u2014 add it to the module events.ts first", { moduleId, eventId });
      }
    }
    const eventBus = getGlobalEventBus();
    if (!eventBus) {
      logger.warn("Event bus not available, cannot emit event", { eventId });
      return;
    }
    await eventBus.emit(eventId, payload, emitOptions);
  };
  return {
    moduleId,
    events: fullEvents,
    emit
  };
}
export {
  createModuleEvents,
  getAllDeclaredEventIds,
  getDeclaredEvents,
  getEventModuleConfigs,
  getGlobalEventBus,
  isBroadcastEvent,
  isEventDeclared,
  isPortalBroadcastEvent,
  registerEventModuleConfigs,
  setGlobalEventBus
};
//# sourceMappingURL=factory.js.map
