"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;
};
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, $bun, $logger, $userSettingsService, $projectConfigService) {
        this.$errors = $errors;
        this.$npm = $npm;
        this.$options = $options;
        this.$yarn = $yarn;
        this.$yarn2 = $yarn2;
        this.$pnpm = $pnpm;
        this.$bun = $bun;
        this.$logger = $logger;
        this.$userSettingsService = $userSettingsService;
        this.$projectConfigService = $projectConfigService;
    }
    async init() {
        this.packageManager = await this._determinePackageManager();
    }
    async getPackageManagerName() {
        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);
    }
    async isRegistered(packageName) {
        return this.packageManager.isRegistered(packageName);
    }
    async getPackageFullName(packageNameParts) {
        return this.packageManager.getPackageFullName(packageNameParts);
    }
    async getPackageNameParts(fullPackageName) {
        return this.packageManager.getPackageNameParts(fullPackageName);
    }
    getRegistryPackageData(packageName) {
        return this.packageManager.getRegistryPackageData(packageName);
    }
    getCachePath() {
        return this.packageManager.getCachePath();
    }
    async getTagVersion(packageName, tag) {
        let version = null;
        if (!tag) {
            return null;
        }
        try {
            const result = await 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 = await this.getRegistryPackageData(packageName);
            version = registryData["dist-tags"][tag];
        }
        return version;
    }
    async _determinePackageManager() {
        let pm = null;
        try {
            pm = await 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 if (pm === constants_1.PackageManagers.bun) {
            this._packageManagerName = constants_1.PackageManagers.bun;
            return this.$bun;
        }
        else {
            this._packageManagerName = constants_1.PackageManagers.npm;
            return this.$npm;
        }
    }
}
exports.PackageManager = PackageManager;
__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);
yok_1.injector.register("packageManager", PackageManager);
