// https
const isExternal = (path: string) => {
  return /^(https?:|mailto:|tel:)/.test(path);
};

// 用户名
const validUsername = (str: string) => {
  const valid_map = ["admin", "editor"];
  return valid_map.indexOf(str.trim()) >= 0;
};

// url
const validURL = (url: string) => {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  return reg.test(url);
};

// 小写字母
const validLowerCase = (str: string) => {
  const reg = /^[a-z]+$/;
  return reg.test(str);
};

// 大写字母
const validUpperCase = (str: string) => {
  const reg = /^[A-Z]+$/;
  return reg.test(str);
};

// 字母
const validAlphabets = (str: string) => {
  const reg = /^[A-Za-z]+$/;
  return reg.test(str);
};

// 邮箱
const validEmail = (email: string) => {
  const reg =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return reg.test(email);
};

// 字符串
const isString = (str: any) => {
  if (typeof str === "string" || str instanceof String) {
    return true;
  }
  return false;
};

// 数组
const isArray = (arg: any) => {
  if (typeof Array.isArray === "undefined") {
    return Object.prototype.toString.call(arg) === "[object Array]";
  }
  return Array.isArray(arg);
};

// 邮箱
const isEmail = (s: string) => {
  return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(
    s
  );
};

// 手机号码

const isMobile = (s: string) => {
  return /^1[3|1|4|5|6|7|8|9][0-9]\d{8}$/.test(s);
};

//电话号码
const isPhone = (s: string) => {
  return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s);
};

// 固定电话(支持分机) 原始

const isExtension = (s: string) => {
  let type = true;
  const arr = s.split("-");
  console.log(arr);
  if (arr.length === 2 || arr.length === 3) {
    type = setF(arr);
  } else {
    type = false;
  }
  console.log("console" + type);
  return type;
  ///0\d{2,3}-\d{7,8}(-\d{1,6})?/.test(s)
};

//区-座-分
const isqzf = (s: string) => {
  let type = true;
  const arr = s.split("-");
  if (arr.length === 3) {
    type = setqj(arr[0]) && setzj(arr[1]) && setfj(arr[2]);
  } else {
    type = false;
  }
  console.log("console区-座-分 " + type);
  return type;
};
// //区-座
const isqz = (s: string) => {
  let type = true;
  const arr = s.split("-");
  if (arr.length === 2) {
    type = setqj(arr[0]) && setzj(arr[1]);
  } else {
    type = false;
  }
  console.log("console区-座 " + type);
  return type;
};
//座-分
const iszf = (s: string) => {
  let type = true;
  const arr = s.split("-");
  if (arr.length === 2) {
    type = setzj(arr[0]) && setfj(arr[1]);
  } else {
    type = false;
  }
  console.log("console座-分" + type);
  return type;
};

//座
const isz = (s: string) => {
  let type = true;
  const arr = s.split("-");
  if (arr.length === 1) {
    type = setzj(arr[0]);
  } else {
    type = false;
  }
  console.log("console座" + type);
  return type;
};

const setqj = (s: string) => {
  return /0\d{2,3}?$/.test(s) && (s.length == 3 || s.length == 4);
};
const setzj = (s: string) => {
  return /\d{7,8}$/.test(s) && (s.length == 7 || s.length == 8);
};
const setfj = (s: string) => {
  return /\d{1,6}$/.test(s) && s.length > 0 && s.length < 7;
};
//原始计算
const setF = (arr: any) => {
  let type1 = true,
    type2 = true,
    type3 = true;
  type1 = setqj(arr[0]);
  type2 = arr[1].length === 7 || arr[1].length === 8 ? setzj(arr[1]) : false;
  if (arr.length === 3) {
    type3 = arr[2].length === 0 || arr[2].length > 6 ? false : setfj(arr[2]);
  }
  console.log(type1, type2, type3);
  if (arr.length === 2) {
    return type1 && type2;
  } else {
    return type1 && type2 && type3;
  }
};

// 纳税人识别号
const isLicense = (s: string) => {
  return /[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}/.test(s);
};

//身份证号码

const isIDentityCard = (s: string) => {
  return /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
    s
  );
};

//URL地址

const isURL = (s: string) => {
  return /^http[s]?:\/\/.*/.test(s);
};

//汉语

const isChinese = (s: string) => {
  return /[\u4e00-\u9fa5]$/.test(s);
};

// 数字

const isnumber = (s: string) => {
  return /^[0-9]*$/.test(s);
};
//数字字母

const isAlphanumeric = (s: string) => {
  return /^[0-9A-Za-z]*$/.test(s);
};

// 表情包

const isEmoticon = (s: string) => {
  const reg =
    /[^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n]/g;
  return reg.test(s);
};

//判断是否为微信浏览器

const JudgeEnvironment = () => {
  const ua = navigator.userAgent.toLowerCase();
  let environment = "";
  const isWeixin = ua.indexOf("micromessenger") != -1;
  if (isWeixin) {
    environment = "Weixin";
  }
  const isWelink = ua.indexOf("huawei-anyoffice") != -1;
  if (isWelink) {
    environment = "Welink";
  }
  const isDingDing = ua.indexOf("dingtalk") != -1;
  if (isDingDing) {
    environment = "isDingDing";
  }
  if (environment === "") {
    environment = "otherBrowser";
  }
  return environment;
};
//
const timestampToTime = timestamp => {
  const date = new Date(timestamp); // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const Y = date.getFullYear() + "-";
  const M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  const D = date.getDate() + " ";
  let h = date.getHours() + "";
  h = parseInt(h) < 10 ? `0${h}:` : h + ":";
  let m = date.getMinutes() + "";
  m = parseInt(m) < 10 ? `0${m}:` : m + ":";
  let s = date.getSeconds() + "";
  s = parseInt(s) < 10 ? `0${s}` : s;
  return Y + M + D + h + m + s;
};
//
const isCreditCode = (s: string) => {
  const reg = /[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g;
  return reg.test(s);
};

const isExcel = (s: string) => {
  return /\.(xlsx|xls|csv)$/.test(s);
};

export {
  isExternal,
  validUsername,
  validURL,
  validLowerCase,
  validUpperCase,
  validAlphabets,
  validEmail,
  isString,
  isArray,
  isEmail,
  isMobile,
  isPhone,
  isExtension,
  isqzf,
  isqz,
  iszf,
  isz,
  setqj,
  setzj,
  setfj,
  setF,
  isLicense,
  isIDentityCard,
  isURL,
  isChinese,
  isnumber,
  isAlphanumeric,
  isEmoticon,
  JudgeEnvironment,
  timestampToTime,
  isCreditCode,
  isExcel
};
