"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConditionAction = void 0;
const mablscriptFind_1 = require("../mablscriptFind");
const MablAction_1 = require("../MablAction");
const ConditionDescriptor_1 = require("../types/ConditionDescriptor");
class ConditionAction extends MablAction_1.MablAction {
    static mablScriptStepNames = [
        'evaluate_condition',
        'evaluate_presence',
        'evaluate_ai_prompt',
    ];
    static yamlMablScriptNames = ['ConditionAction'];
    hasOptionsAtInstantiation = false;
    conditionDescriptor;
    constructor(name, args) {
        super(name, name, args);
        this.conditionDescriptor = this.getConditionDescriptor();
    }
    getConditionDescriptor() {
        switch (this.name) {
            case 'evaluate_condition':
                return this.getEvaluateCondition();
            case 'evaluate_presence':
                return this.getEvaluatePresence();
            case 'evaluate_ai_prompt':
                return this.getEvaluateAIPrompt();
            default:
                throw new Error(`Unexpected condition name: ${this.name}`);
        }
    }
    getEvaluateCondition() {
        const conditionActionArgs = this.getActionArgs();
        let conditionAttribute;
        let conditionValueAttribute;
        let options;
        if (conditionActionArgs) {
            [conditionAttribute, conditionValueAttribute, options] =
                this.getActionArgs();
        }
        if (conditionValueAttribute === undefined) {
            throw new Error('Invalid equality condition. Missing value to compare.');
        }
        if (conditionAttribute === undefined) {
            throw new Error('Invalid equality condition. Missing comparison type.');
        }
        if (!(0, ConditionDescriptor_1.isComparisonType)(conditionAttribute)) {
            throw new Error(`Invalid equality condition. Invalid comparison type: ${conditionAttribute}`);
        }
        if (options &&
            Object.values(options).some((value) => value !== undefined)) {
            this.hasOptionsAtInstantiation = true;
        }
        return {
            conditionType: ConditionDescriptor_1.ConditionType.Comparison,
            comparisonType: conditionAttribute,
            comparatorValue: (0, MablAction_1.parseArgument)(conditionValueAttribute),
            caseInsensitive: options?.caseInsensitive,
        };
    }
    getEvaluatePresence() {
        const conditionActionArgs = this.getActionArgs();
        let conditionAttribute;
        if (conditionActionArgs) {
            [conditionAttribute] = this.getActionArgs();
        }
        if (!conditionAttribute) {
            throw new Error('Invalid evaluate presence condition. Missing evaluation_type.');
        }
        return {
            conditionType: ConditionDescriptor_1.ConditionType.Presence,
            presenceType: ConditionDescriptor_1.PresenceEvaluationType.fromString(conditionAttribute),
        };
    }
    getEvaluateAIPrompt() {
        const conditionActionArgs = this.getActionArgs();
        let userPrompt;
        let options;
        if (conditionActionArgs) {
            [userPrompt, options] = this.getActionArgs();
        }
        if (userPrompt === undefined) {
            throw new Error('Invalid ai prompt condition. Missing user prompt');
        }
        return {
            conditionType: ConditionDescriptor_1.ConditionType.AIPrompt,
            userPrompt,
            metaPrompt: typeof options === 'string' ? options : options?.metaPrompt,
            criteria: options?.criteria,
        };
    }
    generateEvaluateConditionMablscript() {
        const comparatorValue = (0, ConditionDescriptor_1.getOptionalComparatorValue)(this.conditionDescriptor);
        let value = '';
        if (comparatorValue !== undefined) {
            if (!(0, MablAction_1.argumentIsMablVariable)(comparatorValue)) {
                value = `, "${(0, mablscriptFind_1.escapeMablscriptString)(comparatorValue)}"`;
            }
            else {
                value = `, ${this.subVariable(comparatorValue)}`;
            }
        }
        const conditionOptionsString = this.hasOptionsAtInstantiation
            ? `, ${(0, MablAction_1.convertObjectToMablscriptArgs)((0, ConditionDescriptor_1.getOptionalConditionOptions)(this.conditionDescriptor) ?? {})}`
            : '';
        return `${this.name}("${(0, ConditionDescriptor_1.getAssertionConditionFieldFromDescriptor)(this.conditionDescriptor)}"${value}${conditionOptionsString})`;
    }
    toMablscript() {
        return `${this.generateEvaluateConditionMablscript()}`;
    }
    getInputVariables() {
        const fields = [];
        fields.push((0, ConditionDescriptor_1.getOptionalComparatorValue)(this.conditionDescriptor) ?? '');
        if ((0, ConditionDescriptor_1.isAIPromptCondition)(this.conditionDescriptor)) {
            if (this.conditionDescriptor.criteria) {
                fields.push(...this.conditionDescriptor.criteria);
            }
            fields.push(this.conditionDescriptor.userPrompt);
        }
        return MablAction_1.MablAction.findUniqueVariableReferencesInValue(fields);
    }
}
exports.ConditionAction = ConditionAction;
