/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { getPlayerServiceConfig, listDatasetsAsync as listDatasetsActionAsync, } 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 { SharedArguments } from './VerbConstants.js';
export async function listDatasetsAsync(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();
    const dsConfig = {
        vfs: cliFs,
        authProvider: authenticationProvider,
        region: playerServiceConfig.region,
        environmentName: playerServiceConfig.environmentName,
        actionsParams: {
            apiId: await argumentProvider.getOption('apiId'),
            connectionId: await argumentProvider.getOption('connectionId'),
        },
        localFilePaths: {
            powerConfigPath: fileConfig.powerConfigPath,
            schemaPath: fileConfig.schemaPath,
            codeGenPath: fileConfig.codeGenPath,
        },
        logger: getCliLogger(),
    };
    const datasets = await listDatasetsActionAsync(dsConfig);
    if (argumentProvider.isJsonMode()) {
        console.log(JSON.stringify(datasets?.value ?? [], null, 2));
        return;
    }
    printTable({
        title: 'Datasets',
        metadata: [
            `Connector: ${dsConfig.actionsParams.apiId}`,
            `Connection: ${dsConfig.actionsParams.connectionId}`,
        ],
        columns: [{ header: 'Display Name' }, { header: 'Name' }],
        rows: datasets.value.map((dataset) => [
            dataset.DisplayName || '<No Display Name>',
            dataset.Name,
        ]),
        totalLabel: 'dataset(s) found',
    });
}
function createOptionSchema(argumentProvider) {
    argumentProvider.setDescription(HelpStrings.Verbs['List-datasets'].Description);
    argumentProvider.addExamples(HelpStrings.Verbs['List-datasets'].Examples);
    argumentProvider.addOptions([
        createDefaultStringOption({
            flags: {
                key: SharedArguments.ApiId.name,
                alias: SharedArguments.ApiId.alias,
            },
            env: SharedArguments.ApiId.envVar,
            message: SharedArguments.ApiId.promptMessage,
            description: HelpStrings.Verbs['List-datasets'].Options.ApiId,
        }),
        createDefaultStringOption({
            flags: {
                key: SharedArguments.ConnectionId.name,
                alias: SharedArguments.ConnectionId.alias,
            },
            env: SharedArguments.ConnectionId.envVar,
            message: SharedArguments.ConnectionId.promptMessage,
            description: HelpStrings.Verbs['List-datasets'].Options.ConnectionId,
        }),
    ]);
}
//# sourceMappingURL=ListDatasets.js.map