import Mock from 'mockjs'
import { strChineseFirstPY } from './strChineseFirstPY'
import { oMultiDiff } from './oMultiDiff'
import { Staff } from '@/Interface'
import { Role } from '@/service/moudles/service.dto'

// 参数,中文字符串
// 返回值:拼音首字母串数组
export function makePy(str:string) {
  if (typeof (str) !== 'string') {
    throw new Error('函数makePy需要字符串类型参数!')
  }
  const newStr = trim(str)
  const arrResult = new Array() // 保存中间结果的数组
  for (let i = 0, len = newStr.length; i < len; i++) {
    // 获得unicode码
    const ch = newStr.charAt(i)
    // 检查该unicode码是否在处理范围之内,在则返回该码对映汉字的拼音首字母,不在则调用其它函数处理
    arrResult.push(checkCh(ch))
  }
  // 处理arrResult,返回所有可能的拼音首字母串数组
  return mkRslt(arrResult)
}

function checkCh(ch: string) {
  const uni = ch.charCodeAt(0)
  // 如果不在汉字处理范围之内,返回原字符,也可以调用自己的处理函数
  if (uni > 40869 || uni < 19968) {
    return ch // dealWithOthers(ch)
  }
  // 检查是否是多音字,是按多音字处理,不是就直接在strChineseFirstPY字符串中找对应的首字母
  return (oMultiDiff[uni] ? oMultiDiff[uni] : (strChineseFirstPY.charAt(uni - 19968)))
}

function mkRslt(arr: Array<string>) {
  let arrRslt = ['']
  for (let i = 0, len = arr.length; i < len; i++) {
    const str = arr[i]
    const strlen = str.length
    if (strlen == 1) {
      for (let k = 0; k < arrRslt.length; k++) {
        arrRslt[k] += str
      }
    } else {
      const tmpArr = arrRslt.slice(0)
      arrRslt = []
      for (let k = 0; k < strlen; k++) {
        // 复制一个相同的arrRslt
        var tmp = tmpArr.slice(0)
        // 把当前字符str[k]添加到每个元素末尾
        for (let j = 0; j < tmp.length; j++) {
          tmp[j] += str.charAt(k)
        }
        // 把复制并修改后的数组连接到arrRslt上
        arrRslt = arrRslt.concat(tmp)
      }
    }
  }
  return arrRslt
}
// 两端去空格函数
export function trim(str: string) {
  return str.replace(/(^\s*)|(\s*$)/g, '')
}

// let arr = []
interface Contact {
  [key: string]: Array<Staff>
}

// for (let i=0; i<=10; i++) {
//   const person: Staff = {
//     email: Mock.mock('@email'),
//     entId: 'string',
//     joinTime: 111,
//     phone: Mock.mock('@id'),
//     leaderId: Mock.mock('@id'),
//     name: Mock.mock('@cname'),
//     position: 'string',
//     role: Mock.mock({"number|0-6": 0}).number,
//     depId: Mock.mock('@id'),
//     depName: 'string',
//     entName: 'string',
//     id: Mock.mock('@id'),
//     workplace: 'string'
//   }
//   arr.push(person)
// }

export function getContacts(arr: Array<Staff>) {
  if (arr.length === 0) return {}
  const contacts: Contact = {}
  arr.forEach(item => {
    const firstPy = makePy(item.name)[0].slice(0, 1).toUpperCase()
    const test = /[0-9`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]/
    if (test.test(firstPy)) {
      if (typeof contacts['#'] === 'undefined') {
        contacts['#'] = [item]
      } else {
        contacts['#'].push(item)
      }
    } else {
      if (typeof contacts[firstPy] === 'undefined') {
        contacts[firstPy] = [item]
      } else {
        contacts[firstPy].push(item)
      }
    }
  })
  return contacts
}

export function getLeaders(arr: Array<Staff>) {
  if (typeof arr === 'undefined' || arr.length === 0) return []
  return arr.filter((item: Staff) => item.role !== Role.COMMON_MEMBER).sort((a, b) => a.role - b.role)
}

export function uniqueArr(arr: Staff[]) {
  if (typeof arr === 'undefined' || arr.length === 0) return []
  const res = new Map()
  return arr.filter(item => {
    if (!res.has(item.id)) {
      res.set(item.id, true)
      return item
    }
  })
}
