import storage from './storage'

export function mapToTree (map) {
  const list = []
  for (const k in map) {
    if (map[k].pid === 0) {
      list.push(map[k])
    } else {
      map[map[k].pid].children ??= []
      map[map[k].pid].children.push(map[k])
    }
  }
  return list
}

/**
 * 上传文件时 accept 的枚举类
 */
export const ACCEPT_MAP = {
  image: { icon: 'i-tupiao', color: '#ff6a4d', name: '图片文件', accept: 'image/*' },
  audio: { icon: 'i-yinyue', color: '#9660f5', name: '音频文件', accept: 'audio/*' },
  video: { icon: 'i-shipin', color: '#55d1e0', name: '视频文件', accept: 'video/*' },
  ppt: { icon: 'i-ppt', color: '#ff9357', name: '演示文稿', accept: 'application/vnd.ms-powerpoint' },
  pptx: { icon: 'i-pptx', color: '#ff9357', name: '演示文稿', accept: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' },
  xls: { icon: 'i-xls', color: '#28c17a', name: '表格文件', accept: 'application/vnd.ms-excel' },
  xlsx: { icon: 'i-xlsx', color: '#28c17a', name: '表格文件', accept: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' },
  pdf: { icon: 'i-pdf', color: '#ff738e', name: '文本文件', accept: 'application/pdf' },
  doc: { icon: 'i-doc', color: '#39afff', name: '文本文件', accept: 'application/msword' },
  docx: { icon: 'i-docx', color: '#39afff', name: '文本文件', accept: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' },
  folder: { icon: 'i-wenjianjia', color: '#ffd94a', name: '文件夹', accept: '' }
}

export function getAcceptType (accept) {
  if (!accept) return

  for (const type in ACCEPT_MAP) {
    if (accept.startsWith(ACCEPT_MAP[type].accept.replace(/\*$/, ''))) return type
  }
}

/**
 * 颜色生成器
 *
 * @param {string} value
 * @returns
 */
export function colorGenerator (value) {
  if (!value) return
  return Number(value.split('').reduce((n, s) => n + s.charCodeAt(), '')).toString(16).replace(/.*(.{6})$/, '#$1')
}

/**
 * 上传文件工具类
 *
 * @param {Object} options
 * @param {[keyof ACCEPT_MAP]} options.accepts 指定可选的文件类型
 * @param {boolean} options.multiple 是否允许用户选择多个文件
 * @returns {Promise}
 */
export function getDistFile ({ accepts = [], multiple = false } = {}) {
  if (!getDistFile.el) { // 如果没有 Input Dom对象，则创建
    getDistFile.el = document.createElement('input')
    getDistFile.el.type = 'file'
  }
  return new Promise((resolve) => {
    getDistFile.el.setAttribute(
      'accept',
      (accepts?.length ? accepts : Object.keys(ACCEPT_MAP))
        .map(k => ACCEPT_MAP[k].accept).join(' , ')
    )
    getDistFile.el.setAttribute('multiple', multiple)

    getDistFile.el.onchange = () => {
      resolve([...getDistFile.el.files])
      getDistFile.el.value = null
    }
    getDistFile.el.click()
  })
}

/**
 * 下载
 *
 * @param {string} src 文件地址
 * @param {string} name 文件下载名
 * @returns
 */
export async function download (src, name) {
  if (!src) return
  const href = await fetch(src).then(res => {
    if (res.status === 200) return res.arrayBuffer()
    throw Error('文件下载出错')
  })

  if (!download.el) {
    download.el = document.createElement('a')
  }

  download.el.download = name || ''
  download.el.href = URL.createObjectURL(new Blob([href]))
  download.el.click()
}

/**
 * 本地存储 localStorage
 */
export const lst = storage(localStorage)

/**
  * 本地存储 sessionStorage
  */
export const sst = storage(sessionStorage)
