"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssertStepOldConverter = exports.oldToV2StepNameMap = void 0;
const AssertStep_1 = require("./AssertStep");
const ConditionAction_1 = require("../actions/ConditionAction");
const ConditionDescriptor_1 = require("../types/ConditionDescriptor");
exports.oldToV2StepNameMap = {
    assert_equals: 'AssertEquals',
    assert_present: 'AssertPresent',
    assert_not_present: 'AssertNotPresent',
    assert_does_not_equal: 'AssertDoesNotEqual',
    assert_contains: 'AssertContains',
    assert_does_not_contain: 'AssertDoesNotContain',
    assert_starts_with: 'AssertStartsWith',
    assert_starts_without: 'AssertStartsWithout',
    assert_ends_with: 'AssertEndsWith',
    assert_ends_without: 'AssertEndsWithout',
    assert_greater_than: 'AssertGreaterThan',
    assert_less_than: 'AssertLessThan',
    assert_greater_than_or_equals: 'AssertGreaterThanOrEquals',
    assert_less_than_or_equals: 'AssertLessThanOrEquals',
    assert_matches_regex: 'AssertMatchesRegex',
    assert_ai_prompt: 'AssertAIPrompt',
};
class AssertStepOldConverter {
    static convert(name, args, actions) {
        const v2StepName = exports.oldToV2StepNameMap[name];
        if (!v2StepName) {
            throw new Error(`Unknown old assertion type: ${name}`);
        }
        const newActions = [];
        newActions.push(actions[0]);
        if (actions[1]) {
            newActions.push(actions[1]);
        }
        let assertionValue;
        let assertionArguments = {};
        if (args.length > 0) {
            const firstArg = args[0];
            if (typeof firstArg === 'object' && firstArg !== undefined) {
                if ('onFailure' in firstArg || 'observationScope' in firstArg) {
                    assertionArguments = firstArg;
                    if (name === 'assert_present' || name === 'assert_not_present') {
                        assertionValue = undefined;
                    }
                    else {
                        assertionValue = args[1];
                    }
                }
                else {
                    assertionValue = firstArg;
                    if (args[1] && typeof args[1] === 'object') {
                        assertionArguments = args[1];
                    }
                }
            }
            else {
                assertionValue = firstArg;
                if (args[1] && typeof args[1] === 'object') {
                    assertionArguments = args[1];
                }
            }
        }
        let conditionAction;
        if (name === 'assert_present') {
            conditionAction = new ConditionAction_1.ConditionAction('evaluate_presence', ['present']);
        }
        else if (name === 'assert_not_present') {
            conditionAction = new ConditionAction_1.ConditionAction('evaluate_presence', [
                'not_present',
            ]);
        }
        else {
            const conditionType = ConditionDescriptor_1.AssertionStepNameToField[v2StepName];
            if (!conditionType) {
                throw new Error(`Unknown assertion type: ${v2StepName}`);
            }
            conditionAction = new ConditionAction_1.ConditionAction('evaluate_condition', [
                conditionType,
                assertionValue ?? '',
            ]);
        }
        newActions.push(conditionAction);
        const { descriptor, stepActions } = (0, AssertStep_1.buildAssertStepDescriptorFromLegacyActions)(v2StepName, [assertionArguments], newActions);
        const result = new AssertStep_1.AssertStep(descriptor, v2StepName, stepActions);
        return result;
    }
}
exports.AssertStepOldConverter = AssertStepOldConverter;
