"use strict";
/**
 * Copyright: (c) Myia SAS 2026.
 * This file and its contents are licensed under the AGPLv3 License.
 * Please see the LICENSE file at the root of this repository
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.HIGHLIGHTED_LOCATOR_ACTIONS = void 0;
exports.setActiveCaptureSession = setActiveCaptureSession;
exports.getActiveCaptureSession = getActiveCaptureSession;
exports.ensureLocatorPrototypePatched = ensureLocatorPrototypePatched;
const logger_1 = require("../../util/logger");
// Locator action methods that will be highlighted and screenshotted.
// User-facing actions only — not queries, not waits, not assertions.
// If Playwright adds new action methods, append them here.
//
// Deliberately excluded: `scrollIntoViewIfNeeded`. The capture
// pipeline calls `target.scrollIntoViewIfNeeded()` itself before
// every action screenshot (so off-viewport targets aren't framing
// empty space); patching it here would cause infinite recursion
// when our own pre-screenshot scroll re-entered the patched method.
// The standalone scroll-action screenshot is also redundant given
// every other action now scrolls-then-screenshots anyway.
exports.HIGHLIGHTED_LOCATOR_ACTIONS = [
    'click',
    'dblclick',
    'tap',
    'fill',
    'clear',
    'hover',
    'press',
    'pressSequentially',
    'type',
    'check',
    'uncheck',
    'setChecked',
    'focus',
    'blur',
    'selectOption',
    'selectText',
    'setInputFiles',
    'dragTo',
];
const PATCHED = Symbol.for('heal-playwright-tracer.locator-patched');
// Active-session registry. The fixture sets this at test start and
// clears it at teardown; the patched methods read it on every call.
let activeSession = null;
function setActiveCaptureSession(session) {
    activeSession = session;
}
function getActiveCaptureSession() {
    return activeSession;
}
// Idempotent proto patch. Call once per process with any Page-like
// object that produces a Locator via `locator('body')` — we grab the
// prototype from it and overwrite every action method.
function ensureLocatorPrototypePatched(samplePage) {
    const proto = Object.getPrototypeOf(samplePage.locator('body'));
    if (!proto || proto[PATCHED])
        return;
    proto[PATCHED] = true;
    for (const name of exports.HIGHLIGHTED_LOCATOR_ACTIONS) {
        const orig = proto[name];
        if (typeof orig !== 'function')
            continue;
        proto[name] = async function patched(...args) {
            const self = this;
            const pg = typeof self.page === 'function' ? self.page() : null;
            const session = activeSession;
            const cleanup = pg && session
                ? await session.captureWithHighlight(pg, self, name, { scrollBeforeCapture: true })
                : null;
            try {
                return await orig.apply(self, args);
            }
            finally {
                if (cleanup) {
                    try {
                        await cleanup();
                    }
                    catch (err) {
                        // Cleanup is already wrapped in withTimeout inside the
                        // session; a rejection here means the page closed,
                        // navigated, or the renderer wedged past the cap.
                        logger_1.log.warn(`overlay cleanup rejected after locator.${name}`, err);
                    }
                }
            }
        };
    }
}
