export function resolveFileString(str: string) {
  return str ? str.split(';').map(s => resolveSingleFileString(s)) : []
}

export function resolveSingleFileString(str: string, sign = '|') {
  const [url, name] = str.split(sign)
  return {
    name,
    url: url.startsWith('http') ? url : `${(window as any).GLOBAL_CONFIG.oss}/${url}`,
    origin: url,
  }
}

// 获取完整文件地址
export function getFullUrl(url: string) {
  if (!url)
    return ''
  return url.startsWith('http') ? url : `${(window as any).GLOBAL_CONFIG.oss}/${url}`
}

// 获取不完整文件地址
export function getPartUrl(url: string) {
  if (!url)
    return ''
  return url.startsWith('http') ? url.startsWith((window as any).GLOBAL_CONFIG.oss) ? url.substring((window as any).GLOBAL_CONFIG.oss.length) : Error('oss 地址错误') : url
}

export function getAvatarUrl(id: string) {
  return `${(window as any).GLOBAL_CONFIG.oss}/user/main/user_avatar?user_id=${id}`
}

export function getTime(timestamp: string) {
  if (!timestamp)
    return ''

  const date = timestamp.length === 10 ? new Date(Number(timestamp) * 1000) : new Date(timestamp)

  const Year = `${date.getFullYear()}-`
  const Month = `${date.getMonth() < 9 ? `0${date.getMonth() + 1}` : date.getMonth() + 1}-`

  function format(time: number) {
    return time < 10 ? `0${time}` : time
  }

  const D = `${format(date.getDate())} `
  const h = `${format(date.getHours())}:` // 小时
  const m = `${format(date.getMinutes())}:` // 分钟
  const s = format(date.getSeconds()) // 秒
  return Year + Month + D + h + m + s
}

export function getFileType(str: string) {
  if (str) {
    str = str += ''.toLocaleLowerCase()
    const arr = str.split('.')
    const ext = arr[arr.length - 1].toLowerCase()
    if (['pdf'].includes(ext))
      return 'pdf'
    else if (['mp4'].includes(ext))
      return 'video'
    else if (['ppt', 'pptx', 'xls', 'xlsx', 'doc', 'docx'].includes(ext))
      return 'office'
    else if (['mp3'].includes(ext))
      return 'audio'
    else if (['png', 'jpg', 'jpeg', 'webp'].includes(ext))
      return 'image'
    return 'other'
  }
  return 'other'
}

// 手机号码带*处理
export function dealPhone(str: string) {
  if (str) {
    const reg = /(\d{3})\d*(\d{4})/
    const phone = str.replace(reg, '$1****$2')
    return phone
  }
  else {
    return ''
  }
}
