"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateVariableMobileExecutor = void 0;
const messaging_1 = require("@mablhq-internal/runtime/core/messaging/messaging");
const CreateVariableStepDescriptor_1 = require("@mablhq/test-model/types/CreateVariableStepDescriptor");
const logUtils_1 = require("@mablhq-internal/runtime/util/logUtils");
const execution_1 = require("@mablhq-internal/runtime/execution");
const execution_2 = require("@mablhq-internal/runtime/execution");
const execution_3 = require("@mablhq-internal/runtime/execution");
const VariableUtils_1 = require("@mablhq-internal/runtime/core/execution/VariableUtils");
const FindMobileElement_1 = require("../actions/FindMobileElement");
class CreateVariableMobileExecutor {
    async run(step, executionContext) {
        const { variables } = executionContext;
        let value;
        let boundingBox;
        switch (step.generator.type) {
            case CreateVariableStepDescriptor_1.VariableGenerator.PATTERN:
                await executionContext.observeDeferredFunction();
                value = variables.resolve(step.generator.pattern);
                if (value === undefined) {
                    return {
                        status: execution_2.StepStatus.Failed,
                        logInfo: `Cannot generate string from pattern "${step.generator.pattern}"`,
                    };
                }
                break;
            case CreateVariableStepDescriptor_1.VariableGenerator.ATTRIBUTE:
                const findResult = await FindMobileElement_1.FindMobileElement.run(step, executionContext);
                boundingBox = findResult.boundingBox;
                const { mobileElement } = findResult;
                await executionContext.observeDeferredFunction();
                if (!mobileElement) {
                    return {
                        status: execution_2.StepStatus.Failed,
                        logInfo: 'Element not found',
                    };
                }
                if ((0, FindMobileElement_1.isWebViewElementHandle)(findResult)) {
                    throw new Error('Not Implemented -- Creating variables from web view elements is not yet implemented.');
                }
                const { attribute } = step.generator;
                value = await mobileElement.getAttribute(attribute);
                if (value === undefined) {
                    return {
                        status: execution_2.StepStatus.Failed,
                        logInfo: `Cannot extract element property "${step.generator.attribute}"`,
                        boundingBox,
                    };
                }
                break;
            case CreateVariableStepDescriptor_1.VariableGenerator.EMAIL:
                await executionContext.observeDeferredFunction();
                value = await execution_3.CreateVariableExecutor.generateEmailAddress(executionContext);
                break;
            case CreateVariableStepDescriptor_1.VariableGenerator.ELEMENT_COUNT:
                const multiFindResult = await FindMobileElement_1.FindMobileElement.runMultipleResultFind(step.find, executionContext);
                await executionContext.observeDeferredFunction();
                value = multiFindResult?.elements?.length.toString();
                break;
            case CreateVariableStepDescriptor_1.VariableGenerator.COOKIE_ATTRIBUTE:
                throw new Error('Cannot create a variable with a cookie for mobile execution');
            case CreateVariableStepDescriptor_1.VariableGenerator.JAVASCRIPT:
            default:
                await executionContext.observeDeferredFunction();
                try {
                    const snippetResult = await execution_1.EvaluateJavaScript.run(step.generator.javaScript, executionContext);
                    value = snippetResult.value;
                }
                catch (error) {
                    return {
                        status: execution_2.StepStatus.Failed,
                        logInfo: error.toString(),
                    };
                }
                break;
        }
        const name = (0, VariableUtils_1.addUserNamespace)(step.name);
        variables.setTestVariable(name, value);
        const stepId = executionContext.getStepId();
        const stepNumber = executionContext.currentStepDisplayNumber;
        const stepRunId = executionContext.executionIndexTracker.stepRunId;
        if (stepId || stepNumber) {
            variables.recordStepSource(name, { stepId, stepNumber, stepRunId });
        }
        executionContext.log.info(`Assigned the value "${(0, logUtils_1.valueToString)(value)}" to the variable "${step.name}"`, {
            executionPhase: messaging_1.ExecutionPhase.DURING_ACTION,
        });
        return { status: execution_2.StepStatus.Passed, boundingBox };
    }
}
exports.CreateVariableMobileExecutor = CreateVariableMobileExecutor;
