"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapPuppeteerPage = exports.wrapPuppeteer = exports.PuppeteerController = void 0;
exports.puppeteerConfig = puppeteerConfig;
const Controller_1 = __importDefault(require("../Controller"));
const headlessNotSupportedError_1 = require("../utils/headlessNotSupportedError");
const mergeChromeArgs_1 = __importDefault(require("../utils/mergeChromeArgs"));
const writeVariables_1 = __importDefault(require("../utils/writeVariables"));
const checkUserArgs_1 = require("../utils/checkUserArgs");
function headlessNotSupportedError(config) {
    if (!config?.headless || config.headless === 'new') {
        return;
    }
    if (config.headless === 'shell') {
        throw new headlessNotSupportedError_1.HeadlessNotSupportedError('In Puppeteer, pass "headless: true" (not "headless: \'shell\'") in your puppeteer.launch options.');
    }
    throw new headlessNotSupportedError_1.HeadlessNotSupportedError('pass "headless: \'new\'" in your puppeteer.launch options. In more recent Puppeteer versions, pass "headless: true" instead.');
}
function puppeteerConfig(opts) {
    const { axe, ...config } = opts;
    let { args = [] } = config;
    (0, checkUserArgs_1.checkUserArgs)(args);
    (0, writeVariables_1.default)(axe);
    headlessNotSupportedError(config);
    const existingArgs = args;
    args = (0, mergeChromeArgs_1.default)(existingArgs, { disableOtherExtensions: true });
    return {
        ...config,
        headless: false,
        args
    };
}
class PuppeteerController extends Controller_1.default {
    constructor(driver) {
        super({
            debugLoggerName: 'PuppeteerController',
            integrationName: 'Puppeteer',
            configurationFunction: 'puppeteerConfig()'
        });
        this.driver = driver;
    }
    async executeScript(fn, ...args) {
        const result = await this.driver.evaluate(fn, ...args);
        return result;
    }
    captureScreenshot() {
        return this.driver.screenshot({ encoding: 'base64' });
    }
}
exports.PuppeteerController = PuppeteerController;
const wrapPuppeteer = (browserContext, controller) => {
    throw new Error(`
    \`wrapPuppeteer\` is deprecated and may not work as intended. Please use \`wrapPuppeteerPage\` instead. Example:\n
    let page = await browser.newPage()
    const controller = new PuppeteerController(page)
    // Use the new wrapped \`page\` object
    page = wrapPuppeteerPage(page, controller)
    `);
};
exports.wrapPuppeteer = wrapPuppeteer;
var puppeteerWrapping_1 = require("./puppeteerWrapping");
Object.defineProperty(exports, "wrapPuppeteerPage", { enumerable: true, get: function () { return puppeteerWrapping_1.wrapPage; } });
//# sourceMappingURL=puppeteer.js.map