/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { getPlayerServiceConfig, refreshDataSourceAsync as refreshDataSourceActionsAsync, } from '@microsoft/power-apps-actions';
import { getCliLogger, getSettings } from '../CliSettings.js';
import { promptConfirm, promptText } from '../CliUtils.js';
import { HelpStrings } from '../Constants/HelpStrings.js';
import { printError, printInfo, printSuccess } from '../Utils/ConsoleOutput.js';
import { RefreshDataSourceArguments } from './VerbConstants.js';
export async function refreshDataSourceAsync(argumentProvider) {
    const settings = getSettings();
    const { fileConfig, cliFs } = settings;
    createOptionSchema(argumentProvider);
    await argumentProvider.runVerb();
    const playerServiceConfig = getPlayerServiceConfig();
    const authenticationProvider = argumentProvider.getAuthenticationProvider();
    const rawDataSourceName = await argumentProvider.getOption('dataSourceName');
    const dataSourceName = rawDataSourceName || undefined;
    const config = {
        vfs: cliFs,
        authProvider: authenticationProvider,
        region: playerServiceConfig.region,
        environmentName: playerServiceConfig.environmentName,
        localFilePaths: {
            powerConfigPath: fileConfig.powerConfigPath,
            schemaPath: fileConfig.schemaPath,
            codeGenPath: fileConfig.codeGenPath,
        },
        logger: getCliLogger(),
        actionsParams: {
            dataSourceName,
        },
    };
    const result = await refreshDataSourceActionsAsync(config);
    for (const dsName of result.succeeded) {
        printSuccess(`'${dsName}' refreshed successfully.`);
    }
    for (const failure of result.failed) {
        printError(`Failed to refresh '${failure.dataSourceName}': ${failure.error}`);
    }
    if (result.succeeded.length > 0 && result.failed.length === 0) {
        printInfo("Hint: Run 'npx power-apps run' to test locally, or 'npx power-apps push' to deploy.");
    }
}
function createOptionSchema(argumentProvider) {
    argumentProvider.setDescription(HelpStrings.Verbs['Refresh-data-source'].Description);
    argumentProvider.addExamples(HelpStrings.Verbs['Refresh-data-source'].Examples ?? []);
    argumentProvider.addOptions([
        {
            flags: {
                key: RefreshDataSourceArguments.DataSourceName.name,
                alias: RefreshDataSourceArguments.DataSourceName.alias,
            },
            env: RefreshDataSourceArguments.DataSourceName.envVar,
            description: HelpStrings.Verbs['Refresh-data-source'].Options?.DataSourceName ?? '',
            optionalInNonInteractive: true,
            customPrompt: async () => {
                const hasSpecificDs = await promptConfirm({
                    message: 'Do you want to refresh a specific data source? (No to refresh all)',
                });
                if (hasSpecificDs) {
                    return await promptText({
                        message: RefreshDataSourceArguments.DataSourceName.promptMessage,
                    });
                }
                return '';
            },
        },
    ]);
}
//# sourceMappingURL=RefreshDataSource.js.map