/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { getPlayerServiceConfig, listConnectionsAsync as listConnectionsAction, } 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 { ListConnectionsArguments } from './VerbConstants.js';
export async function listConnectionsAsync(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 search = await argumentProvider.getOption(ListConnectionsArguments.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 connections = await listConnectionsAction(context);
    if (argumentProvider.isJsonMode()) {
        console.log(JSON.stringify(connections, null, 2));
        return;
    }
    printTable({
        title: 'Connections',
        columns: [
            { header: 'Display Name', maxWidth: 38 },
            { header: 'Api ID', maxWidth: 40 },
            { header: 'Connection ID' },
        ],
        rows: connections.map((c) => [
            c.properties.displayName,
            c.properties.apiId.split('/').pop() || c.properties.apiId,
            c.name,
        ]),
        emptyMessage: 'No connections found.',
        totalLabel: 'connection(s)',
    });
}
function createOptionSchema(argumentProvider) {
    argumentProvider.setDescription(HelpStrings.Verbs['List-connections'].Description);
    argumentProvider.addExamples(HelpStrings.Verbs['List-connections'].Examples);
    argumentProvider.addOptions([
        createDefaultStringOption({
            flags: {
                key: ListConnectionsArguments.Search.name,
                alias: ListConnectionsArguments.Search.alias,
            },
            env: ListConnectionsArguments.Search.envVar,
            message: ListConnectionsArguments.Search.promptMessage,
            description: HelpStrings.Verbs['List-connections'].Options.Search,
        }),
    ]);
}
//# sourceMappingURL=ListConnections.js.map