/**
 * 获取UA
 * @private
 * @returns {string} ua
 */
function getRealUA() {
  return (navigator.userAgent || '').toLowerCase();
}
/**
 * 检查是否是ios环境
 * @returns {boolean} 是否是ios环境
 *
 * @example
 *
 * checkUAIsIOS()
 *
 * // => true
 *
 */
function checkUAIsIOS() {
  var realUA = getRealUA();
  return /iphone|ipod|ipad|Mac OS X/i.test(realUA) || !!realUA.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
}
/**
 * 获取useragent类型
 * @returns {object} useragent的map
 * @example
 *
 * getEnvUAType()
 *
 * // =>
 * {
 *   isWeixin: false,
 *   isWorkWeixin: false,
 *   isQQ: false,
 *   isPvpApp: false,
 *   isTipApp: false,
 *   isAndroid: false,
 *   isIos: true,
 *   isIOS: true,
 *   isMsdk: false,
 *   isMsdkV5: false,
 *   isSlugSdk: false,
 *   isInGame: false,
 *   isGHelper: false,
 *   isGHelper20004: false,
 *   isMiniProgram: false,
 *   isLolApp: false,
 *   isWindowsPhone: false,
 *   isSymbian: false,
 *   isPc: true,
 * };
 *
 */
function getEnvUAType() {
  var ua = getRealUA();
  var isWeixin = ua.indexOf('micromessenger') !== -1;
  var isWorkWeixin = ua.indexOf('wxwork') !== -1;
  var isQQ = ua.indexOf(' qq/') !== -1;
  var isPvpApp = ua.indexOf(' igameapp/') !== -1;
  var isTipApp = ua.indexOf(' gamelife/') !== -1;
  var isAndroid = ua.indexOf('android') !== -1;
  var isIos = checkUAIsIOS();
  var isIOS = isIos;
  var isMsdk = ua.indexOf(' msdk/') !== -1; // msdk
  var isMsdkX = ua.indexOf(' webviewx msdk/') !== -1; // 嵌入式msdk浏览器
  var isMsdkV5 = ua.indexOf(' msdk/5') !== -1; // msdk V5
  var isSlugSdk = ua.indexOf('ingame') !== -1; // 微社区sdk
  var isInGame = isMsdk || isSlugSdk; // 是否游戏内
  var isGHelper = ua.indexOf('gamehelper') !== -1;
  var isGHelper20004 = ua.indexOf('gamehelper_20004') !== -1;
  var isMiniProgram = ua.indexOf('miniprogram') !== -1 || typeof window !== 'undefined'
  // eslint-disable-next-line no-underscore-dangle
  && window.__wxjs_environment === 'miniprogram';
  var isLolApp = ua.indexOf('lolapp') !== -1; // 掌上英雄联盟app
  var isWindowsPhone = /(?:Windows Phone)/.test(ua);
  var isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone;
  var isPc = !ua.match(/(phone|pad|pod|iphone|ipod|ios|ipad|android|mobile|blackberry|iemobile|mqqbrowser|juc|fennec|wosbrowser|browserng|webos|symbian|windows phone)/i);
  return {
    isWeixin: isWeixin,
    isWorkWeixin: isWorkWeixin,
    isQQ: isQQ,
    isPvpApp: isPvpApp,
    isTipApp: isTipApp,
    isAndroid: isAndroid,
    isIos: isIos,
    isIOS: isIOS,
    isMsdk: isMsdk,
    isMsdkX: isMsdkX,
    isMsdkV5: isMsdkV5,
    isSlugSdk: isSlugSdk,
    isInGame: isInGame,
    isGHelper: isGHelper,
    isGHelper20004: isGHelper20004,
    isMiniProgram: isMiniProgram,
    isLolApp: isLolApp,
    isWindowsPhone: isWindowsPhone,
    isSymbian: isSymbian,
    isPc: isPc
  };
}
/**
 * 初始化环境信息
 * 获取当前运行环境的 UA 类型信息
 * @returns 环境信息对象
 * @example
 * ```ts
 * const env = initEnv();
 * console.log(env.isWeixin); // false
 * console.log(env.isIOS); // true
 * ```
 */
function initEnv() {
  return getEnvUAType();
}
/**
 * 检查是否是node环境
 * @returns {boolean} 是否node环境
 * @example

  const res = checkNodeEnv();
  // false
 */
var checkNodeEnv = function checkNodeEnv() {
  return !(typeof window !== 'undefined' && window.navigator);
};

export { checkNodeEnv, checkUAIsIOS, getEnvUAType, initEnv };
