"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getEnvironmentData = exports.getBrowserVersion = exports.getDevice = exports.getOs = exports.getBrowser = void 0;

var _function = require("./function");

const getBrowser = (ua, vendor = '') => {
  if (ua.includes(' OPR/')) {
    if (ua.includes('Mini')) {
      return 'Opera Mini';
    }

    return 'Opera';
  } else if (/(BlackBerry|PlayBook|BB10)/i.test(ua)) {
    return 'BlackBerry';
  } else if (ua.includes('IEMobile') || ua.includes('WPDesktop')) {
    return 'Internet Explorer Mobile';
  } else if (ua.includes('Edge')) {
    return 'Microsoft Edge';
  } else if (ua.includes('FBIOS')) {
    return 'Facebook Mobile';
  } else if (ua.includes('Chrome')) {
    return 'Chrome';
  } else if (ua.includes('CriOS')) {
    return 'Chrome iOS';
  } else if (ua.includes('UCWEB') || ua.includes('UCBrowser')) {
    return 'UC Browser';
  } else if (ua.includes('FxiOS')) {
    return 'Firefox iOS';
  } else if (vendor.includes('Apple')) {
    if (ua.includes('Mobile')) {
      return 'Mobile Safari';
    }

    return 'Safari';
  } else if (ua.includes('Android')) {
    return 'Android Mobile';
  } else if (ua.includes('Konqueror')) {
    return 'Konqueror';
  } else if (ua.includes('Firefox')) {
    return 'Firefox';
  } else if (ua.includes('MSIE') || ua.includes('Trident/')) {
    return 'Internet Explorer';
  } else if (ua.includes('Gecko')) {
    return 'Mozilla';
  }

  return '';
};

exports.getBrowser = getBrowser;

const getOs = ua => {
  if (/Windows/i.test(ua)) {
    if (/Phone/.test(ua) || /WPDesktop/.test(ua)) {
      return 'Windows Phone';
    }

    return 'Windows';
  } else if (/(iPhone|iPad|iPod)/.test(ua)) {
    return 'iOS';
  } else if (/Android/.test(ua)) {
    return 'Android';
  } else if (/(BlackBerry|PlayBook|BB10)/i.test(ua)) {
    return 'BlackBerry';
  } else if (/Mac/i.test(ua)) {
    return 'Mac OS X';
  } else if (/Linux/.test(ua)) {
    return 'Linux';
  } else if (/CrOS/.test(ua)) {
    return 'Chrome OS';
  }

  return '';
};

exports.getOs = getOs;

const getDevice = ua => {
  if (/Windows Phone/i.test(ua) || /WPDesktop/.test(ua)) {
    return 'Windows Phone';
  } else if (/iPad/.test(ua)) {
    return 'iPad';
  } else if (/iPod/.test(ua)) {
    return 'iPod Touch';
  } else if (/iPhone/.test(ua)) {
    return 'iPhone';
  } else if (/(BlackBerry|PlayBook|BB10)/i.test(ua)) {
    return 'BlackBerry';
  } else if (/Android/.test(ua)) {
    return 'Android';
  }

  return '';
};

exports.getDevice = getDevice;
const versionRegexs = {
  'Internet Explorer Mobile': /rv:(\d+(\.\d+)?)/,
  'Microsoft Edge': /Edge\/(\d+(\.\d+)?)/,
  Chrome: /Chrome\/(\d+(\.\d+)?)/,
  'Chrome iOS': /CriOS\/(\d+(\.\d+)?)/,
  'UC Browser': /(UCBrowser|UCWEB)\/(\d+(\.\d+)?)/,
  Safari: /Version\/(\d+(\.\d+)?)/,
  'Mobile Safari': /Version\/(\d+(\.\d+)?)/,
  Opera: /(Opera|OPR)\/(\d+(\.\d+)?)/,
  Firefox: /Firefox\/(\d+(\.\d+)?)/,
  'Firefox iOS': /FxiOS\/(\d+(\.\d+)?)/,
  Konqueror: /Konqueror:(\d+(\.\d+)?)/,
  BlackBerry: /BlackBerry (\d+(\.\d+)?)/,
  'Android Mobile': /android\s(\d+(\.\d+)?)/,
  'Internet Explorer': /(rv:|MSIE )(\d+(\.\d+)?)/,
  Mozilla: /rv:(\d+(\.\d+)?)/
};

const getBrowserVersion = function (userAgent, vendor) {
  const browser = getBrowser(userAgent, vendor);
  const regex = versionRegexs[browser];

  if (regex === undefined) {
    return null;
  }

  const matches = userAgent.match(regex);

  if (!matches) {
    return null;
  }

  return parseFloat(matches[matches.length - 2]);
};

exports.getBrowserVersion = getBrowserVersion;
const IS_BROWSER = !!globalThis.navigator;
/**
 * @returns {{
 *   browser: string
 *   browserVersion: number
 *   os: string
 *   device: string
 *   nativeAppVersion: string || null
 * }}
 */

const getEnvironmentData = (0, _function.once)(() => {
  if (!IS_BROWSER) {
    return {};
  }

  const {
    vendor,
    userAgent
  } = navigator;
  let electron = false;

  if (window.acElectron) {
    electron = window.acElectron.version || 'unknown';
  }

  return {
    browser: electron ? 'Electron App' : getBrowser(userAgent, vendor),
    browserVersion: getBrowserVersion(userAgent, vendor),
    os: getOs(userAgent),
    device: getDevice(userAgent) || 'unknown',
    nativeAppVersion: electron || null
  };
});
exports.getEnvironmentData = getEnvironmentData;