import md5 from 'js-md5'
import dayjs from 'dayjs'

/**
 * md5 加密
 * @param val string
 * @returns strng
 */
export const fnMd5 = (val: string) => md5(val)

/**
 * 判断对象是否为空
 * @param obj Object
 * @returns boolean
 */
export const isEmptyObject = (obj: object) => Object.keys(obj).length > 0

/**
 * 时间的处理
 * @param date new Date
 * @returns string
 */
export const dayJs = (date?: Date | string) => dayjs(date)

/**
 * 生成一个唯一值
 * @returns string
 */
export const generateRandom = (): string => {
  return Math.random().toString(16).slice(2)
}

/**
 *
 * @param menuList 菜单列表
 * @param result 输出得结果
 * @param parent 子菜单
 * @returns object
 */
export const getAllBreadcrumbList = (menuList: any, result: { [key: string]: any } = {}, parent = []) => {
  for (const item of menuList) {
    result[item.T_permission.trim()] = [...parent, item]
    if (item.Children) getAllBreadcrumbList(item.Children, result, result[item.T_permission])
  }
  return result
}

const get_name = (name: string): string => {
  if (/\//.test(name)) {
    return name.split('/')[1]
  }
  return name
}

/**
 * 将扁平化的数组转换成带有meta component字段的新数组
 * @param menuList []
 */
export const AddRouterMeta = (menuList: any[]) => {
  let reg = /^[a-zA-Z]+$/
  menuList.forEach((menu: any) => {
    if (menu?.Children) {
      AddRouterMeta(menu?.Children)
    }
    menu.path = menu.T_permission
    menu.name = get_name(menu.T_permission)
    menu.meta = {
      icon: reg.test(menu.T_icon) ? menu.T_icon : menu.T_icon.slice(1),
      title: menu.T_name,
      isKeepAlive: true
    }
    menu.component = menu.T_uri
  })
  return menuList
}

/**
 * tree to flat
 * @param menuList []
 */
export const flatMenuListGet = (menuList: any) => {
  const result = []
  for (let menu of menuList) {
    if (menu.Children) {
      let res: any = flatMenuListGet(menu.Children)
      result.push(...res)
    } else {
      result.push({ ...menu })
    }
  }
  return result
}

/**
 * 分钟转小时
 * @param time
 */
export const getFormatDuration = (time: number) => {
  let hour = (time / 60).toString().padStart(2,'0')
  let minute = (time % 60).toString().padStart(2,'0')

  return (parseInt(hour) == 0 ?'':parseInt(hour)+'小时') + (Number(minute)<0?minute.substring(1,3) +'分':Number(minute)==0?'':parseInt(minute)+'分')

  // let hour = 0
  // let minute = 0
  // let result = ''
  // if (time >= 60 || time < 0) {
  //   hour = time / 60
  //   minute = time % 60
  // } else {
  //   result = '' + time + '小时'
  // }
  // if (Math.abs(minute) > 0) {
  //   result = '' + parseInt(minute + '') + '分' + result
  // }
  // if (Math.abs(hour) > 0) {
  //   result = '' + parseInt(hour + '') + '小时' + result
  // }
  // return result
}

interface iconsType {
  [propName: string]: string | number
}

export const icons: iconsType = {
  ue7d1: '\ue7d1',
  ue63e: '\ue63e',
  ue603: '\ue603',
  ue636: '\ue636',
  ue661: '\ue661',
  ue648: '\ue648',
  ue6a1: '\ue6a1',
  ue645: '\ue645',
  ue619: '\ue619',
  ue627: '\ue627',
  ue64c: '\ue64c',
  ue6aa: '\ue6aa',
  ue690: '\ue690',
  ue663: '\ue663',
  ue7cd: '\ue7cd',
  ue831: '\ue831',
  ue7ec: '\ue7ec',
  ue626: '\ue626',
  ue608: '\ue608',
  ue60c: '\ue60c',
  ue607: '\ue607',
  ue72c: '\ue72c',
  ue71f: '\ue71f',
  ue682: '\ue682',
  ue670: '\ue670',
  ue60b: '\ue60b',
  ue88f: '\ue88f',
  ue612: '\ue612',
  ue60a: '\ue60a',
  ue613: '\ue613',
  ue628: '\ue628',
  ue60f: '\ue60f',
  ue7e8: '\ue7e8',
  ue62a: '\ue62a',
  ue63a: '\ue63a',
  ue7d3: '\ue7d3',
  ue6db: '\ue6db',
  ue614: '\ue614',
  ue609: '\ue609',
  ue651: '\ue651',
  ue675: '\ue675',
  ue679: '\ue679'
}
export function debounce<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void {
  let timerId: number | undefined

  return (...args: Parameters<T>) => {
    if (timerId) {
      clearTimeout(timerId)
    }
    timerId = window.setTimeout(() => {
      fn(...args)
      timerId = undefined
    }, delay)
  }
}
