"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasePackageManager = void 0;
const helpers_1 = require("./common/helpers");
class BasePackageManager {
    constructor($childProcess, $fs, $hostInfo, $pacoteService, packageManager) {
        this.$childProcess = $childProcess;
        this.$fs = $fs;
        this.$hostInfo = $hostInfo;
        this.$pacoteService = $pacoteService;
        this.packageManager = packageManager;
    }
    async isRegistered(packageName) {
        if (this.isURL(packageName) ||
            this.$fs.exists(packageName) ||
            this.isTgz(packageName)) {
            return false;
        }
        try {
            const viewResult = await this.view(packageName, { name: true });
            const packageNameRegex = new RegExp(packageName, "i");
            const isProperResult = packageNameRegex.test(viewResult);
            return isProperResult;
        }
        catch (e) {
            return false;
        }
    }
    async getPackageNameParts(fullPackageName) {
        const lastIndexOfAtSign = fullPackageName.lastIndexOf("@");
        let version = "";
        let templateName = "";
        if (lastIndexOfAtSign > 0) {
            templateName = fullPackageName.substr(0, lastIndexOfAtSign).toLowerCase();
            version = fullPackageName.substr(lastIndexOfAtSign + 1);
        }
        return {
            name: templateName || fullPackageName,
            version: version,
        };
    }
    async getPackageFullName(packageNameParts) {
        return packageNameParts.version
            ? `${packageNameParts.name}@${packageNameParts.version}`
            : packageNameParts.name;
    }
    getPackageManagerExecutableName() {
        let npmExecutableName = this.packageManager;
        if (this.$hostInfo.isWindows) {
            npmExecutableName += ".cmd";
        }
        return npmExecutableName;
    }
    async processPackageManagerInstall(packageName, params, opts) {
        const npmExecutable = this.getPackageManagerExecutableName();
        const stdioValue = (0, helpers_1.isInteractive)() ? "inherit" : "pipe";
        await this.$childProcess.spawnFromEvent(npmExecutable, params, "close", {
            cwd: opts.cwd,
            stdio: stdioValue,
            shell: this.$hostInfo.isWindows,
        });
        const { isInstallingAllDependencies } = opts;
        if (isInstallingAllDependencies) {
            return null;
        }
        const packageMetadata = await this.$pacoteService.manifest(packageName);
        return {
            name: packageMetadata.name,
            version: packageMetadata.version,
        };
    }
    getFlagsString(config, asArray) {
        const array = [];
        for (const flag in config) {
            if (flag === "global" &&
                this.packageManager !== "yarn" &&
                this.packageManager !== "yarn2") {
                array.push(`--${flag}`);
                array.push(`${config[flag]}`);
            }
            else if (config[flag]) {
                if (flag === "dist-tags" ||
                    flag === "versions" ||
                    flag === "name" ||
                    flag === "gradle" ||
                    flag === "version_info") {
                    if (this.packageManager === "yarn2") {
                        array.push(`--fields ${flag}`);
                    }
                    else {
                        array.push(` ${flag}`);
                    }
                    continue;
                }
                array.push(`--${flag}`);
            }
        }
        if (asArray) {
            return array;
        }
        return array.join(" ");
    }
    isTgz(packageName) {
        return packageName.indexOf(".tgz") >= 0;
    }
    isURL(str) {
        const urlRegex = "^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$";
        const url = new RegExp(urlRegex, "i");
        return str.length < 2083 && url.test(str);
    }
}
exports.BasePackageManager = BasePackageManager;
