/**
 * @Datei browser.js
 * @Modul-Browser
 * /
import * as Dom from './dom';
import window from 'global/window';

const USER_AGENT = window.navigator && window.navigator.userAgent || '';
const webkitVersionMap = (/AppleWebKit\/([\d.]+)/i).exec(USER_AGENT);
const appleWebkitVersion = webkitVersionMap ? parseFloat(webkitVersionMap.pop()) : null;

/**
 * Ob dieses Gerät ein iPod ist oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_IPOD = (/iPod/i).test(USER_AGENT);

/**
 * Die erkannte iOS-Version - oder `null`.
 *
 * @static
 * @const
 * @type {string|null}
 * /
export const IOS_VERSION = (function() {
  const match = USER_AGENT.match(/OS (\d+)_/i);

  if (match && match[1]) {
    return match[1];
  }
  null zurückgeben;
}());

/**
 * Ob es sich um ein Android-Gerät handelt oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_ANDROID = (/Android/i).test(USER_AGENT);

/**
 * Die erkannte Android-Version - oder `null`.
 *
 * @static
 * @const
 * @Typ {Zahl|String|null}
 * /
export const ANDROID_VERSION = (function() {
  // Dies entspricht den Android Major.Minor.Patch-Versionen
  // ANDROID_VERSION ist Major.Minor als Zahl, wenn Minor nicht verfügbar ist, wird nur Major zurückgegeben
  const match = USER_AGENT.match(/Android (\d+)(?:\.(\d+))?(?:\.(\d+))*/i);

  if (!match) {
    null zurückgeben;
  }

  const major = match[1] && parseFloat(match[1]);
  const minor = match[2] && parseFloat(match[2]);

  if (major && minor) {
    return parseFloat(match[1] + '.' + match[2]);
  } else if (major) {
    hauptfach zurückkehren;
  }
  null zurückgeben;
}());

/**
 * Ob dies ein nativer Android-Browser ist oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_NATIVE_ANDROID = IS_ANDROID && ANDROID_VERSION < 5 && appleWebkitVersion < 537;

/**
 * Ob es sich dabei um Mozilla Firefox handelt oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_FIREFOX = (/Firefox/i).test(USER_AGENT);

/**
 * Ob das nun Microsoft Edge ist oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_EDGE = (/Edg/i).test(USER_AGENT);

/**
 * Ob das nun Google Chrome ist oder nicht.
 *
 * Dies gilt auch für Chrome auf iOS, das eine andere Unterstützung hat
 * da es sich unter der Haube tatsächlich um Safari handelt.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_CHROME = !IS_EDGE && ((/Chrome/i).test(USER_AGENT) || (/CriOS/i).test(USER_AGENT));

/**
 * Die erkannte Google Chrome Version - oder `null`.
 *
 * @static
 * @const
 * @Typ {Zahl|null}
 * /
export const CHROME_VERSION = (function() {
  const match = USER_AGENT.match(/(Chrome|CriOS)\/(\d+)/);

  if (match && match[2]) {
    return parseFloat(match[2]);
  }
  null zurückgeben;
}());

/**
 * Die erkannte Internet Explorer Version - oder `null`.
 *
 * @static
 * @const
 * @Typ {Zahl|null}
 * /
export const IE_VERSION = (function() {
  const result = (/MSIE\s(\d+)\.\d/).exec(USER_AGENT);
  let version = result && parseFloat(result[1]);

  if (!version && (/Trident\/7.0/i).test(USER_AGENT) && (/rv:11.0/).test(USER_AGENT)) {
    // IE 11 hat einen anderen User-Agent-String als andere IE-Versionen
    version = 11.0;
  }

  version zurückgeben;
}());

/**
 * Unabhängig davon, ob es sich um Desktop-Safari handelt oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_SAFARI = (/Safari/i).test(USER_AGENT) && !IS_CHROME && !IS_ANDROID && !IS_EDGE;

/**
 * Ob es sich um einen Windows-Rechner handelt oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_WINDOWS = (/Windows/i).test(USER_AGENT);

/**
 * Ob dieses Gerät berührungsempfindlich ist oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const TOUCH_ENABLED = Boolean(Dom.isReal() && (
  'ontouchstart' im Fenster ||
  window.navigator.maxTouchPoints ||
  window.DocumentTouch && window.document instanceof window.DocumentTouch));

/**
 * Ob dieses Gerät ein iPad ist oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_IPAD = (/iPad/i).test(USER_AGENT) ||
  (IS_SAFARI && TOUCH_ENABLED && !(/iPhone/i).test(USER_AGENT));

/**
 * Ob es sich bei dem Gerät um ein iPhone handelt oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
// Die UIWebView der Facebook-App identifiziert sowohl ein iPhone als auch ein iPad, so dass
// Um iPhones zu identifizieren, müssen wir iPads ausschließen.
// http://artsy.github.io/blog/2012/10/18/the-perils-of-ios-user-agent-sniffing/
export const IS_IPHONE = (/iPhone/i).test(USER_AGENT) && !IS_IPAD;

/**
 * Ob es sich um ein iOS-Gerät handelt oder nicht.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_IOS = IS_IPHONE || IS_IPAD || IS_IPOD;

/**
 * Unabhängig davon, ob es sich um eine beliebige Safari-Variante handelt oder nicht - einschließlich iOS.
 *
 * @static
 * @const
 * @Typ {Boolean}
 * /
export const IS_ANY_SAFARI = (IS_SAFARI || IS_IOS) && !IS_CHROME;