export function isExternal(path) {
  return (
    /^(https?:|mailto:|tel:)/.test(path) ||
    /^(http?:|mailto:|tel:)/.test(path) ||
    path.startsWith('/api/pluginCommon/staticInfo')
  )
}

export function validUsername(str) {
  const valid_map = ['admin', 'cyw']
  return valid_map.indexOf(str.trim()) >= 0
}

export const PHONE_REGEX = '^1[3|4|5|7|8][0-9]{9}$'

export const EMAIL_REGEX = '^[a-zA-Z0-9_._-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$'


export function publicTree(equiptrees:any,equipname:any) {
  let arr:any = []
  let newequiptree = JSON.parse(JSON.stringify(equiptrees))
  if(equipname !=''&& equipname !=null){
    newequiptree.forEach((item:any) => {
      if(item.name.toLowerCase().indexOf(equipname.toLowerCase()) !== -1){
        arr.push(item)
      }
    })
  }else{
    arr = newequiptree
  }
  if(!Array.isArray(arr) || !arr.length) return;
    let map:any = {};
    arr.forEach(item => {//建立每个数组元素id和该对象的关系
      map[item.id] = item //这里可以理解为浅拷贝，共享引用
    })
    let roots:any = [];
    arr.forEach(item => {
      const parent = map[item.pid];
      if(parent){
        (parent.children || (parent.children=[])).push(item);
      }
      else{
        roots.push(item);
      }
    })
    return roots
}
