"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var context_exports = {};
__export(context_exports, {
  emitThemeChange: () => emitThemeChange,
  getAsyncHooks: () => getAsyncHooks,
  getTheme: () => getTheme,
  onThemeChange: () => onThemeChange,
  setTheme: () => setTheme,
  withTheme: () => withTheme,
  withThemeSync: () => withThemeSync
});
module.exports = __toCommonJS(context_exports);
var import_themes = require("./themes");
var import_map_set = require("../primordials/map-set");
let _async_hooks;
function emitThemeChange(theme) {
  for (const listener of listeners) {
    listener(theme);
  }
}
// @__NO_SIDE_EFFECTS__
function getAsyncHooks() {
  if (_async_hooks === void 0) {
    _async_hooks = require("node:async_hooks");
  }
  return _async_hooks;
}
const { AsyncLocalStorage } = /* @__PURE__ */ getAsyncHooks();
const themeStorage = new AsyncLocalStorage();
let fallbackTheme = import_themes.SOCKET_THEME;
const listeners = new import_map_set.SetCtor();
function getTheme() {
  return themeStorage.getStore() ?? fallbackTheme;
}
function onThemeChange(listener) {
  listeners.add(listener);
  return () => {
    listeners.delete(listener);
  };
}
function setTheme(theme) {
  fallbackTheme = typeof theme === "string" ? import_themes.THEMES[theme] ?? fallbackTheme : theme;
  emitThemeChange(fallbackTheme);
}
async function withTheme(theme, fn) {
  const resolvedTheme = typeof theme === "string" ? import_themes.THEMES[theme] ?? fallbackTheme : theme;
  return await themeStorage.run(resolvedTheme, async () => {
    emitThemeChange(resolvedTheme);
    return await fn();
  });
}
function withThemeSync(theme, fn) {
  const resolvedTheme = typeof theme === "string" ? import_themes.THEMES[theme] ?? fallbackTheme : theme;
  return themeStorage.run(resolvedTheme, () => {
    emitThemeChange(resolvedTheme);
    return fn();
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  emitThemeChange,
  getAsyncHooks,
  getTheme,
  onThemeChange,
  setTheme,
  withTheme,
  withThemeSync
});
