"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RuntimeCtx = void 0;
const jsonpointer_1 = __importDefault(require("jsonpointer"));
const moment_1 = __importDefault(require("moment"));
const node_fetch_1 = __importDefault(require("node-fetch"));
const element_js_1 = require("./element.js");
const index_js_1 = require("./services/index.js");
const util = __importStar(require("./util/index.js"));
class RuntimeCtx {
    constructor(action) {
        this.$introspectionEnabled = false;
        this.$introspectionResults = [];
        this.$introspectionSpotlight = null;
        this.$introspectionStats = {
            pipesExecuted: 0,
            pipelinesExecuted: 0,
        };
        this.$stack = [];
        this.$screenshotTaken = false;
        this.action = action;
        this.context = action.$context;
        this.script = action.$script;
        Object.assign(this, util, {
            jsonPointer: jsonpointer_1.default,
            moment: moment_1.default,
            fetch: node_fetch_1.default,
            Element: element_js_1.Element,
        });
    }
    get browser() { return this.action.$browser; }
    get $browser() { return this.action.$browser; }
    get page() { return this.$browser.page; }
    get $page() { return this.$browser.page; }
    get $engine() { return this.script.$engine; }
    get $logger() { return this.action.$logger; }
    get $proxy() { return this.$engine.get(index_js_1.ProxyService); }
    get $api() { return this.$engine.get(index_js_1.ApiRequest); }
    get $fetch() { return this.$engine.get(index_js_1.FetchService); }
    async createDocument(value = {}) {
        const document = await this.page.document();
        return new element_js_1.Element(document, value);
    }
    *allLocals() {
        for (let i = this.$stack.length - 1; i >= 0; i--) {
            const frame = this.$stack[i];
            yield* frame.locals;
        }
    }
    getLocal(key) {
        for (let i = this.$stack.length - 1; i >= 0; i--) {
            const frame = this.$stack[i];
            const els = frame.locals.get(key);
            if (els) {
                return els;
            }
        }
        throw util.playbackError(`Local "${key}" not found`);
    }
    setLocal(key, elements) {
        const frame = this.$stack[this.$stack.length - 1];
        if (!frame) {
            return;
        }
        frame.locals.set(key, elements);
    }
    async evalDefinition(definitionId, inputSet) {
        const def = this.script.requireDefinition(definitionId);
        const results = [];
        for (const el of inputSet) {
            const newEls = await def.pipeline.selectAll([el], this);
            results.push(...newEls);
        }
        return results;
    }
    async takeDebugScreenshot(label) {
        if (this.$screenshotTaken) {
            return;
        }
        const reporter = this.$engine.get(index_js_1.ReporterService);
        try {
            await reporter.sendScreenshot('debug', { label });
        }
        catch (error) {
            this.$logger.error('Debug screenshot failed', { error });
        }
        finally {
            this.$screenshotTaken = true;
        }
    }
}
exports.RuntimeCtx = RuntimeCtx;
//# sourceMappingURL=ctx.js.map