"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 fs_exports = {};
__export(fs_exports, {
  findExists: () => findExists,
  findUp: () => findUp,
  findUpSync: () => findUpSync,
  fse: () => import_fs_extra.default,
  getDistPath: () => getDistPath,
  getFilename: () => getFilename,
  getHTMLPathByEntry: () => getHTMLPathByEntry,
  getPackageNameFromModulePath: () => getPackageNameFromModulePath,
  isFileExists: () => isFileExists,
  isFileSync: () => isFileSync
});
module.exports = __toCommonJS(fs_exports);
var import_path = __toESM(require("path"));
var import_fs_extra = __toESM(require("../compiled/fs-extra"));
var import_constants = require("./constants");
var import_utils = require("./utils");
var import_fs = require("fs");
const getDistPath = (config, type) => {
  const { distPath } = config.output || {};
  const ret = distPath?.[type];
  if (typeof ret !== "string") {
    throw new Error(`unknown key ${type} in "output.distPath"`);
  }
  return ret;
};
async function isFileExists(file) {
  return import_fs.promises.access(file, import_fs.constants.F_OK).then(() => true).catch(() => false);
}
const isFileSync = (filePath) => {
  try {
    return (0, import_fs.statSync)(filePath, { throwIfNoEntry: false })?.isFile();
  } catch (_) {
    return false;
  }
};
const findExists = (files) => {
  for (const file of files) {
    if (isFileSync(file)) {
      return file;
    }
  }
  return false;
};
function getPackageNameFromModulePath(modulePath) {
  const handleModuleContext = modulePath?.match(import_constants.MODULE_PATH_REGEX);
  if (!handleModuleContext) {
    return void 0;
  }
  const [, , scope, name] = handleModuleContext;
  const packageName = ["npm", (scope ?? "").replace("@", ""), name].filter(Boolean).join(".");
  return packageName;
}
function getHTMLPathByEntry(entryName, config) {
  const htmlPath = getDistPath(config, "html");
  const filename = config.html.outputStructure === "flat" ? `${entryName}.html` : `${entryName}/index.html`;
  return (0, import_utils.removeLeadingSlash)(`${htmlPath}/${filename}`);
}
const getFilename = (config, type, isProd) => {
  const { filename } = config.output;
  const useHash = !config.output.disableFilenameHash;
  const hash = useHash ? ".[contenthash:8]" : "";
  switch (type) {
    case "js":
      return filename.js ?? `[name]${isProd ? hash : ""}.js`;
    case "css":
      return filename.css ?? `[name]${isProd ? hash : ""}.css`;
    case "svg":
      return filename.svg ?? `[name]${hash}.svg`;
    case "font":
      return filename.font ?? `[name]${hash}[ext]`;
    case "image":
      return filename.image ?? `[name]${hash}[ext]`;
    case "media":
      return filename.media ?? `[name]${hash}[ext]`;
    default:
      throw new Error(`unknown key ${type} in "output.filename"`);
  }
};
async function findUp({
  filename,
  cwd = process.cwd()
}) {
  const { root } = import_path.default.parse(cwd);
  let dir = cwd;
  while (dir && dir !== root) {
    const filePath = import_path.default.join(dir, filename);
    try {
      const stats = await import_fs.promises.stat(filePath);
      if (stats?.isFile()) {
        return filePath;
      }
    } catch {
    }
    dir = import_path.default.dirname(dir);
  }
}
function findUpSync({
  filename,
  cwd = process.cwd()
}) {
  const { root } = import_path.default.parse(cwd);
  let dir = cwd;
  while (dir && dir !== root) {
    const filePath = import_path.default.join(dir, filename);
    try {
      if (isFileSync(filePath)) {
        return filePath;
      }
    } catch {
    }
    dir = import_path.default.dirname(dir);
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  findExists,
  findUp,
  findUpSync,
  fse,
  getDistPath,
  getFilename,
  getHTMLPathByEntry,
  getPackageNameFromModulePath,
  isFileExists,
  isFileSync
});
