export function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

// 获取终端类型
export const getTerminalType = () => {
  if (navigator.userAgent.indexOf("UPHybrid") >= 0) {
    // app
    console.warn("You are running app in App-WebView.");
    return "App";
  } else if (navigator.userAgent.indexOf("hummer_pc") >= 0) {
    // Pc客户端股牛牛
    console.warn("You are running app in Pc-client-WebView.");
    return "PcClient";
  } else {
    // 浏览器
    console.warn("You are running app in Browser.");
    return "Browser";
  }
};

export const terminalType = getTerminalType();

export function isPcOrH5() {
  let client =
    /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
      navigator.userAgent
    );
  if (terminalType === "PcClient") {
    // 股牛牛PC客户端
    return 1;
  } else if (terminalType === "App") {
    // 股牛牛app
    return 2;
  } else if (client) {
    // h5
    return 4;
  } else {
    // Web
    return 3;
  }
}

export const clientType = isPcOrH5()

export let isMobi = /Mobi|Android|iPhone/i.test(navigator.userAgent);

// 区分系统
export const getSystem = () => {
  const ua = navigator.userAgent.toLowerCase();
  const testUa = (regexp) => regexp.test(ua);

  let system = "unknown";
  if (testUa(/windows|win32|win64|wow32|wow64/gi)) {
    system = "windows"; // window系统
  } else if (testUa(/macintosh|macintel/gi)) {
    system = "osx"; // osx系统
  } else if (testUa(/x11/gi)) {
    system = "linux"; // linux系统
  } else if (testUa(/android|adr/gi)) {
    system = "android"; // android系统
  } else if (testUa(/ios|iphone|ipad|ipod|iwatch/gi)) {
    system = "ios"; // ios系统
  }
  // const isHos = !!window.navigator.userAgent.match(/openharmony/i); // 是否是鸿蒙
  return system;
};

export const isHos = !!window.navigator.userAgent.match(/openharmony/i);