"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.patchExpect = void 0;
const constants_1 = require("../../../constants");
function patchExpect(LocatorClass) {
    //@ts-ignore _expect returnsPromise<{ matches: boolean, received?: any, log?: string[], timedOut?: boolean }>
    const originalExpect = LocatorClass.prototype._expect;
    // @ts-ignore _expect is a private method in playwright
    // https://github.com/microsoft/playwright/blob/69287f26bc514b740eac40160503d6fac8185d37/packages/playwright-core/src/client/locator.ts#L352
    LocatorClass.prototype._expect = async function (expression, options) {
        const result = await originalExpect.apply(this, [expression, options]);
        if (result.matches) {
            try {
                await this.evaluate((node) => {
                    if (node.scrollIntoViewIfNeeded) {
                        node.scrollIntoViewIfNeeded();
                    }
                    else {
                        node.scrollIntoView();
                    }
                    node.classList.add("empirical-element-grab-highlight");
                    setTimeout(() => {
                        if (node && node.isConnected && node.classList) {
                            node.classList.remove("empirical-element-grab-highlight");
                        }
                    }, 2000);
                }, undefined, { timeout: constants_1.DEFAULT_SCRIPT_EXECUTION_TIMEOUT });
            }
            catch (e) {
                console.warn("Failed to add highlight for locator method: _expect");
            }
        }
        return result;
    };
}
exports.patchExpect = patchExpect;
