/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { getPlayerServiceConfig, listAllEnvironmentVariablesAsync as listAllEnvironmentVariablesActionAsync, } from '@microsoft/power-apps-actions';
import { getCliLogger, getSettings } from '../CliSettings.js';
import { HelpStrings } from '../Constants/HelpStrings.js';
import { CliFS } from '../FS/CliFs.js';
import { printKeyValueList } from '../Utils/ConsoleOutput.js';
/**
 * Get environment variables from a Dataverse environment.
 * Expects the following arguments:
 * - envUrl: The environment URL for Dataverse data sources
 *
 * @param {ArgumentProvider} argumentProvider
 * @returns {Promise<void>}
 */
export async function listAllEnvironmentVariablesAsync(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: {},
        localFilePaths: {
            powerConfigPath: fileConfig.powerConfigPath,
            schemaPath: fileConfig.schemaPath,
            codeGenPath: fileConfig.codeGenPath,
        },
        logger: getCliLogger(),
    };
    const environmentVariableDefinitions = await listAllEnvironmentVariablesActionAsync(dsConfig);
    if (argumentProvider.isJsonMode()) {
        console.log(JSON.stringify(environmentVariableDefinitions, null, 2));
        return;
    }
    printKeyValueList({
        title: 'Environment Variables:',
        items: environmentVariableDefinitions,
        fields: [
            { label: 'Schema Name', value: (v) => v.schemaname },
            { label: 'Display Name', value: (v) => v.displayname },
            { label: 'Type', value: (v) => String(v.type) },
            {
                label: 'Value',
                value: (v) => v.environmentvariabledefinition_environmentvariablevalue[0]?.value ?? '<No Value>',
            },
        ],
    });
}
function createOptionSchema(argumentProvider) {
    argumentProvider.setDescription(HelpStrings.Verbs['List-environment-variables'].Description);
    argumentProvider.addExamples(HelpStrings.Verbs['List-environment-variables'].Examples);
}
//# sourceMappingURL=ListEnvironmentVariables.js.map