import { createContext, use } from "react";
import { jsx } from "react/jsx-runtime";
//#region src/core/configurable-module.tsx
function chainProviders(outer, inner) {
	if (!outer) return inner;
	const Outer = outer;
	const Inner = inner;
	const Chained = ({ children }) => /* @__PURE__ */ jsx(Outer, { children: /* @__PURE__ */ jsx(Inner, { children }) });
	Chained.displayName = "ChainedProvider";
	return Chained;
}
/**
* Build a configurable module factory. The config travels to renderers through
* a dedicated React Context (the module `Provider`); `useConfig` reads it.
* `createModule(base)` produces a `ConfigurableModule` usable directly (default
* config) or via `.setup(config)` — the NestJS `DynamicModule` analog.
*/
function defineConfigurableModule(opts) {
	const ConfigContext = createContext(opts.defaultConfig);
	const useConfig = () => use(ConfigContext);
	function createModule(base) {
		function build(config) {
			const ConfigProvider = ({ children }) => /* @__PURE__ */ jsx(ConfigContext, {
				value: config,
				children
			});
			ConfigProvider.displayName = `${opts.name}ConfigProvider`;
			return {
				...base,
				name: opts.name,
				Provider: chainProviders(base.Provider, ConfigProvider)
			};
		}
		const configurable = build(opts.defaultConfig);
		configurable.setup = (config) => build({
			...opts.defaultConfig,
			...config
		});
		return configurable;
	}
	return {
		createModule,
		useConfig
	};
}
//#endregion
export { defineConfigurableModule as t };
