"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InteractiveUpgrader = void 0;
const prompt_1 = __importDefault(require("inquirer/lib/ui/prompt"));
const npm_check_fork_1 = require("@rushstack/npm-check-fork");
const terminal_1 = require("@rushstack/terminal");
const InteractiveUpgradeUI_1 = require("../utilities/InteractiveUpgradeUI");
const SearchListPrompt_1 = require("../utilities/prompts/SearchListPrompt");
class InteractiveUpgrader {
    constructor(rushConfiguration) {
        this._rushConfiguration = rushConfiguration;
    }
    async upgradeAsync() {
        const rushProject = await this._getUserSelectedProjectForUpgradeAsync();
        const dependenciesState = await this._getPackageDependenciesStatusAsync(rushProject);
        const depsToUpgrade = await this._getUserSelectedDependenciesToUpgradeAsync(dependenciesState);
        return { projects: [rushProject], depsToUpgrade };
    }
    async _getUserSelectedDependenciesToUpgradeAsync(packages) {
        return (0, InteractiveUpgradeUI_1.upgradeInteractive)(packages);
    }
    async _getUserSelectedProjectForUpgradeAsync() {
        const projects = this._rushConfiguration.projects;
        const ui = new prompt_1.default({
            list: SearchListPrompt_1.SearchListPrompt
        });
        const { selectProject } = await ui.run([
            {
                name: 'selectProject',
                message: 'Select a project you would like to upgrade',
                type: 'list',
                choices: projects.map((project) => {
                    return {
                        name: terminal_1.Colorize.green(project.packageName),
                        value: project
                    };
                }),
                pageSize: 12
            }
        ]);
        return selectProject;
    }
    async _getPackageDependenciesStatusAsync(rushProject) {
        var _a;
        const { projectFolder } = rushProject;
        const currentState = await (0, npm_check_fork_1.NpmCheck)({
            cwd: projectFolder
        });
        return (_a = currentState.packages) !== null && _a !== void 0 ? _a : [];
    }
}
exports.InteractiveUpgrader = InteractiveUpgrader;
//# sourceMappingURL=InteractiveUpgrader.js.map