"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.logsAfterEachHook = exports.logsBeforeEachHook = exports.logsAfterAllHook = exports.logsBeforeAllHook = void 0;
const utils_1 = require("./utils");
async function logsBeforeAllHook(testContext, proxyContext, rootPage) {
    if (utils_1.isNotPlaywrightPage(rootPage)) {
        rootPage.on('console', (message) => {
            testContext.consoleEntries.push(puppeteerOrPlaywrightConsoleMessageToOurRepresentation(message, testContext.dateConstructor.now()));
        });
        rootPage.on('pageerror', (error) => {
            testContext.unhandledExceptions.push(puppeteerOrPlaywrightPageErrorToOurRepresentation(error, testContext.dateConstructor.now()));
        });
    }
    else {
        rootPage.on('console', (message) => {
            testContext.consoleEntries.push(puppeteerOrPlaywrightConsoleMessageToOurRepresentation(message, testContext.dateConstructor.now()));
        });
        rootPage.on('pageerror', (error) => {
            testContext.unhandledExceptions.push(puppeteerOrPlaywrightPageErrorToOurRepresentation(error, testContext.dateConstructor.now()));
        });
    }
}
exports.logsBeforeAllHook = logsBeforeAllHook;
async function logsAfterAllHook(testContext) {
}
exports.logsAfterAllHook = logsAfterAllHook;
async function logsBeforeEachHook(testContext, proxyContext, rootPage) {
}
exports.logsBeforeEachHook = logsBeforeEachHook;
async function logsAfterEachHook(testContext, proxyContext, rootPage) {
    const stepContext = testContext.currentStep;
    utils_1.assertNotNullOrUndefined(stepContext);
    const consoleEntries = testContext.consoleEntries.filter(e => e.timestamp > stepContext.startTimestamp);
    const unhandledExceptions = testContext.unhandledExceptions.filter(e => e.timestamp > stepContext.startTimestamp);
    testContext.addStepMetadata({ consoleEntries, unhandledExceptions });
}
exports.logsAfterEachHook = logsAfterEachHook;
function puppeteerOrPlaywrightConsoleMessageToOurRepresentation(message, timestamp) {
    const location = message.location();
    return {
        level: message.type(),
        text: message.text(),
        line: location.lineNumber,
        column: location.columnNumber,
        url: location.url,
        timestamp,
    };
}
function puppeteerOrPlaywrightPageErrorToOurRepresentation(error, timestamp) {
    return {
        message: error.message,
        timestamp,
    };
}
