/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { getPlayerServiceConfig, removeFlowAsync as removeFlowActionAsync, } from '@microsoft/power-apps-actions';
import { getCliLogger, getSettings } from '../CliSettings.js';
import { promptConfirm, promptText } from '../CliUtils.js';
import { HelpStrings } from '../Constants/HelpStrings.js';
import { CliFS } from '../FS/CliFs.js';
import { RemoveFlowArguments } from './VerbConstants.js';
/**
 * Remove a cloud flow from the current Power Apps code app.
 *
 * @param {ArgumentProvider} argumentProvider
 * @returns {Promise<void>}
 */
export async function removeFlowAsync(argumentProvider) {
    const settings = getSettings();
    const { fileConfig } = settings;
    createOptionSchema(argumentProvider);
    await argumentProvider.runVerb();
    const cliFs = new CliFS({
        cwd: process.cwd(),
        allowCwdRead: true,
        allowCwdWrite: true,
    });
    const playerServiceConfig = getPlayerServiceConfig();
    const authenticationProvider = argumentProvider.getAuthenticationProvider();
    const flowDataSourceName = await argumentProvider.getOption('flowName');
    const flowId = await argumentProvider.getOption('flowId');
    const context = {
        vfs: cliFs,
        authProvider: authenticationProvider,
        region: playerServiceConfig.region,
        environmentName: playerServiceConfig.environmentName,
        actionsParams: {
            flowDataSourceName,
            flowId,
        },
        localFilePaths: {
            powerConfigPath: fileConfig.powerConfigPath,
            schemaPath: fileConfig.schemaPath,
            codeGenPath: fileConfig.codeGenPath,
        },
        logger: getCliLogger(),
    };
    const result = await removeFlowActionAsync(context);
    const flowIdentifier = flowDataSourceName ?? flowId ?? '';
    if (!result.found) {
        console.log(`\nFlow "${flowIdentifier}" was not found in power.config.`);
    }
    else if (result.success) {
        console.log(`\nFlow "${flowIdentifier}" removed successfully.`);
    }
    else {
        console.error(`\nFailed to remove flow: ${result.error}`);
    }
}
function createOptionSchema(argumentProvider) {
    argumentProvider.setDescription(HelpStrings.Verbs['Remove-flow'].Description);
    argumentProvider.addOptions([
        {
            flags: {
                key: RemoveFlowArguments.FlowDataSourceName.name,
                alias: RemoveFlowArguments.FlowDataSourceName.alias,
            },
            env: RemoveFlowArguments.FlowDataSourceName.envVar,
            description: HelpStrings.Verbs['Remove-flow'].Options.FlowDataSourceName,
            optionalInNonInteractive: true,
            customPrompt: async () => {
                const useFlowName = await promptConfirm({
                    message: 'Do you want to remove a flow by name?',
                });
                if (useFlowName) {
                    return await promptText({
                        message: RemoveFlowArguments.FlowDataSourceName.promptMessage,
                    });
                }
                return undefined;
            },
        },
        {
            flags: {
                key: RemoveFlowArguments.FlowId.name,
                alias: RemoveFlowArguments.FlowId.alias,
            },
            env: RemoveFlowArguments.FlowId.envVar,
            description: HelpStrings.Verbs['Remove-flow'].Options.FlowId,
            optionalInNonInteractive: true,
            customPrompt: async () => {
                const existingName = await argumentProvider.getOption('flowName', true);
                if (existingName)
                    return undefined;
                return await promptText({ message: RemoveFlowArguments.FlowId.promptMessage });
            },
        },
    ]);
}
//# sourceMappingURL=RemoveFlow.js.map