"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) {
    const filename = `${testContext.getStepIndex()}.${proxyContext.constructor.name.toLowerCase()}-${fnName}.png`;
    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),
    });
    testContext.addStepMetadata({ screenshot: filename });
}
exports.puppeteerScreenshot = puppeteerScreenshot;
