"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFilename = exports.getHTMLPathByEntry = exports.getPackageNameFromModulePath = exports.isFileExists = exports.getDistPath = exports.getAbsoluteDistPath = void 0;
const path_1 = require("path");
const constants_1 = require("./constants");
const utils_1 = require("./utils");
function getAbsoluteDistPath(cwd, outputConfig) {
    const root = (0, exports.getDistPath)(outputConfig, 'root');
    return (0, path_1.isAbsolute)(root) ? root : (0, path_1.join)(cwd, root);
}
exports.getAbsoluteDistPath = getAbsoluteDistPath;
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;
};
exports.getDistPath = getDistPath;
async function isFileExists(file) {
    const { promises, constants } = await Promise.resolve().then(() => __importStar(require('fs')));
    return promises
        .access(file, constants.F_OK)
        .then(() => true)
        .catch(() => false);
}
exports.isFileExists = isFileExists;
function getPackageNameFromModulePath(modulePath) {
    const handleModuleContext = modulePath === null || modulePath === void 0 ? void 0 : modulePath.match(constants_1.MODULE_PATH_REGEX);
    if (!handleModuleContext) {
        return false;
    }
    const [, , scope, name] = handleModuleContext;
    const packageName = ['npm', (scope !== null && scope !== void 0 ? scope : '').replace('@', ''), name]
        .filter(Boolean)
        .join('.');
    return packageName;
}
exports.getPackageNameFromModulePath = getPackageNameFromModulePath;
function getHTMLPathByEntry(entryName, config) {
    const htmlPath = (0, exports.getDistPath)(config.output, 'html');
    const filename = config.html.disableHtmlFolder
        ? `${entryName}.html`
        : `${entryName}/index.html`;
    return (0, utils_1.removeLeadingSlash)(`${htmlPath}/${filename}`);
}
exports.getHTMLPathByEntry = getHTMLPathByEntry;
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 !== void 0 ? _a : `[name]${isProd ? hash : ''}.js`;
        case 'css':
            return (_b = filename.css) !== null && _b !== void 0 ? _b : `[name]${isProd ? hash : ''}.css`;
        case 'svg':
            return (_c = filename.svg) !== null && _c !== void 0 ? _c : `[name]${hash}.svg`;
        case 'font':
            return (_d = filename.font) !== null && _d !== void 0 ? _d : `[name]${hash}[ext]`;
        case 'image':
            return (_e = filename.image) !== null && _e !== void 0 ? _e : `[name]${hash}[ext]`;
        case 'media':
            return (_f = filename.media) !== null && _f !== void 0 ? _f : `[name]${hash}[ext]`;
        default:
            throw new Error(`unknown key ${type} in "output.filename"`);
    }
};
exports.getFilename = getFilename;
