/**
 * 判断url是否是http或https
 * @param {string} path
 * @returns {Boolean}
 */
export function isHttp(url: string) {
  return url.indexOf("http://") !== -1 || url.indexOf("https://") !== -1;
}

/**
 * 判断path是否为外链
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
  return /^(https?:|mailto:|tel:)/.test(path);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str: string) {
  const valid_map = ["admin", "editor"];
  return valid_map.indexOf(str.trim()) >= 0;
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function 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);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str: string) {
  const reg = /^[a-z]+$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str: string) {
  const reg = /^[A-Z]+$/;
  return reg.test(str);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str: string) {
  const reg = /^[A-Za-z]+$/;
  return reg.test(str);
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
// export function 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);
// }

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str: string) {
  if (typeof str === "string") {
    return true;
  }
  return false;
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg: unknown) {
  if (typeof Array.isArray === "undefined") {
    return Object.prototype.toString.call(arg) === "[object Array]";
  }
  return Array.isArray(arg);
}

export function setNum(v?:any){
  if(v){
    v = v+''
    //替换掉数字和小数点 . 之外的字符
    v = v.replace(/[^\d.]/g,'')
    //把2个以上的小数点 . 替换成一个小数点 . 
    v = v.replace(/\.{2,}/g,'.')
    //保证第一个字符为数字而不是小数点 .
    v = v.replace(/^\./g,'')
    //保证最后一个字符为数字而不是小数点 .
    //v = v.replace(/\.$/g,'')
    //保证小数点 . 只出现一次，而不能出现两次以上
    v = v.replace('.','$#$').replace(/\./g,'').replace('$#$','.')
    //只能输入两个小数
    v = v.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')

    if(/^0+[1-9]\d*\.*/.test(v)) {
      v = v.replace(/^0+/,'')
    }
    if(/^0+\.*/.test(v)) {
      v = v.replace(/^0+/,'0')
    }
    return v
  }
  return ''
}

export function carNum(rule:any, value:any, callback:any) {
  if(value.length > 0){
    const regExp = new RegExp(/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF][A-HJ-NP-Z0-9][0-9]{4}))$/)
    //const regExp = new RegExp(/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[0-9][A-Z]$/)
    if (!regExp.test(value)) {
      return callback(new Error('车牌号格式不正确'))
    }
    return callback()
  }
  return callback()
}

export  function getDateDaysAgo(days:number = 0,time?:string) {
  const date = new Date();
  date.setDate(date.getDate() - days);
  
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  
  if(time){
    return `${year}-${month}-${day} ${time}`
  }
  return `${year}-${month}-${day}`
}