"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.extractCapabilitiesViewport = exports.extractCapabilitiesEnvironment = void 0;
const utils = __importStar(require("@applitools/utils"));
function extractCapabilitiesEnvironment(capabilities) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
    if (capabilities.capabilities)
        capabilities = capabilities.capabilities;
    const environment = {
        browserName: !((_b = (_a = capabilities.app) !== null && _a !== void 0 ? _a : capabilities['appium:app']) !== null && _b !== void 0 ? _b : (_c = capabilities['appium:desired']) === null || _c === void 0 ? void 0 : _c.app) && !capabilities.bundleId
            ? ((_d = capabilities.browserName) !== null && _d !== void 0 ? _d : (_e = capabilities.desired) === null || _e === void 0 ? void 0 : _e.browserName) || undefined
            : undefined,
        browserVersion: ((_f = capabilities.browserVersion) !== null && _f !== void 0 ? _f : capabilities.version) || undefined,
        platformName: ((_k = (_g = capabilities.platformName) !== null && _g !== void 0 ? _g : (_j = ((_h = capabilities.desired) !== null && _h !== void 0 ? _h : capabilities['appium:desired'])) === null || _j === void 0 ? void 0 : _j.platformName) !== null && _k !== void 0 ? _k : capabilities.platform) ||
            undefined,
        platformVersion: ((_m = (_l = capabilities.platformVersion) !== null && _l !== void 0 ? _l : capabilities['appium:platformVersion']) !== null && _m !== void 0 ? _m : (_o = capabilities['appium:desired']) === null || _o === void 0 ? void 0 : _o.platformVersion) ||
            undefined,
        isW3C: isW3C(capabilities),
        isMobile: isMobile(capabilities),
        isChrome: isChrome(capabilities),
        isECClient: !!capabilities['applitools:isECClient'],
    };
    if (environment === null || environment === void 0 ? void 0 : environment.isMobile) {
        environment.deviceName =
            ((_s = (_r = (_q = ((_p = capabilities['appium:desired']) !== null && _p !== void 0 ? _p : capabilities.desired)) === null || _q === void 0 ? void 0 : _q.deviceName) !== null && _r !== void 0 ? _r : capabilities['appium:deviceName']) !== null && _s !== void 0 ? _s : capabilities.deviceName) ||
                undefined;
        environment.isIOS = isIOS(capabilities);
        environment.isAndroid = isAndroid(capabilities);
        if (!environment.browserName) {
            environment.isNative = true;
            environment.applitoolsLib = { instrumented: false };
            if (environment.isAndroid) {
                const optionalIntentArguments = (_t = capabilities['appium:optionalIntentArguments']) !== null && _t !== void 0 ? _t : capabilities.optionalIntentArguments;
                environment.applitoolsLib.instrumented = !!(optionalIntentArguments === null || optionalIntentArguments === void 0 ? void 0 : optionalIntentArguments.includes('APPLITOOLS_API_KEY'));
            }
            else if (environment.isIOS) {
                const processArguments = (_u = capabilities['appium:processArguments']) !== null && _u !== void 0 ? _u : capabilities.processArguments;
                environment.applitoolsLib.instrumented = utils.types.isString(processArguments)
                    ? processArguments.includes('APPLITOOLS_API_KEY')
                    : !!((_v = processArguments === null || processArguments === void 0 ? void 0 : processArguments.env) === null || _v === void 0 ? void 0 : _v.APPLITOOLS_API_KEY);
            }
            if (environment.applitoolsLib.instrumented) {
                const conflictingCapabilities = { fullReset: false, noReset: true, autoLaunch: false };
                environment.applitoolsLib.conflictingCapabilities = Object.entries(conflictingCapabilities).reduce((conflictingCapabilities, [capability, value]) => {
                    ;
                    [capability, `appium:${capability}`].forEach(capability => {
                        if (capabilities[capability] === value) {
                            conflictingCapabilities !== null && conflictingCapabilities !== void 0 ? conflictingCapabilities : (conflictingCapabilities = {});
                            conflictingCapabilities[capability] = value;
                        }
                    });
                    return conflictingCapabilities;
                }, undefined);
            }
        }
        else if (environment.isIOS &&
            capabilities.CFBundleIdentifier &&
            !/mobilesafari/i.test(capabilities.CFBundleIdentifier)) {
            environment.browserName = undefined;
            environment.isNative = true;
        }
        else {
            environment.isNative = false;
        }
    }
    return environment;
}
exports.extractCapabilitiesEnvironment = extractCapabilitiesEnvironment;
function extractCapabilitiesViewport(capabilities) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
    if (capabilities.capabilities)
        capabilities = capabilities.capabilities;
    const viewport = {
        displaySize: extractDisplaySize(capabilities),
        orientation: (_c = ((_b = (_a = capabilities['appium:orientation']) !== null && _a !== void 0 ? _a : capabilities.deviceOrientation) !== null && _b !== void 0 ? _b : capabilities.orientation)) === null || _c === void 0 ? void 0 : _c.toLowerCase(),
        pixelRatio: (_d = capabilities['appium:pixelRatio']) !== null && _d !== void 0 ? _d : capabilities.pixelRatio,
        statusBarSize: (_f = (_e = capabilities['appium:statBarHeight']) !== null && _e !== void 0 ? _e : capabilities.statBarHeight) !== null && _f !== void 0 ? _f : (_h = ((_g = capabilities['appium:viewportRect']) !== null && _g !== void 0 ? _g : capabilities.viewportRect)) === null || _h === void 0 ? void 0 : _h.top,
    };
    if (viewport.displaySize &&
        viewport.orientation &&
        ((_j = capabilities['appium:viewportRect']) !== null && _j !== void 0 ? _j : capabilities.viewportRect)) {
        viewport.navigationBarSize =
            viewport.orientation === 'landscape'
                ? viewport.displaySize.width -
                    (((_k = capabilities['appium:viewportRect']) !== null && _k !== void 0 ? _k : capabilities.viewportRect).left +
                        ((_l = capabilities['appium:viewportRect']) !== null && _l !== void 0 ? _l : capabilities.viewportRect).width)
                : viewport.displaySize.height -
                    (((_m = capabilities['appium:viewportRect']) !== null && _m !== void 0 ? _m : capabilities.viewportRect).top +
                        ((_o = capabilities['appium:viewportRect']) !== null && _o !== void 0 ? _o : capabilities.viewportRect).height);
    }
    return viewport;
}
exports.extractCapabilitiesViewport = extractCapabilitiesViewport;
function isW3C(capabilities) {
    const isW3C = Boolean((capabilities.platformName || capabilities.browserVersion) &&
        (capabilities.platformVersion || capabilities.hasOwnProperty('setWindowRect')));
    return isW3C || isAppium(capabilities);
}
function isAppium(capabilities) {
    return (Boolean(capabilities.automationName || capabilities.deviceName || capabilities.appiumVersion) ||
        Object.keys(capabilities).some(cap => cap.startsWith('appium:')));
}
function isChrome(capabilities) {
    return Boolean(capabilities.chrome || capabilities['goog:chromeOptions']);
}
function _isFirefox(capabilities) {
    return capabilities.browserName === 'firefox' || Object.keys(capabilities).some(cap => cap.startsWith('moz:'));
}
function isMobile(capabilities) {
    var _a, _b;
    return (capabilities.browserName === '' ||
        ['ipad', 'iphone', 'android'].includes((_b = (_a = capabilities.browserName) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : '') ||
        isAppium(capabilities));
}
function isIOS(capabilities) {
    return /iOS/i.test(capabilities.platformName) || /(iPad|iPhone)/i.test(capabilities.deviceName);
}
function isAndroid(capabilities) {
    return /Android/i.test(capabilities.platformName) || /Android/i.test(capabilities.browserName);
}
function extractDisplaySize(capabilities) {
    var _a;
    const deviceScreenSize = (_a = capabilities['appium:deviceScreenSize']) !== null && _a !== void 0 ? _a : capabilities.deviceScreenSize;
    if (!deviceScreenSize)
        return undefined;
    const [width, height] = deviceScreenSize.split('x');
    if (Number.isNaN(Number(width)) || Number.isNaN(Number(height)))
        return undefined;
    return { width: Number(width), height: Number(height) };
}
