"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 utils_exports = {};
__export(utils_exports, {
  COMPILATION_PROCESS_STAGE: () => COMPILATION_PROCESS_STAGE,
  addTrailingSlash: () => addTrailingSlash,
  awaitableGetter: () => awaitableGetter,
  camelCase: () => camelCase,
  castArray: () => castArray,
  cloneDeep: () => cloneDeep,
  color: () => import_picocolors.default,
  createCacheGroups: () => createCacheGroups,
  createDependenciesRegExp: () => createDependenciesRegExp,
  createVirtualModule: () => createVirtualModule,
  debounce: () => debounce,
  deepmerge: () => import_deepmerge.default,
  ensureAbsolutePath: () => ensureAbsolutePath,
  generateScriptTag: () => generateScriptTag,
  getCoreJsVersion: () => getCoreJsVersion,
  getJsSourceMap: () => getJsSourceMap,
  getProgressColor: () => getProgressColor,
  getPublicPathFromCompiler: () => getPublicPathFromCompiler,
  getSharedPkgCompiledPath: () => getSharedPkgCompiledPath,
  isDev: () => isDev,
  isFunction: () => isFunction,
  isHtmlDisabled: () => isHtmlDisabled,
  isNil: () => isNil,
  isObject: () => isObject,
  isPackageInstalled: () => isPackageInstalled,
  isPlainObject: () => isPlainObject,
  isProd: () => isProd,
  isRegExp: () => isRegExp,
  isServerTarget: () => isServerTarget,
  isString: () => isString,
  isTest: () => isTest,
  isURL: () => isURL,
  isUndefined: () => isUndefined,
  isUsingHMR: () => isUsingHMR,
  isWebTarget: () => isWebTarget,
  onExitProcess: () => onExitProcess,
  partition: () => partition,
  pick: () => pick,
  prettyTime: () => prettyTime,
  removeLeadingSlash: () => removeLeadingSlash,
  removeTailSlash: () => removeTailSlash,
  resolvePackage: () => resolvePackage,
  upperFirst: () => upperFirst
});
module.exports = __toCommonJS(utils_exports);
var import_path = __toESM(require("path"));
var import_fs_extra = __toESM(require("../compiled/fs-extra"));
var import_deepmerge = __toESM(require("../compiled/deepmerge"));
var import_picocolors = __toESM(require("../compiled/picocolors"));
var import_constants = require("./constants");
const isDev = () => process.env.NODE_ENV === "development";
const isProd = () => process.env.NODE_ENV === "production";
const isTest = () => process.env.NODE_ENV === "test";
const isString = (str) => typeof str === "string";
const isUndefined = (obj) => typeof obj === "undefined";
const isFunction = (func) => typeof func === "function";
const isObject = (obj) => obj !== null && typeof obj === "object";
const isPlainObject = (obj) => isObject(obj) && Object.prototype.toString.call(obj) === "[object Object]";
const isRegExp = (obj) => Object.prototype.toString.call(obj) === "[object RegExp]";
const isNil = (o) => o === void 0 || o === null;
const createVirtualModule = (content) => `data:text/javascript,${content}`;
const removeLeadingSlash = (s) => s.replace(/^\/+/, "");
const removeTailSlash = (s) => s.replace(/\/+$/, "");
const addTrailingSlash = (s) => s.endsWith("/") ? s : `${s}/`;
const awaitableGetter = (promises) => {
  const then = (...args) => Promise.all(promises).then(...args);
  return { then, promises };
};
const getJsSourceMap = (config) => {
  const { sourceMap } = config.output;
  if (sourceMap.js === void 0) {
    return isProd() ? false : "cheap-module-source-map";
  }
  return sourceMap.js;
};
const getSharedPkgCompiledPath = (packageName) => import_path.default.join(__dirname, "../compiled", packageName);
const isURL = (str) => str.startsWith("http") || str.startsWith("//:");
function isWebTarget(target) {
  return ["web", "web-worker"].some(
    (t) => (Array.isArray(target) ? target : [target]).includes(t)
  );
}
function isServerTarget(target) {
  return (Array.isArray(target) ? target : [target]).some(
    (item) => ["node", "service-worker"].includes(item)
  );
}
function resolvePackage(loader, dirname) {
  return process.env.VITEST ? loader : require.resolve(loader, { paths: [dirname] });
}
const getCoreJsVersion = (corejsPkgPath) => {
  try {
    const { version } = import_fs_extra.default.readJSONSync(corejsPkgPath);
    const [major, minor] = version.split(".");
    return `${major}.${minor}`;
  } catch (err) {
    return "3";
  }
};
const ensureAbsolutePath = (base, filePath) => import_path.default.isAbsolute(filePath) ? filePath : import_path.default.resolve(base, filePath);
const castArray = (arr) => {
  if (arr === void 0) {
    return [];
  }
  return Array.isArray(arr) ? arr : [arr];
};
const isPackageInstalled = (name, resolvePaths) => {
  try {
    require.resolve(name, { paths: castArray(resolvePaths) });
    return true;
  } catch (err) {
    return false;
  }
};
const camelCase = (input) => input.replace(/[-_](\w)/g, (_, c) => c.toUpperCase());
const cloneDeep = (value) => (0, import_deepmerge.default)({}, value);
const DEP_MATCH_TEMPLATE = /[\\/]node_modules[\\/](<SOURCES>)[\\/]/.source;
const createDependenciesRegExp = (...dependencies) => {
  const sources = dependencies.map(
    (d) => typeof d === "string" ? d : d.source
  );
  const expr = DEP_MATCH_TEMPLATE.replace("<SOURCES>", sources.join("|"));
  return new RegExp(expr);
};
function createCacheGroups(group) {
  const experienceCacheGroup = {};
  Object.entries(group).forEach(([name, pkgs]) => {
    const key = `lib-${name}`;
    experienceCacheGroup[key] = {
      test: createDependenciesRegExp(...pkgs),
      priority: 0,
      name: key,
      reuseExistingChunk: true
    };
  });
  return experienceCacheGroup;
}
function debounce(func, wait) {
  let timeoutId = null;
  return (...args) => {
    if (timeoutId !== null) {
      clearTimeout(timeoutId);
    }
    timeoutId = setTimeout(() => {
      func(...args);
    }, wait);
  };
}
const upperFirst = (str) => str ? str.charAt(0).toUpperCase() + str.slice(1) : "";
const COMPILATION_PROCESS_STAGE = {
  PROCESS_ASSETS_STAGE_ADDITIONAL: -2e3,
  PROCESS_ASSETS_STAGE_PRE_PROCESS: -1e3,
  PROCESS_ASSETS_STAGE_OPTIMIZE_INLINE: 700,
  PROCESS_ASSETS_STAGE_SUMMARIZE: 1e3,
  PROCESS_ASSETS_STAGE_REPORT: 5e3
};
const generateScriptTag = () => ({
  tagName: "script",
  attributes: {
    type: "text/javascript"
  },
  voidTag: false,
  meta: {}
});
const getPublicPathFromCompiler = (compiler) => {
  const { publicPath } = compiler.options.output;
  if (typeof publicPath === "string" && publicPath !== "auto") {
    return addTrailingSlash(publicPath);
  }
  return import_constants.DEFAULT_ASSET_PREFIX;
};
function partition(array, predicate) {
  const truthy = [];
  const falsy = [];
  for (const value of array) {
    if (predicate(value)) {
      truthy.push(value);
    } else {
      falsy.push(value);
    }
  }
  return [truthy, falsy];
}
function pick(obj, keys) {
  return keys.reduce(
    (ret, key) => {
      if (obj[key] !== void 0) {
        ret[key] = obj[key];
      }
      return ret;
    },
    {}
  );
}
const prettyTime = (seconds) => {
  const format = (time) => import_picocolors.default.bold(Number(time));
  if (seconds < 1) {
    const digits = seconds >= 0.01 ? 2 : 3;
    return `${format(seconds.toFixed(digits))} s`;
  }
  if (seconds < 60) {
    return `${format(seconds.toFixed(1))} s`;
  }
  const minutes = seconds / 60;
  return `${format(minutes.toFixed(2))} m`;
};
const colorList = ["green", "cyan", "yellow", "blue", "magenta"];
const getProgressColor = (index) => colorList[index % colorList.length];
function onExitProcess(listener) {
  process.on("exit", listener);
  process.on("SIGINT", () => {
    process.exit(0);
  });
}
const isHtmlDisabled = (config, target) => {
  const { htmlPlugin } = config.tools;
  return htmlPlugin === false || Array.isArray(htmlPlugin) && htmlPlugin.includes(false) || target !== "web";
};
function isUsingHMR(config, { isProd: isProd2, target }) {
  return !isProd2 && target !== "node" && target !== "web-worker" && target !== "service-worker" && config.dev.hmr;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  COMPILATION_PROCESS_STAGE,
  addTrailingSlash,
  awaitableGetter,
  camelCase,
  castArray,
  cloneDeep,
  color,
  createCacheGroups,
  createDependenciesRegExp,
  createVirtualModule,
  debounce,
  deepmerge,
  ensureAbsolutePath,
  generateScriptTag,
  getCoreJsVersion,
  getJsSourceMap,
  getProgressColor,
  getPublicPathFromCompiler,
  getSharedPkgCompiledPath,
  isDev,
  isFunction,
  isHtmlDisabled,
  isNil,
  isObject,
  isPackageInstalled,
  isPlainObject,
  isProd,
  isRegExp,
  isServerTarget,
  isString,
  isTest,
  isURL,
  isUndefined,
  isUsingHMR,
  isWebTarget,
  onExitProcess,
  partition,
  pick,
  prettyTime,
  removeLeadingSlash,
  removeTailSlash,
  resolvePackage,
  upperFirst
});
