"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) {
    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 decorators_1 = require("./common/decorators");
class PackageManager {
    constructor($errors, $npm, $options, $yarn, $userSettingsService) {
        this.$errors = $errors;
        this.$npm = $npm;
        this.$options = $options;
        this.$yarn = $yarn;
        this.$userSettingsService = $userSettingsService;
    }
    init() {
        return __awaiter(this, void 0, void 0, function* () {
            this.packageManager = yield this._determinePackageManager();
        });
    }
    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();
    }
    _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}`);
            }
            if (pm === 'yarn' || this.$options.yarn) {
                return this.$yarn;
            }
            else {
                return this.$npm;
            }
        });
    }
}
__decorate([
    decorators_1.cache()
], PackageManager.prototype, "init", null);
__decorate([
    decorators_1.exported("packageManager"),
    decorators_1.invokeInit()
], PackageManager.prototype, "install", null);
__decorate([
    decorators_1.exported("packageManager"),
    decorators_1.invokeInit()
], PackageManager.prototype, "uninstall", null);
__decorate([
    decorators_1.exported("packageManager"),
    decorators_1.invokeInit()
], PackageManager.prototype, "view", null);
__decorate([
    decorators_1.exported("packageManager"),
    decorators_1.invokeInit()
], PackageManager.prototype, "search", null);
__decorate([
    decorators_1.invokeInit()
], PackageManager.prototype, "searchNpms", null);
__decorate([
    decorators_1.invokeInit()
], PackageManager.prototype, "isRegistered", null);
__decorate([
    decorators_1.invokeInit()
], PackageManager.prototype, "getPackageFullName", null);
__decorate([
    decorators_1.invokeInit()
], PackageManager.prototype, "getPackageNameParts", null);
__decorate([
    decorators_1.invokeInit()
], PackageManager.prototype, "getRegistryPackageData", null);
__decorate([
    decorators_1.invokeInit()
], PackageManager.prototype, "getCachePath", null);
exports.PackageManager = PackageManager;
$injector.register('packageManager', PackageManager);
