/*! © 2026 Yahoo, Inc. UDS Tailwind and Purger v0.0.0-development */
import { entries } from "../../utils/entries.js";
import { isFunction, isString, merge, partition } from "lodash-es";
import plugin from "tailwindcss/plugin.js";

//#region src/tailwind/utils/composeTailwindPlugins.ts
const shouldNotMergeTokensIntoTailwindTheme = (disableOverrideTailwindTheme, feature) => {
	return disableOverrideTailwindTheme === true || Array.isArray(disableOverrideTailwindTheme) && disableOverrideTailwindTheme.includes(feature);
};
const composeTailwindPlugins = (...plugins) => plugin.withOptions((options) => {
	if (!options.config) throw new Error("`tailwindPlugin` requires a config object. If your project has not yet sync'd a config, you can import `defaultTokensConfig` from `@yahoo/uds/defaultTokensConfig` and pass it to `tailwindPlugin`.");
	options.disableOverrideTailwindTheme ??= false;
	options.onlyTheme ??= false;
	options.ignorePluginSafelists ??= false;
	return (api) => {
		if (options.onlyTheme) return;
		const { addBase, addComponents, addUtilities, config } = api;
		const parentConfigImportant = config().important;
		const useImportNamespace = isString(parentConfigImportant);
		const rootSelector = useImportNamespace ? parentConfigImportant : ":root";
		const fns = {
			...api,
			addBase(css) {
				const [vars, classes] = (Array.isArray(css) ? css : [css]).map((rule) => entries(rule)).map((pairs) => partition(pairs, ([key]) => key.startsWith("--"))).reduce((acc, [vars, classes]) => [{
					...acc[0],
					...Object.fromEntries(vars)
				}, {
					...acc[1],
					...Object.fromEntries(classes)
				}], [{}, {}]);
				addBase({ [rootSelector]: vars });
				addBase(classes);
			},
			addComponents(css, options = {}) {
				addComponents(css, {
					respectImportant: useImportNamespace,
					...options
				});
			},
			addUtilities(css, options = {}) {
				addUtilities(css, {
					respectPrefix: true,
					respectImportant: useImportNamespace,
					...options
				});
			}
		};
		for (const plugin of plugins) plugin(options).handler(fns);
	};
}, (options) => {
	if (!options.config) throw new Error("`tailwindPlugin` requires a config object. If your project has not yet sync'd a config, you can import `defaultTokensConfig` from `@yahoo/uds/defaultTokensConfig` and pass it to `tailwindPlugin`.");
	const pluginConfigs = plugins.map((plugin) => plugin(options).config ?? {});
	for (const config of pluginConfigs) for (const [key, value] of entries(config.theme?.extend ?? {})) {
		if (!isFunction(value)) continue;
		throw new Error(`The theme.extend.${key} value cannot be a function when using composeTailwindPlugins. Please resolve the function to an object instead.`);
	}
	const mergedConfig = merge({}, ...pluginConfigs);
	mergedConfig.safelist = options.ignorePluginSafelists ? options.safelist : Array.from(new Set([...options.safelist ?? [], ...pluginConfigs.flatMap((c) => c.safelist ?? [])]));
	return mergedConfig;
});

//#endregion
export { composeTailwindPlugins, shouldNotMergeTokensIntoTailwindTheme };
//# sourceMappingURL=composeTailwindPlugins.js.map