/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { getPlayerServiceConfig, listFlowsAsync as listFlowsActionAsync, } from '@microsoft/power-apps-actions';
import { getCliLogger, getSettings } from '../CliSettings.js';
import { createDefaultStringOption } from '../CliUtils.js';
import { HelpStrings } from '../Constants/HelpStrings.js';
import { CliFS } from '../FS/CliFs.js';
import { printTable } from '../Utils/ConsoleOutput.js';
import { ListFlowsArguments } from './VerbConstants.js';
/**
 * List all cloud flows from a Dataverse environment.
 * The org URL is discovered from the power.config.json file.
 *
 * @param {ArgumentProvider} argumentProvider
 * @returns {Promise<void>}
 */
export async function listFlowsAsync(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();
    // Use the already initialized authentication provider from ArgumentProvider
    const authenticationProvider = argumentProvider.getAuthenticationProvider();
    // Get the optional search parameter
    const search = await argumentProvider.getOption(ListFlowsArguments.Search.name, true);
    const context = {
        vfs: cliFs,
        authProvider: authenticationProvider,
        region: playerServiceConfig.region,
        environmentName: playerServiceConfig.environmentName,
        actionsParams: {
            search,
        },
        localFilePaths: {
            powerConfigPath: fileConfig.powerConfigPath,
            schemaPath: fileConfig.schemaPath,
            codeGenPath: fileConfig.codeGenPath,
        },
        logger: getCliLogger(),
    };
    const flows = await listFlowsActionAsync(context);
    if (argumentProvider.isJsonMode()) {
        console.log(JSON.stringify(flows, null, 2));
        return;
    }
    printTable({
        title: 'Cloud Flows',
        columns: [
            { header: 'Name', maxWidth: 38 },
            { header: 'Status' },
            { header: 'Modified On' },
            { header: 'Flow ID' },
        ],
        // For workflows: statecode 1 = Active/On, 0 = Draft/Off
        rows: flows.map((flow) => [
            flow.name,
            flow.statecode === 1 ? 'Active' : 'Inactive',
            new Date(flow.modifiedOn).toLocaleString(),
            flow.workflowId ?? '',
        ]),
        emptyMessage: 'No flows found.',
        totalLabel: 'flow(s)',
    });
}
function createOptionSchema(argumentProvider) {
    argumentProvider.setDescription(HelpStrings.Verbs['List-flows'].Description);
    argumentProvider.addExamples(HelpStrings.Verbs['List-flows'].Examples);
    argumentProvider.addOptions([
        createDefaultStringOption({
            flags: {
                key: ListFlowsArguments.Search.name,
                alias: ListFlowsArguments.Search.alias,
            },
            env: ListFlowsArguments.Search.envVar,
            message: ListFlowsArguments.Search.promptMessage,
            description: HelpStrings.Verbs['List-flows'].Options.Search,
        }),
    ]);
}
//# sourceMappingURL=ListFlows.js.map