// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import Prompt from 'inquirer/lib/ui/prompt';
import { NpmCheck } from '@rushstack/npm-check-fork';
import { Colorize } from '@rushstack/terminal';
import { upgradeInteractive } from '../utilities/InteractiveUpgradeUI';
import { SearchListPrompt } from '../utilities/prompts/SearchListPrompt';
export 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 upgradeInteractive(packages);
    }
    async _getUserSelectedProjectForUpgradeAsync() {
        const projects = this._rushConfiguration.projects;
        const ui = new Prompt({
            list: 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: Colorize.green(project.packageName),
                        value: project
                    };
                }),
                pageSize: 12
            }
        ]);
        return selectProject;
    }
    async _getPackageDependenciesStatusAsync(rushProject) {
        var _a;
        const { projectFolder } = rushProject;
        const currentState = await NpmCheck({
            cwd: projectFolder
        });
        return (_a = currentState.packages) !== null && _a !== void 0 ? _a : [];
    }
}
//# sourceMappingURL=InteractiveUpgrader.js.map