"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmulationService = exports.EMULATION_MODE = void 0;
const inversify_1 = require("inversify");
const config_js_1 = require("../../config.js");
const logger_js_1 = require("../../logger.js");
const session_js_1 = require("../session.js");
const browser_js_1 = require("./browser.js");
exports.EMULATION_MODE = (0, config_js_1.stringConfig)('EMULATION_MODE', 'desktop');
let EmulationService = class EmulationService {
    constructor(browser, config, logger) {
        this.browser = browser;
        this.config = config;
        this.logger = logger;
        browser.addTargetInit(target => this.applyToTarget(target));
        this.mode = this.getDefaultEmulationMode();
    }
    getDefaultEmulationMode() {
        return this.config.get(exports.EMULATION_MODE);
    }
    async onSessionStart() {
        this.mode = this.getDefaultEmulationMode();
        await this.applyToAllTargets();
    }
    isEnabled() {
        return this.mode !== 'disabled';
    }
    async setMode(mode) {
        this.mode = mode;
        await this.applyToAllTargets();
    }
    async applyToAllTargets() {
        for (const target of this.browser.attachedTargets()) {
            await this.applyToTarget(target);
        }
    }
    async applyToTarget(target) {
        if (target.type !== 'page') {
            return;
        }
        if (this.mode === 'disabled') {
            await target.send('Emulation.clearDeviceMetricsOverride');
        }
        else {
            const emulationSettings = this.getEmulationSettings();
            await target.send('Emulation.setDeviceMetricsOverride', emulationSettings);
        }
    }
    getEmulationSettings() {
        switch (this.mode) {
            case 'desktop':
                return {
                    width: 1270,
                    height: 712,
                    deviceScaleFactor: 1,
                    mobile: false,
                    scale: 1,
                };
            case 'mobile':
                return {
                    width: 375,
                    height: 667,
                    deviceScaleFactor: 1,
                    mobile: true,
                    scale: 1,
                    screenWidth: 375,
                    screenHeight: 667,
                };
            default:
                return {};
        }
    }
};
EmulationService = __decorate([
    (0, inversify_1.injectable)(),
    (0, session_js_1.SessionHandler)(),
    __param(0, (0, inversify_1.inject)(browser_js_1.BrowserService)),
    __param(1, (0, inversify_1.inject)(config_js_1.Configuration)),
    __param(2, (0, inversify_1.inject)(logger_js_1.Logger)),
    __metadata("design:paramtypes", [browser_js_1.BrowserService,
        config_js_1.Configuration,
        logger_js_1.Logger])
], EmulationService);
exports.EmulationService = EmulationService;
//# sourceMappingURL=emulation.js.map