"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_map_set = require('../primordials/map-set.js');
const require_themes_themes = require('./themes.js');

//#region src/themes/context.ts
let _async_hooks;
/**
* Emit theme change event to listeners.
*
* @private
*/
function emitThemeChange(theme) {
	for (const listener of listeners) listener(theme);
}
/**
* Lazily load the async_hooks module to avoid Webpack errors.
*
* @private
*/
/*@__NO_SIDE_EFFECTS__*/
function getAsyncHooks() {
	if (_async_hooks === void 0) _async_hooks = /*@__PURE__*/ require("node:async_hooks");
	return _async_hooks;
}
/**
* AsyncLocalStorage for theme context isolation.
*/
const { AsyncLocalStorage } = /* @__PURE__ */ getAsyncHooks();
const themeStorage = new AsyncLocalStorage();
/**
* Fallback theme for global context.
*/
let fallbackTheme = require_themes_themes.SOCKET_THEME;
/**
* Registered theme change listeners.
*/
const listeners = new require_primordials_map_set.SetCtor();
/**
* Get the active theme from context.
*
* @example
*   ;```ts
*   const theme = getTheme()
*   console.log(theme.displayName)
*   ```
*
* @returns Current theme
*/
function getTheme() {
	return themeStorage.getStore() ?? fallbackTheme;
}
/**
* Subscribe to theme change events.
*
* @example
*   ;```ts
*   const unsubscribe = onThemeChange(theme => {
*     console.log('Theme:', theme.displayName)
*   })
*
*   // Cleanup
*   unsubscribe()
*   ```
*
* @param listener - Change handler.
*
* @returns Unsubscribe function
*/
function onThemeChange(listener) {
	listeners.add(listener);
	return () => {
		listeners.delete(listener);
	};
}
/**
* Set the global fallback theme.
*
* @example
*   ;```ts
*   setTheme('socket-firewall')
*   ```
*
* @param theme - Theme name or object.
*/
function setTheme(theme) {
	fallbackTheme = typeof theme === "string" ? require_themes_themes.THEMES[theme] ?? fallbackTheme : theme;
	emitThemeChange(fallbackTheme);
}
/**
* Execute async operation with scoped theme. Theme automatically restored on
* completion.
*
* @example
*   ;```ts
*   await withTheme('ultra', async () => {
*     // Operations use Ultra theme
*   })
*   ```
*
* @template T - Return type.
*
* @param theme - Scoped theme.
* @param fn - Async operation.
*
* @returns Operation result
*/
async function withTheme(theme, fn) {
	const resolvedTheme = typeof theme === "string" ? require_themes_themes.THEMES[theme] ?? fallbackTheme : theme;
	return await themeStorage.run(resolvedTheme, async () => {
		emitThemeChange(resolvedTheme);
		return await fn();
	});
}
/**
* Execute sync operation with scoped theme. Theme automatically restored on
* completion.
*
* @example
*   ;```ts
*   const result = withThemeSync('coana', () => {
*     return processData()
*   })
*   ```
*
* @template T - Return type.
*
* @param theme - Scoped theme.
* @param fn - Sync operation.
*
* @returns Operation result
*/
function withThemeSync(theme, fn) {
	const resolvedTheme = typeof theme === "string" ? require_themes_themes.THEMES[theme] ?? fallbackTheme : theme;
	return themeStorage.run(resolvedTheme, () => {
		emitThemeChange(resolvedTheme);
		return fn();
	});
}

//#endregion
exports.emitThemeChange = emitThemeChange;
exports.getAsyncHooks = getAsyncHooks;
exports.getTheme = getTheme;
exports.onThemeChange = onThemeChange;
exports.setTheme = setTheme;
exports.withTheme = withTheme;
exports.withThemeSync = withThemeSync;