"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("@ulixee/commons/lib/utils");
const DomOverridesBuilder_1 = require("./DomOverridesBuilder");
const parseNavigatorPlugins_1 = require("./utils/parseNavigatorPlugins");
function loadDomOverrides(emulationProfile, data, userAgentData) {
    const domOverrides = new DomOverridesBuilder_1.default();
    domOverrides.add('Error.captureStackTrace');
    domOverrides.add('Error.constructor');
    const deviceProfile = emulationProfile.deviceProfile;
    const rtt = (0, utils_1.pickRandom)([25, 50, 100]);
    domOverrides.add('navigator.deviceMemory', {
        memory: deviceProfile.deviceMemory,
        maxHeapSize: deviceProfile.maxHeapSize,
    });
    domOverrides.add('navigator', {
        userAgentString: emulationProfile.userAgentOption.string,
        platform: emulationProfile.windowNavigatorPlatform,
        headless: emulationProfile.browserEngine.isHeaded !== true,
        pdfViewerEnabled: data.windowNavigator.navigator.pdfViewerEnabled?._$value,
        userAgentData,
        rtt,
    });
    domOverrides.add('MediaDevices.prototype.enumerateDevices', {
        videoDevice: deviceProfile.videoDevice,
    });
    domOverrides.add('Notification.permission');
    domOverrides.add('Permission.prototype.query');
    const windowChrome = data.windowChrome;
    if (windowChrome) {
        domOverrides.add('window.chrome', {
            updateLoadTimes: true,
            polyfill: {
                property: windowChrome.chrome,
                prevProperty: windowChrome.prevProperty,
            },
        });
    }
    const domPolyfill = data.domPolyfill;
    if (domPolyfill) {
        if (domPolyfill?.add?.length) {
            domOverrides.add('polyfill.add', {
                itemsToAdd: domPolyfill.add,
            });
        }
        if (domPolyfill?.remove?.length) {
            domOverrides.add('polyfill.remove', {
                itemsToRemove: domPolyfill.remove,
            });
        }
        if (domPolyfill?.modify?.length) {
            domOverrides.add('polyfill.modify', {
                itemsToModify: domPolyfill.modify,
            });
        }
        if (domPolyfill?.reorder?.length) {
            domOverrides.add('polyfill.reorder', {
                itemsToReorder: domPolyfill.reorder,
            });
        }
    }
    const windowNavigator = data.windowNavigator;
    domOverrides.add('navigator.plugins', (0, parseNavigatorPlugins_1.default)(windowNavigator.navigator));
    domOverrides.add('WebGLRenderingContext.prototype.getParameter', deviceProfile.webGlParameters);
    domOverrides.add('console.debug');
    domOverrides.add('HTMLIFrameElement.prototype');
    const locale = emulationProfile.locale;
    const voices = data.speech.voices?.map(x => {
        x.default = locale.includes(x.lang);
        return x;
    });
    if (voices?.length) {
        domOverrides.add('speechSynthesis.getVoices', { voices });
    }
    domOverrides.add('window.outerWidth', {
        frameBorderWidth: data.windowFraming.frameBorderWidth,
    });
    domOverrides.add('window.outerHeight', {
        frameBorderHeight: data.windowFraming.frameBorderHeight,
    });
    return domOverrides;
}
exports.default = loadDomOverrides;
//# sourceMappingURL=loadDomOverrides.js.map