"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CONTENT_JS_PATH_EXPORT = exports.expect = exports.testWithoutBridge = exports.test = void 0;
const test_1 = require("@playwright/test");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const CONTENT_JS_PATH = path_1.default.resolve(__dirname, '../../../resources/contentJs/content.js');
exports.test = test_1.test.extend({
    contentJs: async ({ page }, use) => {
        const receivedMessages = [];
        await page.addInitScript(() => {
            window.mablFrameInfo = {
                tabId: 'initial',
                frameId: '0',
                position: { x: 0, y: 0 },
                pageSize: { width: 0, height: 0 },
            };
        });
        await page.goto('about:blank');
        await page.exposeFunction('mablSendMessageToMain', (message) => {
            receivedMessages.push(message);
            return { success: true, messageId: receivedMessages.length };
        });
        const contentJsCode = fs_1.default.readFileSync(CONTENT_JS_PATH, 'utf-8');
        await page.evaluate(contentJsCode);
        await page.evaluate(() => {
            window.__mabl.messageListener({ action: 'start_listeners' });
        });
        const initialMessageCount = receivedMessages.length;
        const setContentAndInject = async (html) => {
            await page.setContent(html);
            await page.evaluate(() => {
                window.__mablInit = false;
                window.__mablListenersRegistered = false;
            });
            await page.evaluate(contentJsCode);
            await page.evaluate(() => {
                window.__mabl.messageListener({ action: 'start_listeners' });
            });
        };
        await use({
            page,
            receivedMessages,
            initialMessageCount,
            getNewMessages: () => receivedMessages.slice(initialMessageCount),
            setContentAndInject,
        });
    },
});
exports.testWithoutBridge = test_1.test.extend({
    contentJsNoBridge: async ({ page }, use) => {
        await page.addInitScript(() => {
            window.mablFrameInfo = {
                tabId: 'initial',
                frameId: '0',
                position: { x: 0, y: 0 },
                pageSize: { width: 0, height: 0 },
            };
        });
        await page.goto('about:blank');
        const contentJsCode = fs_1.default.readFileSync(CONTENT_JS_PATH, 'utf-8');
        await page.evaluate(contentJsCode);
        await use({ page });
    },
});
var test_2 = require("@playwright/test");
Object.defineProperty(exports, "expect", { enumerable: true, get: function () { return test_2.expect; } });
exports.CONTENT_JS_PATH_EXPORT = CONTENT_JS_PATH;
