/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { addDataverseApiAsync as addDataverseApiActionAsync, getPlayerServiceConfig, } 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 { AddDataverseApiArguments } from './VerbConstants.js';
/**
 * Adds a Dataverse API (custom action, custom function, SDK message, or first-party
 * action/function) to the app.
 *
 * @param {ArgumentProvider} argumentProvider
 * @returns {Promise<void>}
 */
export async function addDataverseApiAsync(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 apiName = await argumentProvider.getOption(AddDataverseApiArguments.ApiName.key);
    if (!apiName) {
        throw new Error('Dataverse API name is required.');
    }
    const context = {
        vfs: cliFs,
        authProvider: authenticationProvider,
        region: playerServiceConfig.region,
        environmentName: playerServiceConfig.environmentName,
        actionsParams: {
            apiName,
        },
        localFilePaths: {
            powerConfigPath: fileConfig.powerConfigPath,
            schemaPath: fileConfig.schemaPath,
            codeGenPath: fileConfig.codeGenPath,
        },
        logger: getCliLogger(),
    };
    await addDataverseApiActionAsync(context);
    console.log(`Dataverse API '${apiName}' added successfully.`);
    console.log("Hint: Run 'npx power-apps run' to test locally, or 'npx power-apps push' to deploy.");
}
function createOptionSchema(argumentProvider) {
    argumentProvider.setDescription(HelpStrings.Verbs['Add-dataverse-api'].Description);
    argumentProvider.addExamples(HelpStrings.Verbs['Add-dataverse-api'].Examples);
    argumentProvider.addOptions([
        createDefaultStringOption({
            flags: {
                key: AddDataverseApiArguments.ApiName.name,
                alias: AddDataverseApiArguments.ApiName.alias,
            },
            env: AddDataverseApiArguments.ApiName.envVar,
            message: AddDataverseApiArguments.ApiName.promptMessage,
            description: HelpStrings.Verbs['Add-dataverse-api'].Options.ApiName,
        }),
    ]);
}
//# sourceMappingURL=AddDataverseApi.js.map