"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const inquirer = require("inquirer");
const command_base_tenantless_1 = require("../../command-base-tenantless");
class DepUnsource extends command_base_tenantless_1.CommandBaseTenantless {
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                try {
                    this.bootstrap();
                    const { args, flags } = yield this.parse(DepUnsource);
                    let projectRoot = this._ops.getConfigRoot();
                    let select = false;
                    let names = yield this._projCfg.submodules.map(sm => sm.folderName);
                    if (names.length == 0) {
                        this.logger.error(`Cannot unsource: none of your dependencies are currently sourced.`, `Use 'cod dep:source' to source a dependency.`);
                    }
                    else {
                        let name;
                        if (args.name && names.indexOf(args.name) >= 0) {
                            name = args.name;
                        }
                        else {
                            let result = yield inquirer.prompt({ type: 'list', name: 'name', message: 'dependency to unsource:', choices: names });
                            if (!result.name) {
                                this._ui.userMessage("Operation cancelled: No work was done.");
                                this.exit(0);
                            }
                            else {
                                name = result.name;
                            }
                        }
                        if (name !== undefined) {
                            yield this._ops.unSymLinkSubmodule(name);
                            yield this._ops.removeSubmodule(name, flags.discard);
                        }
                        // let nxVersion = "";
                        // if (flags.discard) {
                        //   nxVersion = "last used version"  // where do we get this?
                        // } else {
                        //   nxVersion = "version just published"  // we'll need removeSubmodule to return this
                        // }
                        // await this._ops.dependencyInstall(name, nxVersion);
                    }
                    resolve();
                }
                catch (err) {
                    this.handleError(err);
                    reject(err);
                }
            }));
        });
    }
}
exports.default = DepUnsource;
DepUnsource.description = 'Exits source mode for the given dependency by physically removing its source code folder from the project, and points the project dependency back to the package version it previously depended on.';
DepUnsource.aliases = ['dep:unsrc'];
DepUnsource.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    discard: core_1.Flags.boolean({ char: 'd', description: 'Discards all uncommitted changes (staged and unstaged) before unsourcing, and goes back to the previously installed version of the dependency.  Again, uncomitted changes are LOST when this flag is set.' }),
    comment: core_1.Flags.boolean({ char: 'c', description: 'The commit message for any outstanding changes.' })
};
DepUnsource.args = {
    name: core_1.Args.string({ required: false, description: 'The official unique name of the dependency package.' })
};
//# sourceMappingURL=unsource.js.map