"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnterAuthCodeExecutor = void 0;
const execution_1 = require("@mablhq-internal/runtime/execution");
const execution_2 = require("@mablhq-internal/runtime/execution");
const EnterTextExecutor_1 = require("./EnterTextExecutor");
class EnterAuthCodeExecutor {
    run(step, executionContext) {
        return (0, execution_2.runActionWithElementReadyRetries)(() => this.enterAuthCode(step, executionContext), executionContext);
    }
    async enterAuthCode(step, executionContext) {
        const credentials = executionContext.testContext.credentials;
        if (!credentials) {
            return {
                status: execution_1.StepStatus.Failed,
                logInfo: 'Credentials required to enter an authentication code, but none supplied.',
            };
        }
        executionContext.log.info(`Fetching current value of authentication code for credential ${credentials.id}`);
        await executionContext.observeDeferredFunction();
        const credentialWithAuthCodes = await executionContext.mablApiClient.getCredential(credentials.id, true, 1);
        if (!credentialWithAuthCodes.totp_pins?.length) {
            return {
                status: execution_1.StepStatus.Failed,
                logInfo: 'No authentication code found for the credential',
            };
        }
        executionContext.log.info(`Successfully retrieved current value of authentication code`);
        const authCodeValue = credentialWithAuthCodes.totp_pins[0].pin;
        const enterTextStepDescriptor = {
            find: step.find,
            text: authCodeValue,
        };
        const enterTextExecutor = new EnterTextExecutor_1.EnterTextExecutor();
        const result = await enterTextExecutor.enterText(enterTextStepDescriptor, executionContext);
        return { status: execution_1.StepStatus.Passed, boundingBox: result.boundingBox };
    }
}
exports.EnterAuthCodeExecutor = EnterAuthCodeExecutor;
