"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,
  getAbsoluteDistPath: () => getAbsoluteDistPath,
  getDistPath: () => getDistPath,
  getFilename: () => getFilename,
  getHTMLPathByEntry: () => getHTMLPathByEntry,
  getPackageNameFromModulePath: () => getPackageNameFromModulePath,
  isFileExists: () => isFileExists
});
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");
function getAbsoluteDistPath(cwd, outputConfig) {
  const root = getDistPath(outputConfig, "root");
  return (0, import_path.isAbsolute)(root) ? root : (0, import_path.join)(cwd, root);
}
const getDistPath = (outputConfig, type) => {
  const { distPath } = outputConfig;
  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 findExists = (files) => {
  for (const file of files) {
    if ((0, import_fs.existsSync)(file) && (0, import_fs.statSync)(file).isFile()) {
      return file;
    }
  }
  return false;
};
function getPackageNameFromModulePath(modulePath) {
  const handleModuleContext = modulePath == null ? void 0 : modulePath.match(import_constants.MODULE_PATH_REGEX);
  if (!handleModuleContext) {
    return false;
  }
  const [, , scope, name] = handleModuleContext;
  const packageName = ["npm", (scope != null ? scope : "").replace("@", ""), name].filter(Boolean).join(".");
  return packageName;
}
function getHTMLPathByEntry(entryName, config) {
  const htmlPath = getDistPath(config.output, "html");
  const filename = config.html.outputStructure === "flat" ? `${entryName}.html` : `${entryName}/index.html`;
  return (0, import_utils.removeLeadingSlash)(`${htmlPath}/${filename}`);
}
const getFilename = (output, type, isProd) => {
  var _a, _b, _c, _d, _e, _f;
  const { filename } = output;
  const useHash = !output.disableFilenameHash;
  const hash = useHash ? ".[contenthash:8]" : "";
  switch (type) {
    case "js":
      return (_a = filename.js) != null ? _a : `[name]${isProd ? hash : ""}.js`;
    case "css":
      return (_b = filename.css) != null ? _b : `[name]${isProd ? hash : ""}.css`;
    case "svg":
      return (_c = filename.svg) != null ? _c : `[name]${hash}.svg`;
    case "font":
      return (_d = filename.font) != null ? _d : `[name]${hash}[ext]`;
    case "image":
      return (_e = filename.image) != null ? _e : `[name]${hash}[ext]`;
    case "media":
      return (_f = filename.media) != null ? _f : `[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 == null ? void 0 : 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 {
      const stats = (0, import_fs.statSync)(filePath, { throwIfNoEntry: false });
      if (stats == null ? void 0 : stats.isFile()) {
        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,
  getAbsoluteDistPath,
  getDistPath,
  getFilename,
  getHTMLPathByEntry,
  getPackageNameFromModulePath,
  isFileExists
});
