import { z } from 'zod';
import { globalOptionsZod } from '../../../../Command.js';
import request from '../../../../request.js';
import { formatting } from '../../../../utils/formatting.js';
import PowerAutomateCommand from '../../../base/PowerAutomateCommand.js';
import commands from '../../commands.js';
export const options = z.strictObject({
    ...globalOptionsZod.shape,
    name: z.string().alias('n'),
    flowName: z.uuid(),
    environmentName: z.string().alias('e'),
    withTrigger: z.boolean().optional(),
    withActions: z.union([z.string(), z.boolean()]).optional()
});
class FlowRunGetCommand extends PowerAutomateCommand {
    get name() {
        return commands.RUN_GET;
    }
    get description() {
        return 'Gets information about a specific run of the specified Microsoft Flow';
    }
    get schema() {
        return options;
    }
    async commandAction(logger, args) {
        if (this.verbose) {
            await logger.logToStderr(`Retrieving information about run ${args.options.name} of Microsoft Flow ${args.options.flowName}...`);
        }
        const actionsParameter = args.options.withActions ? '$expand=properties%2Factions&' : '';
        const requestOptions = {
            url: `${PowerAutomateCommand.resource}/providers/Microsoft.ProcessSimple/environments/${formatting.encodeQueryParameter(args.options.environmentName)}/flows/${formatting.encodeQueryParameter(args.options.flowName)}/runs/${formatting.encodeQueryParameter(args.options.name)}?${actionsParameter}api-version=2016-11-01`,
            headers: {
                accept: 'application/json'
            },
            responseType: 'json'
        };
        try {
            const res = await request.get(requestOptions);
            res.startTime = res.properties.startTime;
            res.endTime = res.properties.endTime || '';
            res.status = res.properties.status;
            res.triggerName = res.properties.trigger.name;
            if (args.options.withTrigger && res.properties.trigger.outputsLink) {
                res.triggerInformation = await this.getTriggerInformation(res);
            }
            if (args.options.withActions) {
                res.actions = await this.getActionsInformation(res, args.options.withActions);
            }
            await logger.log(res);
        }
        catch (err) {
            this.handleRejectedODataJsonPromise(err);
        }
    }
    async getTriggerInformation(res) {
        return await this.requestAdditionalInformation(res.properties.trigger.outputsLink.uri);
    }
    async getActionsInformation(res, withActions) {
        const chosenActions = typeof withActions === 'string' ? withActions.split(',') : null;
        const actionsResult = {};
        for (const action in res.properties.actions) {
            if (!res.properties.actions[action] || (chosenActions && chosenActions.indexOf(action) === -1)) {
                continue;
            }
            actionsResult[action] = res.properties.actions[action];
            if (res.properties.actions[action].inputsLink?.uri) {
                actionsResult[action].input = await this.requestAdditionalInformation(res.properties.actions[action].inputsLink?.uri);
            }
            if (res.properties.actions[action].outputsLink?.uri) {
                actionsResult[action].output = await this.requestAdditionalInformation(res.properties.actions[action].outputsLink?.uri);
            }
        }
        return actionsResult;
    }
    async requestAdditionalInformation(requestUri) {
        const additionalInformationOptions = {
            url: requestUri,
            headers: {
                accept: 'application/json',
                'x-anonymous': true
            },
            responseType: 'json'
        };
        const additionalInformationResponse = await request.get(additionalInformationOptions);
        return additionalInformationResponse.body ? additionalInformationResponse.body : additionalInformationResponse;
    }
}
export default new FlowRunGetCommand();
//# sourceMappingURL=run-get.js.map