"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MobileStepExecutor = void 0;
const execution_1 = require("@mablhq-internal/runtime/execution");
const execution_2 = require("@mablhq-internal/runtime/execution");
const messaging_1 = require("@mablhq-internal/runtime/core/messaging/messaging");
const MobileTestExecutionContext_1 = require("./contexts/MobileTestExecutionContext");
const StepGroupStep_1 = require("@mablhq/test-model/steps/StepGroupStep");
const EvaluateFlowStep_1 = require("@mablhq/test-model/steps/EvaluateFlowStep");
const fileUploadUtil_1 = require("@mablhq-internal/runtime/util/fileUploadUtil");
const execution_3 = require("@mablhq-internal/runtime/execution");
class MobileStepExecutor {
    executionContext;
    stepMapper = new execution_2.MobileExecutorStore();
    dryRun = false;
    constructor(mobileEnvConfig, testContext, apiClient, mobileEnv) {
        this.executionContext = new MobileTestExecutionContext_1.MobileTestExecutionContext(mobileEnvConfig, apiClient, testContext, messaging_1.mablEventEmitter, undefined, this.stepMapper, mobileEnv);
    }
    getExecutionContext() {
        return this.executionContext;
    }
    executeMablStep(step, maxTimeout) {
        if (this.isContainerStep(step)) {
            return this.executionContext.runStep(step, this.noOpExecutor, maxTimeout);
        }
        const executor = this.stepMapper.getStepExecutor(step);
        return this.executionContext.runStep(step, executor, maxTimeout);
    }
    isContainerStep(step) {
        return (step.name === StepGroupStep_1.StepGroupStep.stepName ||
            step.name === EvaluateFlowStep_1.EVALUATE_FLOW_STEP_NAME);
    }
    noOpExecutor = {
        run: () => Promise.resolve({ status: execution_1.StepStatus.Passed }),
    };
    setShortCircuit() {
        this.executionContext.setShortCircuitFindExecution();
        return Promise.resolve();
    }
    clearShortCircuit() {
        this.executionContext.clearShortCircuitFindExecution();
        return Promise.resolve(true);
    }
    setStepRetryShortCircuit() {
        this.executionContext.setStepRetryShortCircuit();
    }
    clearStepRetryShortCircuit() {
        this.executionContext.clearStepRetryShortCircuit();
    }
    setStepsAreActive(status) {
        this.executionContext.conditionalContext.stepsAreActive = status;
    }
    setSnippet(snippet) {
        this.executionContext.testContext.setSnippet(snippet);
    }
    getSessionId() {
        return this.executionContext.sessionId;
    }
    getVariables() {
        return this.executionContext.variables;
    }
    takeScreenshot(screenshotBoundingBox) {
        return (0, execution_3.takeScreenshot)(this.executionContext, screenshotBoundingBox);
    }
    async downloadFileUploadsAndSetReference(fileUploadReferences) {
        const testContext = this.getExecutionContext().testContext;
        return (0, fileUploadUtil_1.downloadFileUploadsAndSetReferenceInContext)(this.executionContext.mablApiClient, testContext, fileUploadReferences);
    }
    async captureScreenshot() {
        return this.executionContext.mobileTestEnvironment
            .getDriver()
            .takeScreenshot();
    }
}
exports.MobileStepExecutor = MobileStepExecutor;
