"use strict";
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());
    });
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander = require("commander");
const codeArtifactUtils_1 = require("../utils/codeArtifactUtils");
const ensureAuth_1 = require("./utils/ensureAuth");
const logger_1 = require("./utils/logger");
const WizError_1 = require("../modules/WizError");
const I18n_1 = require("../modules/I18n");
const Logger_1 = require("../modules/Logger");
const utils_1 = require("../utils");
commander
    .name('wiz unpublish')
    .usage('<package> [options]')
    .description('Remove package version from the private repository. Use the `-r global` option to remove from the global repository')
    .addOption(new commander.Option('-r, --repository <repositoryType>', 'remove package version from the global repository').choices(['global']))
    .parse(process.argv);
unpublish(commander.args, commander.opts())
    .catch(logger_1.handleError)
    .finally(logger_1.closeLogger);
function unpublish(args = [], options) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
        validateArgs(args);
        const packageArg = args[0];
        const _b = (0, utils_1.parseNpmPackageSpec)(packageArg), { version } = _b, params = __rest(_b, ["version"]);
        validateVersion(version);
        const ewizard = yield (0, ensureAuth_1.default)();
        const cwd = process.cwd();
        const repositoryType = (_a = options.repository) !== null && _a !== void 0 ? _a : (yield (0, codeArtifactUtils_1.getRepositoryType)(cwd));
        yield ewizard.deletePackageVersionsFromCodeArtifact({
            repositoryType,
            packageArg,
            packageSpec: Object.assign({ versions: [version] }, params),
        });
        Logger_1.default.info(I18n_1.default.info('UNPUBLISH_SUCCESS'));
    });
}
function validateArgs(args) {
    if (!args.length) {
        throw new WizError_1.WizError(I18n_1.default.error('UNPUBLISH_ARGUMENTS_INVALID'));
    }
}
function validateVersion(version) {
    if (!version) {
        throw new WizError_1.WizError(I18n_1.default.error('UNPUBLISH_VERSION_INVALID'));
    }
}
//# sourceMappingURL=wiz-unpublish.js.map