import { applyNotificationHandlerOverridesToEntries } from "@open-mercato/shared/modules/overrides";
const GLOBAL_NOTIFICATION_HANDLERS_KEY = "__openMercatoNotificationHandlers__";
let _handlerEntries = null;
function readGlobalEntries() {
  try {
    const value = globalThis[GLOBAL_NOTIFICATION_HANDLERS_KEY];
    return Array.isArray(value) ? value : null;
  } catch {
    return null;
  }
}
function writeGlobalEntries(entries) {
  try {
    ;
    globalThis[GLOBAL_NOTIFICATION_HANDLERS_KEY] = entries;
  } catch {
  }
}
function registerNotificationHandlers(entries) {
  const finalEntries = applyNotificationHandlerOverridesToEntries(entries);
  const flat = [];
  for (const entry of finalEntries) {
    for (const handler of entry.handlers ?? []) {
      flat.push({ moduleId: entry.moduleId, handler });
    }
  }
  flat.sort((a, b) => (b.handler.priority ?? 50) - (a.handler.priority ?? 50));
  _handlerEntries = flat;
  writeGlobalEntries(flat);
}
function getNotificationHandlerEntries() {
  const globalEntries = readGlobalEntries();
  if (globalEntries) return globalEntries;
  if (!_handlerEntries) return [];
  return _handlerEntries;
}
export {
  getNotificationHandlerEntries,
  registerNotificationHandlers
};
//# sourceMappingURL=handler-registry.js.map
