export function setCookie(name, value, exptime) {
  const ctx = location.hostname
  var now = new Date()
  var time = now.getTime()
  var anhour = (exptime + 8) * 3600
  time += anhour * 1000
  now.setTime(time)
  document.cookie = name + '=' + value + ';path=' + ctx + '/;expires=' + now.toUTCString()
}
export function getCookie(name) {
  const cArr = document.cookie.split('; ')
  for (var i = 0; i < cArr.length; i++) {
    let cArr2 = cArr[i].split('=')
    if (cArr2[0] === name) {
      return cArr2[1]
    }
  }
  return ''
}

// 获取边侧栏路径indexPath与点击跳转链接link的mapping
export function indexPathMapping(menus = [], mapping = {}, indexPath = []) {
  menus.forEach((item, index) => {
    let indexTemp = [...indexPath]
    indexTemp.push(index)
    if (item.children && indexPath <= 3) {
      indexPathMapping(item.children, mapping, indexTemp)
    }

    if (item.link) {
      let link = item.link
      mapping[link] = indexTemp
    }
  })
}

export function calcIndexPathByRouter(mapping = {}, route = '') {
  console.log(route)
  console.log('this is mapping %o', mapping)
  const key = Object.keys(mapping).filter(item => route.indexOf(item) > -1)[0]

  console.log('this is key %o', mapping[key])
  return mapping[key]
}

export function calcIndexPathByCurrentRouter(menus = [], currentRoute = '') {
  let indexPathMappingWithLink = {}
  indexPathMapping(menus, indexPathMappingWithLink)
  return calcIndexPathByRouter(indexPathMappingWithLink, currentRoute)
}
