"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OBSERVABLE_ACTIONS = exports.NoOpDispatcher = exports.ObserverPhase = exports.ObservationType = exports.DEFERRED_OBSERVABLE_ACTIONS = exports.OBSERVATION_MAX_WAIT_TIME_MS = exports.MAX_RETRIES = exports.MAX_TIMEOUT_MS = exports.MIN_TIMEOUT_MS = exports.STEPLESS_ACTION_STEP_SOURCE_INDEX = void 0;
exports.setObservationsArtifactsPath = setObservationsArtifactsPath;
exports.getObservationsArtifactsPath = getObservationsArtifactsPath;
exports.isObservable = isObservable;
exports.isDeferredObservableAction = isDeferredObservableAction;
exports.createCaptureApplicationStateExecutionIndex = createCaptureApplicationStateExecutionIndex;
exports.createRuntimeRecoverySummaryExecutionIndex = createRuntimeRecoverySummaryExecutionIndex;
exports.createTempFile = createTempFile;
const promises_1 = require("fs/promises");
const uuid_1 = require("uuid");
const syntheticActionCodes_1 = require("./syntheticActionCodes");
exports.STEPLESS_ACTION_STEP_SOURCE_INDEX = -1;
exports.MIN_TIMEOUT_MS = 100;
exports.MAX_TIMEOUT_MS = 200;
exports.MAX_RETRIES = 15;
exports.OBSERVATION_MAX_WAIT_TIME_MS = 10_000;
exports.DEFERRED_OBSERVABLE_ACTIONS = new Set(['root_page', 'sub_page']);
let artifactsPath;
function setObservationsArtifactsPath(path) {
    artifactsPath = path;
}
function getObservationsArtifactsPath() {
    return artifactsPath;
}
var ObservationType;
(function (ObservationType) {
    ObservationType["ACCESSIBILITY_CHECK"] = "accessibility_check";
    ObservationType["API_REQUEST_RESPONSE"] = "api_request_response";
    ObservationType["BROKEN_LINKS"] = "broken_links";
    ObservationType["BROWSER_LOG"] = "browser_log";
    ObservationType["CHROME_TRACE"] = "chrome_trace";
    ObservationType["DOM_SNAPSHOT"] = "dom_snapshot";
    ObservationType["FIND_RESULT"] = "find_result";
    ObservationType["FRAMEBUFFER"] = "framebuffer";
    ObservationType["FULL_PAGE_SNAPSHOT"] = "full_page_snapshot";
    ObservationType["FULL_SCREENSHOT"] = "full_screenshot";
    ObservationType["HAR"] = "har";
    ObservationType["MOBILE_PAGE_SOURCE"] = "mobile_page_source";
    ObservationType["SCREENSHOT"] = "screenshot";
    ObservationType["SYSTEM_LOGS"] = "system_logs";
    ObservationType["VARIABLES"] = "variables";
})(ObservationType || (exports.ObservationType = ObservationType = {}));
var ObserverPhase;
(function (ObserverPhase) {
    ObserverPhase[ObserverPhase["AFTER"] = 0] = "AFTER";
    ObserverPhase[ObserverPhase["DEFERRED"] = 1] = "DEFERRED";
    ObserverPhase[ObserverPhase["FLOW"] = 2] = "FLOW";
    ObserverPhase[ObserverPhase["TEST"] = 3] = "TEST";
})(ObserverPhase || (exports.ObserverPhase = ObserverPhase = {}));
exports.NoOpDispatcher = {
    onObservationComplete: () => { },
    onObservationError: () => { },
};
function isObservable(executionIndex) {
    return (exports.OBSERVABLE_ACTIONS.has(executionIndex.actionName?.toLowerCase() ?? '') ||
        exports.OBSERVABLE_ACTIONS.has(executionIndex.actionCode?.toLowerCase() ?? ''));
}
function isDeferredObservableAction(actionName) {
    return actionName
        ? exports.DEFERRED_OBSERVABLE_ACTIONS.has(actionName.toLowerCase())
        : false;
}
function createCaptureApplicationStateExecutionIndex(executionContext) {
    return {
        ...executionContext.executionIndexTracker.getExecutionIndex(),
        stepSourceIndex: exports.STEPLESS_ACTION_STEP_SOURCE_INDEX,
        stepRunIndex: 0,
        stepRunId: (0, uuid_1.v4)(),
        stepName: 'CaptureApplicationState',
        actionSourceIndex: 0,
        actionName: syntheticActionCodes_1.CAPTURE_APPLICATION_STATE_ACTION_CODE,
    };
}
function createRuntimeRecoverySummaryExecutionIndex(executionContext) {
    return {
        ...executionContext.executionIndexTracker.getExecutionIndex(),
        stepSourceIndex: exports.STEPLESS_ACTION_STEP_SOURCE_INDEX,
        stepRunIndex: 0,
        stepRunId: (0, uuid_1.v4)(),
        stepName: 'RuntimeRecoverySummary',
        actionSourceIndex: 1,
        actionName: syntheticActionCodes_1.RUNTIME_RECOVERY_SUMMARY_ACTION_CODE,
    };
}
async function createTempFile(filename) {
    const base = artifactsPath ?? '/tmp';
    const tmpDir = await (0, promises_1.mkdtemp)(`${base}/`);
    return `${tmpDir}/${filename}`;
}
exports.OBSERVABLE_ACTIONS = new Set([
    'accessibility_check',
    'as',
    'assert',
    'autologin',
    'await_download',
    'await_pdf',
    'await_tab',
    syntheticActionCodes_1.CAPTURE_APPLICATION_STATE_ACTION_CODE,
    syntheticActionCodes_1.RUNTIME_RECOVERY_SUMMARY_ACTION_CODE,
    'click',
    'click_and_hold',
    'conditional_else_if',
    'conditional_if',
    'double_click',
    'enter_auth_code',
    'enter_text',
    'evaluate_js',
    'hover',
    'navigate',
    'navigate_back',
    'navigate_home',
    'scroll',
    'set_orientation',
    'release',
    'right_click',
    'root_page',
    'select',
    'send_http_request',
    'send_keys',
    'set_files',
    'sub_page',
    'tap',
    'validate_page',
    'verify_url',
    'visit_url',
    'wait',
]);
