"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var css_exports = {};
__export(css_exports, {
  getCssLoaderOptions: () => getCssLoaderOptions,
  getCssModuleLocalIdentName: () => getCssModuleLocalIdentName,
  getCssModulesAutoRule: () => getCssModulesAutoRule,
  getCssnanoDefaultOptions: () => getCssnanoDefaultOptions,
  getPostcssConfig: () => getPostcssConfig,
  getResolveUrlJoinFn: () => getResolveUrlJoinFn,
  isCssModules: () => isCssModules,
  isInNodeModules: () => isInNodeModules,
  isLooseCssModules: () => isLooseCssModules,
  isUseCssExtract: () => isUseCssExtract,
  normalizeCssLoaderOptions: () => normalizeCssLoaderOptions
});
module.exports = __toCommonJS(css_exports);
var import_assert = __toESM(require("assert"));
var import_constants = require("./constants");
var import_deepmerge = __toESM(require("../compiled/deepmerge"));
var import_utils = require("./utils");
var import_mergeChainedOptions = require("./mergeChainedOptions");
const getCssModuleLocalIdentName = (config, isProd) => config.output.cssModules.localIdentName || // Using shorter classname in production to reduce bundle size
(isProd ? "[local]-[hash:base64:6]" : "[path][name]__[local]-[hash:base64:6]");
const isInNodeModules = (path) => import_constants.NODE_MODULES_REGEX.test(path);
const isLooseCssModules = (path) => {
  if (import_constants.NODE_MODULES_REGEX.test(path)) {
    return import_constants.CSS_MODULES_REGEX.test(path);
  }
  return !import_constants.GLOBAL_CSS_REGEX.test(path);
};
const isCssModules = (filename, modules) => {
  if (typeof modules === "boolean") {
    return modules;
  }
  if (typeof modules === "string") {
    return true;
  }
  const { auto } = modules;
  if (typeof auto === "boolean") {
    return auto && import_constants.CSS_MODULES_REGEX.test(filename);
  } else if (auto instanceof RegExp) {
    return auto.test(filename);
  } else if (typeof auto === "function") {
    return auto(filename);
  }
  return true;
};
const getCssModulesAutoRule = (config, disableCssModuleExtension = false) => {
  if (!config || (config == null ? void 0 : config.auto) === void 0) {
    return disableCssModuleExtension ? isLooseCssModules : true;
  }
  return config.auto;
};
const getCssnanoDefaultOptions = () => ({
  preset: [
    "default",
    {
      // merge longhand will break safe-area-inset-top, so disable it
      // https://github.com/cssnano/cssnano/issues/803
      // https://github.com/cssnano/cssnano/issues/967
      mergeLonghand: false
    }
  ]
});
const getPostcssConfig = ({
  enableCssMinify,
  enableSourceMap,
  browserslist,
  config
}) => {
  const extraPlugins = [];
  const utils = {
    addPlugins(plugins) {
      if (Array.isArray(plugins)) {
        extraPlugins.push(...plugins);
      } else {
        extraPlugins.push(plugins);
      }
    }
  };
  const autoprefixerOptions = (0, import_mergeChainedOptions.mergeChainedOptions)({
    defaults: {
      flexbox: "no-2009",
      overrideBrowserslist: browserslist
    },
    options: config.tools.autoprefixer
  });
  const defaultPostcssConfig = {
    postcssOptions: {
      plugins: [
        require((0, import_utils.getSharedPkgCompiledPath)("postcss-flexbugs-fixes")),
        require((0, import_utils.getSharedPkgCompiledPath)("autoprefixer"))(autoprefixerOptions)
      ].filter(Boolean)
    },
    sourceMap: enableSourceMap
  };
  const mergedConfig = (0, import_mergeChainedOptions.mergeChainedOptions)({
    defaults: defaultPostcssConfig,
    options: config.tools.postcss,
    utils
  });
  if (extraPlugins.length) {
    (0, import_assert.default)("postcssOptions" in mergedConfig);
    (0, import_assert.default)("plugins" in mergedConfig.postcssOptions);
    mergedConfig.postcssOptions.plugins.push(...extraPlugins);
  }
  return mergedConfig;
};
const normalizeCssLoaderOptions = (options, exportOnlyLocals) => {
  if (options.modules && exportOnlyLocals) {
    let { modules } = options;
    if (modules === true) {
      modules = { exportOnlyLocals: true };
    } else if (typeof modules === "string") {
      modules = { mode: modules, exportOnlyLocals: true };
    } else {
      modules = {
        ...modules,
        exportOnlyLocals: true
      };
    }
    return {
      ...options,
      modules
    };
  }
  return options;
};
const getCssLoaderOptions = ({
  config,
  enableSourceMap,
  importLoaders,
  isServer,
  isWebWorker,
  localIdentName
}) => {
  const { cssModules } = config.output;
  const defaultOptions = {
    importLoaders,
    modules: {
      auto: getCssModulesAutoRule(
        cssModules,
        config.output.disableCssModuleExtension
      ),
      exportLocalsConvention: cssModules.exportLocalsConvention,
      localIdentName
    },
    sourceMap: enableSourceMap
  };
  const mergedCssLoaderOptions = (0, import_mergeChainedOptions.mergeChainedOptions)({
    defaults: defaultOptions,
    options: config.tools.cssLoader,
    mergeFn: import_deepmerge.default
  });
  const cssLoaderOptions = normalizeCssLoaderOptions(
    mergedCssLoaderOptions,
    isServer || isWebWorker
  );
  return cssLoaderOptions;
};
const isUseCssExtract = (config, target) => !config.output.disableCssExtract && target !== "node" && target !== "web-worker";
const getResolveUrlJoinFn = async () => {
  const {
    createJoinFunction,
    asGenerator,
    createJoinImplementation,
    defaultJoinGenerator
  } = await Promise.resolve().then(() => __toESM(require("../compiled/resolve-url-loader")));
  const rsbuildGenerator = asGenerator((item, ...rest) => {
    if (!item.uri.startsWith(".")) {
      return [null];
    }
    return defaultJoinGenerator(item, ...rest);
  });
  return createJoinFunction(
    "rsbuild-resolve-join-fn",
    createJoinImplementation(rsbuildGenerator)
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getCssLoaderOptions,
  getCssModuleLocalIdentName,
  getCssModulesAutoRule,
  getCssnanoDefaultOptions,
  getPostcssConfig,
  getResolveUrlJoinFn,
  isCssModules,
  isInNodeModules,
  isLooseCssModules,
  isUseCssExtract,
  normalizeCssLoaderOptions
});
