"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractDefaultEnvironments = void 0;
const utils = __importStar(require("@applitools/utils"));
async function extractDefaultEnvironments({ driver, settings, }) {
    var _a, _b;
    const environment = { ...settings === null || settings === void 0 ? void 0 : settings.environment };
    if (!driver)
        return [environment];
    const currentContext = driver.currentContext;
    try {
        const driverEnvironment = await driver.getEnvironment();
        const viewport = await driver.getViewport();
        const size = await driver.getViewportSize();
        environment.viewportSize = utils.geometry.scale(size, viewport.viewportScale);
        environment.environmentId = utils.general.guid();
        if (driverEnvironment.isEC)
            environment.ecSessionId = (_a = (await driver.getSessionId())) !== null && _a !== void 0 ? _a : undefined;
        if (driverEnvironment.isWeb)
            environment.userAgent = await driver.getUserAgentLegacy();
        if (!environment.deviceName && driverEnvironment.deviceName)
            environment.deviceName = driverEnvironment.deviceName;
        if (!environment.os) {
            if (driverEnvironment.isNative && driverEnvironment.platformName) {
                environment.os = driverEnvironment.platformName;
                if (driverEnvironment.platformVersion)
                    environment.os += ` ${driverEnvironment.platformVersion}`;
                if (!(settings === null || settings === void 0 ? void 0 : settings.keepPlatformNameAsIs)) {
                    if (/^android/i.test(environment.os)) {
                        environment.os = `Android${environment.os.slice(7)}`;
                    }
                    if (/^ios/i.test(environment.os)) {
                        environment.os = `iOS${environment.os.slice(3)}`;
                    }
                }
            }
            else if (driverEnvironment.isReliable &&
                driverEnvironment.isChromium &&
                ((driverEnvironment.isWindows && Number.parseInt(driverEnvironment.browserVersion) >= 107) ||
                    (driverEnvironment.isMac && Number.parseInt(driverEnvironment.browserVersion) >= 90))) {
                environment.os = `${driverEnvironment.platformName} ${(_b = driverEnvironment.platformVersion) !== null && _b !== void 0 ? _b : ''}`.trim();
            }
        }
        return [environment];
    }
    finally {
        await currentContext.focus();
    }
}
exports.extractDefaultEnvironments = extractDefaultEnvironments;
