"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PackageManager = void 0;
const decorators_1 = require("./common/decorators");
const decorators_2 = require("./common/decorators");
const constants_1 = require("./constants");
const yok_1 = require("./common/yok");
class PackageManager {
    constructor($errors, $npm, $options, $yarn, $yarn2, $pnpm, $logger, $userSettingsService, $projectConfigService) {
        this.$errors = $errors;
        this.$npm = $npm;
        this.$options = $options;
        this.$yarn = $yarn;
        this.$yarn2 = $yarn2;
        this.$pnpm = $pnpm;
        this.$logger = $logger;
        this.$userSettingsService = $userSettingsService;
        this.$projectConfigService = $projectConfigService;
    }
    init() {
        return __awaiter(this, void 0, void 0, function* () {
            this.packageManager = yield this._determinePackageManager();
        });
    }
    getPackageManagerName() {
        return __awaiter(this, void 0, void 0, function* () {
            return this._packageManagerName;
        });
    }
    install(packageName, pathToSave, config) {
        return this.packageManager.install(packageName, pathToSave, config);
    }
    uninstall(packageName, config, path) {
        return this.packageManager.uninstall(packageName, config, path);
    }
    view(packageName, config) {
        return this.packageManager.view(packageName, config);
    }
    search(filter, config) {
        return this.packageManager.search(filter, config);
    }
    searchNpms(keyword) {
        return this.packageManager.searchNpms(keyword);
    }
    isRegistered(packageName) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.packageManager.isRegistered(packageName);
        });
    }
    getPackageFullName(packageNameParts) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.packageManager.getPackageFullName(packageNameParts);
        });
    }
    getPackageNameParts(fullPackageName) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.packageManager.getPackageNameParts(fullPackageName);
        });
    }
    getRegistryPackageData(packageName) {
        return this.packageManager.getRegistryPackageData(packageName);
    }
    getCachePath() {
        return this.packageManager.getCachePath();
    }
    getTagVersion(packageName, tag) {
        return __awaiter(this, void 0, void 0, function* () {
            let version = null;
            if (!tag) {
                return null;
            }
            try {
                const result = yield this.view(packageName, { "dist-tags": true });
                version = result[tag];
            }
            catch (err) {
                this.$logger.trace(`Error while getting tag version from view command: ${err}`);
                const registryData = yield this.getRegistryPackageData(packageName);
                version = registryData["dist-tags"][tag];
            }
            return version;
        });
    }
    _determinePackageManager() {
        return __awaiter(this, void 0, void 0, function* () {
            let pm = null;
            try {
                pm = yield this.$userSettingsService.getSettingValue("packageManager");
            }
            catch (err) {
                this.$errors.fail(`Unable to read package manager config from user settings ${err}`);
            }
            try {
                const configPm = this.$projectConfigService.getValue("cli.packageManager");
                if (configPm) {
                    this.$logger.trace(`Determined packageManager to use from user config is: ${configPm}`);
                    pm = configPm;
                }
            }
            catch (err) {
                this.$logger.trace("Tried to read cli.packageManager from project config and failed. Error is: ", err);
            }
            if (pm === constants_1.PackageManagers.yarn || this.$options.yarn) {
                this._packageManagerName = constants_1.PackageManagers.yarn;
                return this.$yarn;
            }
            if (pm === constants_1.PackageManagers.yarn2 || this.$options.yarn2) {
                this._packageManagerName = constants_1.PackageManagers.yarn2;
                return this.$yarn2;
            }
            else if (pm === constants_1.PackageManagers.pnpm || this.$options.pnpm) {
                this._packageManagerName = constants_1.PackageManagers.pnpm;
                return this.$pnpm;
            }
            else {
                this._packageManagerName = constants_1.PackageManagers.npm;
                return this.$npm;
            }
        });
    }
}
__decorate([
    (0, decorators_1.cache)()
], PackageManager.prototype, "init", null);
__decorate([
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "getPackageManagerName", null);
__decorate([
    (0, decorators_1.exported)("packageManager"),
    (0, decorators_2.performanceLog)(),
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "install", null);
__decorate([
    (0, decorators_1.exported)("packageManager"),
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "uninstall", null);
__decorate([
    (0, decorators_1.exported)("packageManager"),
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "view", null);
__decorate([
    (0, decorators_1.exported)("packageManager"),
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "search", null);
__decorate([
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "searchNpms", null);
__decorate([
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "isRegistered", null);
__decorate([
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "getPackageFullName", null);
__decorate([
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "getPackageNameParts", null);
__decorate([
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "getRegistryPackageData", null);
__decorate([
    (0, decorators_1.invokeInit)()
], PackageManager.prototype, "getCachePath", null);
exports.PackageManager = PackageManager;
yok_1.injector.register("packageManager", PackageManager);
