"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 Stage 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(Stage);
                    let skip = false;
                    let files;
                    if (!args.files && !flags.all) {
                        // throw new Error("Operation cancelled: You must either specify files or set the 'all' flag (--all or -A).");
                        let unstaged = yield this._ops.gitListChanges("unstaged");
                        if (unstaged.length > 0) {
                            let result = yield inquirer.prompt({
                                type: 'checkbox',
                                name: 'files',
                                message: 'Changes to stage: (ctrl-c to cancel) ',
                                choices: unstaged
                            });
                            files = result.files;
                        }
                        else {
                            this._ui.userMessage("Nothing to stage.");
                            skip = true;
                        }
                    }
                    else {
                        files = (args.files) ? args.files.split(" ") : undefined;
                        // TODO:  Verify that the specified files do indeed exist.  Git will silently ignore any files that are not changed.
                        //        THOUGH we dont really need to cuz git will throw if they dont.
                        // let uncomitted = await this._ops.gitListChanges();
                    }
                    if (!skip) {
                        yield this._ops.gitStage(files);
                        if (flags.commit) {
                            yield this._ops.gitCommit(flags.commit);
                        }
                    }
                    resolve();
                }
                catch (err) {
                    this.handleError(err);
                    reject(err);
                }
            }));
        });
    }
}
exports.default = Stage;
Stage.description = 'Flags files to be included in the next commit.';
Stage.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    all: core_1.Flags.boolean({ char: 'A', description: 'Include all modified files.' }),
    commit: core_1.Flags.string({ char: 'c', description: 'Commit the staged changes using the given message', helpValue: 'message' })
};
Stage.args = {
    files: core_1.Args.string({ description: 'Space-separated list of files to stage. Paths are relative to the main project root.', require: false })
};
//# sourceMappingURL=stage.js.map