"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const fixtures_1 = require("./fixtures");
fixtures_1.test.describe('contentJs initialization', () => {
    (0, fixtures_1.test)('content.js file should exist after build', async () => {
        (0, fixtures_1.expect)(fs_1.default.existsSync(fixtures_1.CONTENT_JS_PATH_EXPORT)).toBe(true);
    });
    (0, fixtures_1.test)('should initialize window.__mabl when injected', async ({ contentJs }) => {
        const hasMablGlobal = await contentJs.page.evaluate(() => {
            return typeof window.__mabl !== 'undefined';
        });
        (0, fixtures_1.expect)(hasMablGlobal).toBe(true);
    });
    (0, fixtures_1.test)('should set window.__mabltron flag', async ({ contentJs }) => {
        const isMabltron = await contentJs.page.evaluate(() => {
            return window.__mabltron === true;
        });
        (0, fixtures_1.expect)(isMabltron).toBe(true);
    });
    (0, fixtures_1.test)('should expose PlaywrightBridge on window.__mabl', async ({ contentJs, }) => {
        const hasBridge = await contentJs.page.evaluate(() => {
            return (typeof window.__mabl?.bridge !== 'undefined' &&
                typeof window.__mabl?.bridge?.sendToMain === 'function');
        });
        (0, fixtures_1.expect)(hasBridge).toBe(true);
    });
    (0, fixtures_1.test)('should expose messageListener on window.__mabl', async ({ contentJs, }) => {
        const hasMessageListener = await contentJs.page.evaluate(() => {
            return typeof window.__mabl?.messageListener === 'function';
        });
        (0, fixtures_1.expect)(hasMessageListener).toBe(true);
    });
    (0, fixtures_1.test)('should throw error if __mablInit is already set', async ({ page }) => {
        const contentJsCode = fs_1.default.readFileSync(fixtures_1.CONTENT_JS_PATH_EXPORT, 'utf-8');
        await page.addInitScript(() => {
            window.mablFrameInfo = {
                tabId: 'initial',
                frameId: '0',
                position: { x: 0, y: 0 },
                pageSize: { width: 1000, height: 1000 },
            };
        });
        await page.goto('about:blank');
        await page.evaluate(() => {
            window.__mablInit = true;
        });
        const error = await page
            .evaluate(contentJsCode)
            .then(() => null)
            .catch((e) => e.message);
        (0, fixtures_1.expect)(error).toContain('Script already loaded and initialized');
    });
});
fixtures_1.test.describe('contentJs PlaywrightBridge messaging', () => {
    (0, fixtures_1.testWithoutBridge)('sendToMain should fail if mablSendMessageToMain is not exposed', async ({ contentJsNoBridge }) => {
        const error = await contentJsNoBridge.page.evaluate(async () => {
            try {
                await window.__mabl.bridge.sendToMain({ action: 'test' });
                return null;
            }
            catch (e) {
                return e.message;
            }
        });
        (0, fixtures_1.expect)(error).toContain('mablSendMessageToMain not available');
    });
    (0, fixtures_1.test)('sendToMain should send messages via mablSendMessageToMain', async ({ contentJs, }) => {
        const response = await contentJs.page.evaluate(async () => {
            return await window.__mabl.bridge.sendToMain({
                action: 'TEST_ACTION',
            });
        });
        const newMessages = contentJs.getNewMessages();
        (0, fixtures_1.expect)(newMessages).toHaveLength(1);
        (0, fixtures_1.expect)(newMessages[0]).toMatchObject({ action: 'TEST_ACTION' });
        (0, fixtures_1.expect)(response).toMatchObject({ success: true });
    });
    (0, fixtures_1.test)('sendToMain should handle multiple messages', async ({ contentJs }) => {
        await contentJs.page.evaluate(async () => {
            await window.__mabl.bridge.sendToMain({
                action: 'ACTION_1',
            });
            await window.__mabl.bridge.sendToMain({
                action: 'ACTION_2',
            });
            await window.__mabl.bridge.sendToMain({
                action: 'ACTION_3',
            });
        });
        const newMessages = contentJs.getNewMessages();
        (0, fixtures_1.expect)(newMessages).toHaveLength(3);
        (0, fixtures_1.expect)(newMessages[0]).toMatchObject({ action: 'ACTION_1' });
        (0, fixtures_1.expect)(newMessages[1]).toMatchObject({ action: 'ACTION_2' });
        (0, fixtures_1.expect)(newMessages[2]).toMatchObject({ action: 'ACTION_3' });
    });
    (0, fixtures_1.test)('bridge should handle async responses from main', async ({ page }) => {
        const contentJsCode = fs_1.default.readFileSync(fixtures_1.CONTENT_JS_PATH_EXPORT, 'utf-8');
        await page.addInitScript(() => {
            window.mablFrameInfo = {
                tabId: 'initial',
                frameId: '0',
                position: { x: 0, y: 0 },
                pageSize: { width: 1000, height: 1000 },
            };
        });
        await page.goto('about:blank');
        await page.exposeFunction('mablSendMessageToMain', async (message) => {
            await new Promise((resolve) => setTimeout(resolve, 10));
            return {
                processed: true,
                action: message.action,
                timestamp: Date.now(),
            };
        });
        await page.evaluate(contentJsCode);
        const response = await page.evaluate(async () => {
            return await window.__mabl.bridge.sendToMain({ action: 'ASYNC_TEST' });
        });
        (0, fixtures_1.expect)(response).toMatchObject({
            processed: true,
            action: 'ASYNC_TEST',
        });
        (0, fixtures_1.expect)(response).toHaveProperty('timestamp');
    });
});
