"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvaluateFlowStep = exports.EVALUATE_FLOW_STEP_NAME = void 0;
exports.isEvaluateFlowStep = isEvaluateFlowStep;
const mablscriptFind_1 = require("../mablscriptFind");
const MablAction_1 = require("../MablAction");
const MablStepV2_1 = require("../MablStepV2");
exports.EVALUATE_FLOW_STEP_NAME = 'EvaluateFlow';
class EvaluateFlowStep extends MablStepV2_1.MablStepV2 {
    static stepName = exports.EVALUATE_FLOW_STEP_NAME;
    static actionCode = 'evaluate_flow';
    static mablScriptStepNames = [EvaluateFlowStep.actionCode];
    static yamlMablScriptNames = [EvaluateFlowStep.stepName];
    flowDescriptor;
    config;
    constructor(descriptor) {
        super(EvaluateFlowStep.stepName, descriptor, EvaluateFlowStep.actionCode);
        if (!this.isValidDescriptor(this.descriptor)) {
            throw new Error(`${EvaluateFlowStep.stepName}: descriptor.flow must be defined`);
        }
        this.flowDescriptor = this.descriptor.flow;
        this.config = this.descriptor.config;
    }
    isValidDescriptor(value) {
        return value.flow !== undefined;
    }
    getStepName() {
        return EvaluateFlowStep.stepName;
    }
    stepDescription() {
        return `Evaluate Flow "${this.flowDescriptor.invariant_id}"`;
    }
    static fromYaml(_stepName, stepArgs) {
        return new EvaluateFlowStep(stepArgs);
    }
    static fromLegacyMablscript(args, _actions) {
        if (args.length < 1 || args.length > 2) {
            throw new Error(`EvaluateFlowStep.fromLegacyMablscript expects 1 or 2 arguments but received ${args.length}`);
        }
        const flowDescriptor = args[0];
        const config = args.length === 2 ? args[1] : undefined;
        const stepDescriptor = {
            flow: flowDescriptor,
            config,
        };
        const step = new EvaluateFlowStep(stepDescriptor);
        step.actions = _actions;
        return step;
    }
    toMablscript() {
        const flowReference = (0, mablscriptFind_1.buildStepArgumentString)({
            params: { ...this.flowDescriptor },
            legacy: true,
        });
        const flowConfig = this.config
            ? ', ' + (0, mablscriptFind_1.buildStepArgumentString)({ params: { ...this.config }, legacy: true })
            : '';
        return `${EvaluateFlowStep.mablScriptStepNames[0]}(${flowReference}${flowConfig})`;
    }
    getInputVariables() {
        return (0, MablAction_1.distinctStrings)([
            ...MablAction_1.MablAction.findUniqueVariableReferencesInValue(this.flowDescriptor),
            ...(this.config
                ? MablAction_1.MablAction.findUniqueVariableReferencesInValue(this.config)
                : []),
        ]);
    }
    toLineDiffFormat(_omitStepId = false) {
        const descriptor = {
            flow: this.flowDescriptor,
        };
        if (this.config) {
            descriptor.config = this.config;
        }
        const lineDiffProperties = {
            annotation: this.annotation,
            disabled: this.isDisabled(),
            descriptor: descriptor,
        };
        return this.stringify(lineDiffProperties);
    }
    static create(descriptor) {
        descriptor.id = descriptor.id ?? MablStepV2_1.MablStepV2.generateStepId();
        return new EvaluateFlowStep(descriptor);
    }
}
exports.EvaluateFlowStep = EvaluateFlowStep;
function isEvaluateFlowStep(step) {
    return step?.getStepName() === exports.EVALUATE_FLOW_STEP_NAME;
}
