/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-29d58ba | Cube Dev Team */
import { __require } from "../../_virtual/_rolldown/runtime.js";
import * as path from "path";

//#region src/tasty/zero/next.ts
/**
* Next.js configuration wrapper for tasty-zero.
*
* Provides a convenient way to configure the Babel plugin for Next.js projects.
*
* @example
* ```javascript
* // next.config.js
* const { withTastyZero } = require('@cube-dev/ui-kit/tasty/zero/next');
*
* module.exports = withTastyZero({
*   output: 'public/tasty.css',
* })({
*   // your Next.js config
* });
* ```
*/
/**
* Next.js configuration wrapper for tasty-zero.
*
* @param options - Configuration options
* @returns A function that wraps the Next.js config
*
* @example
* ```javascript
* // next.config.js
* const { withTastyZero } = require('@cube-dev/ui-kit/tasty/zero/next');
*
* module.exports = withTastyZero()({
*   reactStrictMode: true,
* });
* ```
*/
function withTastyZero(options = {}) {
	const { output = "public/tasty.css", enabled = true } = options;
	return (nextConfig = {}) => {
		if (!enabled) return nextConfig;
		return {
			...nextConfig,
			webpack(config, context) {
				const { isServer, dir } = context;
				if (!isServer) {
					const absoluteOutput = path.resolve(dir || process.cwd(), output);
					const babelPluginConfig = [__require.resolve("./babel"), { output: absoluteOutput }];
					const existingRule = config.module?.rules?.find((rule) => rule.use?.loader === "babel-loader" || rule.use?.some?.((u) => u.loader === "babel-loader"));
					if (existingRule) {
						const babelUse = Array.isArray(existingRule.use) ? existingRule.use.find((u) => u.loader === "babel-loader") : existingRule.use;
						if (babelUse?.options) {
							babelUse.options.plugins = babelUse.options.plugins || [];
							babelUse.options.plugins.push(babelPluginConfig);
						}
					} else {
						config.module = config.module || {};
						config.module.rules = config.module.rules || [];
						config.module.rules.push({
							test: /\.(tsx?|jsx?)$/,
							exclude: /node_modules/,
							use: [{
								loader: __require.resolve("babel-loader"),
								options: { plugins: [babelPluginConfig] }
							}]
						});
					}
				}
				if (typeof nextConfig.webpack === "function") return nextConfig.webpack(config, context);
				return config;
			}
		};
	};
}

//#endregion
export { withTastyZero };
//# sourceMappingURL=next.js.map