"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const _ = require("lodash");
const inquirer = require("inquirer");
const command_base_tenantless_1 = require("../../command-base-tenantless");
class PkgRemove extends command_base_tenantless_1.CommandBaseTenantless {
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.bootstrap();
            const { args, flags } = yield this.parse(PkgRemove);
            if (!this._projCfg.packages || _.isEmpty(this._projCfg.packages)) {
                this._ui.userMessage(`No package exports are defined for this project.`);
                this._ui.userMessage(`Operation cancelled.  No work was done.`);
            }
            else {
                let packageName;
                if (!args.packageName) {
                    let names = [];
                    for (let key in this._projCfg.packages)
                        names.push(key);
                    if (names.length > 0) {
                        let result = yield inquirer.prompt({ type: 'list', name: 'name', message: 'package definition to remove:', choices: names });
                        if (result.name) {
                            packageName = result.name;
                        }
                        else {
                            this._ui.userMessage(`Operation cancelled.  No work was done.`);
                            this.exit(0);
                        }
                    }
                }
                else {
                    packageName = args.packageName;
                }
                if (packageName) {
                    if (this._projCfg.packages[packageName]) {
                        if (_.unset(this._projCfg.packages, packageName)) {
                            yield this.projCfgRepo.updateProjectConfigFile(this._projCfg);
                            this._ui.userMessage(`COMPLETE!  Removed package export: ${this._styler.hilite(packageName)}`);
                        }
                        else {
                            this._ui.userMessage(`Unsucessful.  Unknown failure to delete that package.  You may need to manually remove it from the config file.`);
                        }
                    }
                    else {
                        this._ui.userMessage(`This project has no package export named [${packageName}].`);
                        this._ui.userMessage(`No work was done.`);
                    }
                }
                else {
                    this._ui.userMessage(`Operation cancelled.  No work was done.`);
                    this.exit(0);
                }
            }
        });
    }
}
exports.default = PkgRemove;
PkgRemove.description = 'Deletes the given package export from the configuration.';
PkgRemove.aliases = ['pkg:delete'];
PkgRemove.flags = {
    help: core_1.Flags.help({ char: 'h' })
};
// TODO:  Make this arg optional and provide a prompt to choose the package to remove.
PkgRemove.args = {
    packageName: core_1.Args.string({ required: false, description: 'package name including, scope prefix. eg:  "your-company/your-package"' })
};
//# sourceMappingURL=remove.js.map