"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 puppeteer_selector_mapping_1 = require("../utils/puppeteer-selector-mapping");
async function puppeteerScreenshot(testContext, fnName, proxyContext, rootPage, args) {
    if (!testContext.featuresSettings.screenshots) {
        return;
    }
    const filename = `${testContext.getStepIndex()}.${proxyContext.constructor.name.toLowerCase()}-${fnName}.${testContext.featuresSettings.screenshots.format === 'png' ? 'png' : 'jpg'}`;
    const selector = puppeteer_selector_mapping_1.extractPuppeteerSelector(fnName, args);
    if (selector) {
        try {
            const rect = await proxyContext.evaluate((selector) => {
                const found = document.querySelector(selector);
                if (!found) {
                    return { error: 'not found' };
                }
                const { x, y, width, height, top, right, bottom, left } = found.getBoundingClientRect();
                const { innerWidth: screenWidth, innerHeight: screenHeight } = window;
                const { devicePixelRatio } = window;
                return {
                    x,
                    y,
                    width,
                    height,
                    top,
                    right,
                    bottom,
                    left,
                    screenWidth,
                    screenHeight,
                    devicePixelRatio,
                };
            }, selector);
            testContext.addStepMetadata({ rect });
        }
        catch (e) {
            testContext.addStepMetadata({ rect: { error: e } });
        }
    }
    await rootPage.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,
    });
    testContext.addStepMetadata({ screenshot: filename });
}
exports.puppeteerScreenshot = puppeteerScreenshot;
