"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebdriverController = exports.wrapWebdriver = void 0;
exports.webdriverConfig = webdriverConfig;
const Controller_1 = __importDefault(require("../Controller"));
const writeVariables_1 = __importDefault(require("../utils/writeVariables"));
const headlessNotSupportedError_1 = require("../utils/headlessNotSupportedError");
const mergeChromeArgs_1 = __importDefault(require("../utils/mergeChromeArgs"));
const checkUserArgs_1 = require("../utils/checkUserArgs");
var webdriverWrapping_1 = require("./webdriverWrapping");
Object.defineProperty(exports, "wrapWebdriver", { enumerable: true, get: function () { return webdriverWrapping_1.wrapDriver; } });
function webdriverConfig(arg) {
    const chrome = require('selenium-webdriver/chrome');
    const { axe, options } = arg;
    (0, writeVariables_1.default)(axe);
    const opts = options ? options : new chrome.Options();
    const existingArgs = (opts?.options_?.args || []);
    (0, checkUserArgs_1.checkUserArgs)(existingArgs);
    opts.options_.args = (0, mergeChromeArgs_1.default)(existingArgs, {
        disableOtherExtensions: false,
        disableLoadExtensionCommandFeature: true
    });
    if (['headless', '--headless'].some(flag => opts?.options_?.args?.includes(flag))) {
        throw new headlessNotSupportedError_1.HeadlessNotSupportedError('In WebdriverJS, use "chromeOptions.addArguments(\'--headless=new\')" instead of "setHeadless(true)" or  "addArguments(\'--headless\')"');
    }
    return opts;
}
class WebdriverController extends Controller_1.default {
    constructor(driver) {
        super({
            debugLoggerName: 'WebdriverController',
            integrationName: 'WebDriver',
            configurationFunction: 'webdriverConfig()'
        });
        this.driver = driver;
    }
    async executeScript(fn, ...args) {
        const script = `
      const callback = arguments[arguments.length - 1];
      Promise.resolve(...arguments)
        .then(${fn})
        .then(callback);
    `;
        const result = await this.driver.executeAsyncScript(script, ...args);
        return result;
    }
    captureScreenshot() {
        throw new Error('captureScreenshot is not yet implemented');
    }
}
exports.WebdriverController = WebdriverController;
//# sourceMappingURL=webdriver.js.map