"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resetFlowInstanceIdIncrementer = resetFlowInstanceIdIncrementer;
exports.humanizeExecuteFlow = humanizeExecuteFlow;
exports.executeFlowStepToAIString = executeFlowStepToAIString;
const BaseStep_1 = __importDefault(require("../canonical/BaseStep"));
const mablscript_step_types_1 = require("../../mablscript-step-types");
const yaml_utils_1 = require("../yaml-utils");
const EvaluateFlowStep_1 = require("@mablhq/test-model/steps/EvaluateFlowStep");
let FLOW_STEP_INSTANCE_ID_INCREMENTER = 0;
function resetFlowInstanceIdIncrementer() {
    FLOW_STEP_INSTANCE_ID_INCREMENTER = 0;
}
class ExecuteFlowStep extends BaseStep_1.default {
    static getStepType() {
        return mablscript_step_types_1.StepType.EXECUTE_FLOW;
    }
    static generateStepFromMablStep(evalFlowStep) {
        const invariantId = evalFlowStep.flowDescriptor.invariant_id;
        const flowStub = {
            invariant_id: invariantId,
            reusable: true,
        };
        const executeFlowStep = new ExecuteFlowStep({
            flowInvariantId: invariantId,
            flow: flowStub,
            isError: false,
        });
        const existingStepId = evalFlowStep.stepId();
        if (existingStepId) {
            executeFlowStep.setStepId(existingStepId);
        }
        if (evalFlowStep.config) {
            executeFlowStep.flowConfiguration = evalFlowStep.config;
        }
        if (evalFlowStep.config?.iterations) {
            executeFlowStep.iterationConfig = evalFlowStep.config.iterations;
        }
        return executeFlowStep;
    }
    constructor(params) {
        super(ExecuteFlowStep.getStepType());
        this.instanceId = FLOW_STEP_INSTANCE_ID_INCREMENTER.toString();
        FLOW_STEP_INSTANCE_ID_INCREMENTER++;
        if (params.isError === true) {
            this.flowError = params.flowError;
        }
        else {
            this.flowInvariantId = params.flowInvariantId;
            this.flow = params.flow;
            this.flowConfiguration = params.flowConfiguration;
        }
        this.collapsed = true;
    }
    willGenerateMablscript() {
        return false;
    }
    inputVariables() {
        return [];
    }
    buildFlowConfiguration() {
        const iterations = this.flowConfiguration?.iterations ?? this.iterationConfig;
        if (this.flowConfiguration) {
            return { ...this.flowConfiguration, iterations };
        }
        if (iterations) {
            return { iterations };
        }
        return undefined;
    }
    generateMablStep() {
        const flowInvariantId = this.flow?.invariant_id ?? this.flowInvariantId;
        if (!flowInvariantId) {
            throw new Error('Cannot generate MablStep for ExecuteFlowStep without flowInvariantId');
        }
        const descriptor = {
            flow: {
                invariant_id: flowInvariantId,
            },
            config: this.buildFlowConfiguration(),
        };
        const evalFlowStep = new EvaluateFlowStep_1.EvaluateFlowStep(descriptor);
        if (this.id) {
            evalFlowStep.setStepId(this.id);
        }
        return evalFlowStep;
    }
}
exports.default = ExecuteFlowStep;
function humanizeExecuteFlow(step) {
    if (step.flowError) {
        return `Missing flow "${step.flowInvariantId}"`;
    }
    return `Start flow "${step.flow?.description ?? step.flowInvariantId ?? 'Unknown flow'}"`;
}
function executeFlowStepToAIString(step) {
    if (step.flowError) {
        return (0, yaml_utils_1.toYaml)({
            stepType: step.stepType,
            error: 'missing_flow',
            flow_id: step.flowInvariantId,
        });
    }
    return (0, yaml_utils_1.toYaml)({
        stepType: step.stepType,
        flow_name: step.flow?.description,
        flow_id: step.flowInvariantId,
    });
}
