"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionBuilderUtils = void 0;
const FindAction_1 = require("./FindAction");
const GetUrlAction_1 = require("./GetUrlAction");
const GetVariableValue_1 = require("./GetVariableValue");
const GetViewportAction_1 = require("./GetViewportAction");
const ExtractAction_1 = require("./ExtractAction");
const CountAction_1 = require("./CountAction");
const ConditionAction_1 = require("./ConditionAction");
const MobileFindAction_1 = require("../mobile/steps/actions/MobileFindAction");
const mablscriptFind_1 = require("../mablscriptFind");
const GetCurrentLocationDescriptor_1 = require("../types/GetCurrentLocationDescriptor");
const GetVariableDescriptor_1 = require("../types/GetVariableDescriptor");
const GetViewportDescriptor_1 = require("../types/GetViewportDescriptor");
const ExtractDescriptor_1 = require("../types/ExtractDescriptor");
function hasUserPrompt(condition) {
    return 'userPrompt' in condition;
}
function hasPresent(condition) {
    return 'present' in condition;
}
function hasNotPresent(condition) {
    return 'not_present' in condition;
}
function hasConditionType(condition) {
    return 'conditionType' in condition;
}
class ActionBuilderUtils {
    static buildPrimaryActionFromTarget(target) {
        if (target?.kind === GetCurrentLocationDescriptor_1.GET_CURRENT_LOCATION_KIND) {
            return new GetUrlAction_1.GetUrlAction(GetUrlAction_1.GetUrlAction.mablScriptStepNames[0], []);
        }
        if ((0, GetVariableDescriptor_1.isGetVariableDescriptor)(target)) {
            return GetVariableValue_1.GetVariableValue.buildAction(target.variable);
        }
        if ((0, GetViewportDescriptor_1.isGetViewportDescriptor)(target)) {
            return new GetViewportAction_1.GetViewportAction();
        }
        if ((0, mablscriptFind_1.isNormalizedFindEmailDescriptor)(target?.findTarget) ||
            (0, mablscriptFind_1.isCookieSelector)(target?.findTarget) ||
            (0, mablscriptFind_1.isTabSelector)(target?.findTarget)) {
            const { findTarget, findType } = target;
            return new FindAction_1.FindAction(findType, [findTarget]);
        }
        if ((0, mablscriptFind_1.isMobileFindDescriptor)(target)) {
            return MobileFindAction_1.MobileFindAction.fromDescriptor(target);
        }
        if ((0, mablscriptFind_1.isFindElementDescriptor)(target)) {
            const { findOptions, findTarget, findType, foundElementSelectors } = target;
            if ((0, mablscriptFind_1.isFindCustomDescriptor)(target)) {
                return new FindAction_1.FindAction(findType, [
                    findTarget,
                    foundElementSelectors,
                    findOptions,
                ]);
            }
            return new FindAction_1.FindAction(findType, [findTarget, findOptions]);
        }
        throw new Error(`Error parsing target with incompatible step arguments: ${JSON.stringify(target)}`);
    }
    static buildExtractActionFromDescriptor(extract) {
        if (!extract) {
            return undefined;
        }
        let attributeName;
        switch (extract.extractType) {
            case ExtractDescriptor_1.ExtractType.EXTRACT_ATTRIBUTE:
            case ExtractDescriptor_1.ExtractType.EXTRACT_CSS_PROPERTIES:
            case ExtractDescriptor_1.ExtractType.EXTRACT_EMAIL_ATTRIBUTE:
                attributeName = extract.attributeName;
                break;
            case ExtractDescriptor_1.ExtractType.EXTRACT_INNER_TEXT:
                attributeName = 'innerText';
                break;
            case ExtractDescriptor_1.ExtractType.EXTRACT_PROMPT:
                attributeName = 'prompt';
                break;
            default:
                throw new Error(`Unknown extractType: ${extract.extractType}`);
        }
        const extractArgs = [attributeName];
        if (extract.extractType === ExtractDescriptor_1.ExtractType.EXTRACT_CSS_PROPERTIES) {
            extractArgs.push({ extraction_type: 'css' });
        }
        return new ExtractAction_1.ExtractAction(ExtractAction_1.ExtractAction.mablScriptTypeFromYamlType(extract.extractType), extractArgs);
    }
    static buildCountActionFromDescriptor(count) {
        if (!count?.count) {
            return undefined;
        }
        return new CountAction_1.CountAction('count', ['count']);
    }
    static buildConditionActionFromDescriptor(condition, conditionOptions) {
        if (!condition) {
            throw new Error('Condition descriptor is required');
        }
        if (hasUserPrompt(condition)) {
            const conditionArgs = [condition.userPrompt];
            if (condition.metaPrompt || condition.criteria) {
                conditionArgs.push({
                    metaPrompt: condition.metaPrompt,
                    criteria: condition.criteria,
                });
            }
            return new ConditionAction_1.ConditionAction('evaluate_ai_prompt', conditionArgs);
        }
        if (hasPresent(condition) || hasNotPresent(condition)) {
            const presenceType = hasPresent(condition) ? 'present' : 'not_present';
            return new ConditionAction_1.ConditionAction('evaluate_presence', [presenceType]);
        }
        if (hasConditionType(condition) && condition.conditionType === 'presence') {
            const presenceType = condition.presenceType || 'present';
            return new ConditionAction_1.ConditionAction('evaluate_presence', [presenceType]);
        }
        const comparisonCondition = condition;
        if (!comparisonCondition.comparisonType) {
            throw new Error(`Unable to determine comparison type from descriptor: ${JSON.stringify(condition)}`);
        }
        return new ConditionAction_1.ConditionAction('evaluate_condition', [
            comparisonCondition.comparisonType,
            comparisonCondition.comparatorValue,
            conditionOptions,
        ]);
    }
}
exports.ActionBuilderUtils = ActionBuilderUtils;
