import { z } from 'zod';
import chalk from 'chalk';
import { cli } from '../../../../cli/cli.js';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import { formatting } from '../../../../utils/formatting.js';
import commands from '../../commands.js';
import PowerAutomateCommand from '../../../base/PowerAutomateCommand.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    name: z.string().alias('n'),
    flowName: z.uuid(),
    environmentName: z.string().alias('e'),
    force: z.boolean().optional().alias('f')
});
class FlowRunResubmitCommand extends PowerAutomateCommand {
    get name() {
        return commands.RUN_RESUBMIT;
    }
    get description() {
        return 'Resubmits a specific flow run for the specified Microsoft Flow';
    }
    get schema() {
        return options;
    }
    async commandAction(logger, args) {
        if (this.verbose) {
            await logger.logToStderr(`Resubmitting run ${args.options.name} of Microsoft Flow ${args.options.flowName}...`);
        }
        const resubmitFlow = async () => {
            try {
                const triggerName = await this.getTriggerName(args.options.environmentName, args.options.flowName);
                if (this.debug) {
                    await logger.logToStderr(chalk.yellow(`Retrieved trigger: ${triggerName}`));
                }
                const requestOptions = {
                    url: `${PowerAutomateCommand.resource}/providers/Microsoft.ProcessSimple/environments/${formatting.encodeQueryParameter(args.options.environmentName)}/flows/${formatting.encodeQueryParameter(args.options.flowName)}/triggers/${formatting.encodeQueryParameter(triggerName)}/histories/${formatting.encodeQueryParameter(args.options.name)}/resubmit?api-version=2016-11-01`,
                    headers: {
                        accept: 'application/json'
                    },
                    responseType: 'json'
                };
                await request.post(requestOptions);
            }
            catch (err) {
                this.handleRejectedODataJsonPromise(err);
            }
        };
        if (args.options.force) {
            await resubmitFlow();
        }
        else {
            const result = await cli.promptForConfirmation({ message: `Are you sure you want to resubmit the flow with run ${args.options.name}?` });
            if (result) {
                await resubmitFlow();
            }
        }
    }
    async getTriggerName(environment, flow) {
        const requestOptions = {
            url: `${PowerAutomateCommand.resource}/providers/Microsoft.ProcessSimple/environments/${formatting.encodeQueryParameter(environment)}/flows/${formatting.encodeQueryParameter(flow)}/triggers?api-version=2016-11-01`,
            headers: {
                accept: 'application/json'
            },
            responseType: 'json'
        };
        const res = await request.get(requestOptions);
        return res.value[0].name;
    }
}
export default new FlowRunResubmitCommand();
//# sourceMappingURL=run-resubmit.js.map