"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const helpers_1 = require("./common/helpers");
class BasePackageManager {
    constructor($childProcess, $hostInfo, $pacoteService, packageManager) {
        this.$childProcess = $childProcess;
        this.$hostInfo = $hostInfo;
        this.$pacoteService = $pacoteService;
        this.packageManager = packageManager;
    }
    getPackageManagerExecutableName() {
        let npmExecutableName = this.packageManager;
        if (this.$hostInfo.isWindows) {
            npmExecutableName += ".cmd";
        }
        return npmExecutableName;
    }
    processPackageManagerInstall(packageName, params, opts) {
        return __awaiter(this, void 0, void 0, function* () {
            const npmExecutable = this.getPackageManagerExecutableName();
            const stdioValue = helpers_1.isInteractive() ? "inherit" : "pipe";
            yield this.$childProcess.spawnFromEvent(npmExecutable, params, "close", { cwd: opts.cwd, stdio: stdioValue });
            const { isInstallingAllDependencies } = opts;
            if (isInstallingAllDependencies) {
                return null;
            }
            const packageMetadata = yield 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') {
                array.push(`--${flag}`);
                array.push(`${config[flag]}`);
            }
            else if (config[flag]) {
                if (flag === "dist-tags" || flag === "versions") {
                    array.push(` ${flag}`);
                    continue;
                }
                array.push(`--${flag}`);
            }
        }
        if (asArray) {
            return array;
        }
        return array.join(" ");
    }
}
exports.BasePackageManager = BasePackageManager;
