"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.puppeteerScreenshot = void 0;
const path_1 = __importDefault(require("path"));
const utils_1 = require("../utils");
exports.puppeteerScreenshot = async function puppeteerScreenshot(hookArgs) {
    var _a;
    const { proxyContext, testContext, fnName, rootPage, stepResult } = hookArgs;
    const pageToScreenshot = (_a = (await utils_1.getPageFromProxyContext(proxyContext))) !== null && _a !== void 0 ? _a : rootPage;
    if (!testContext.featuresSettings.screenshots) {
        return;
    }
    const filename = `${stepResult.index}.${proxyContext.constructor.name.toLowerCase()}-${fnName}.${testContext.featuresSettings.screenshots.format === 'png' ? 'png' : 'jpg'}`;
    const rect = await getElementRect(hookArgs);
    if (rect) {
        stepResult.rect = rect;
    }
    await pageToScreenshot.screenshot({
        path: path_1.default.join(testContext.testArtifactsFolder, filename),
        type: testContext.featuresSettings.screenshots.format,
        quality: testContext.featuresSettings.screenshots.format === 'jpeg'
            ? testContext.featuresSettings.screenshots.quality
            : undefined,
        fullPage: testContext.featuresSettings.screenshots.fullPage,
    });
    stepResult.screenshot = filename;
};
async function getElementRect({ proxyContext, methodCallData }) {
    var _a;
    if (!shouldExtractElementRect(proxyContext)) {
        return null;
    }
    try {
        const elementHandleRect = (await getElementHandleRectWithBoundingBox(proxyContext)) ||
            (await getElementHandleRectWithSelector(proxyContext, (_a = utils_1.getLast(methodCallData)) === null || _a === void 0 ? void 0 : _a.selector));
        if (!elementHandleRect || elementHandleRect.error) {
            return elementHandleRect;
        }
        const windowRect = await proxyContext.evaluate(function getWindowRect() {
            const { devicePixelRatio, innerWidth: screenWidth, innerHeight: screenHeight } = window;
            return { screenWidth, screenHeight, devicePixelRatio };
        });
        const rect = {
            ...windowRect,
            ...elementHandleRect,
        };
        return rect;
    }
    catch (error) {
        return { error };
    }
}
async function getElementHandleRectWithBoundingBox(proxyContext) {
    if (typeof proxyContext.boundingBox !== 'function') {
        return null;
    }
    const boundingBox = await proxyContext.boundingBox();
    if (!boundingBox) {
        return null;
    }
    const rect = {
        ...boundingBox,
        top: boundingBox.y,
        left: boundingBox.x,
        bottom: boundingBox.y + boundingBox.height,
        right: boundingBox.x + boundingBox.width,
    };
    return rect;
}
async function getElementHandleRectWithSelector(proxyContext, selector) {
    if (!selector) {
        return null;
    }
    const rect = await proxyContext.evaluate(function getElementHandleRectOnBrowserSide(selector) {
        const element = document.querySelector(selector);
        if (!element) {
            return { error: 'not found' };
        }
        const { x, y, width, height, top, right, bottom, left } = element.getBoundingClientRect();
        return { x, y, width, height, top, right, bottom, left };
    }, selector);
    return rect;
}
function shouldExtractElementRect({ constructor: { name } }) {
    return name === 'ElementHandle' || name === 'Page';
}
