import moment, { Moment } from 'moment'

export default function transferTimeStampToMoment(
  startTime: number,
  endTime: number,
) {
  if (startTime && endTime) return [moment(startTime), moment(endTime)]
  else return []
}

// 早上9：00 下午17:00
export function transfterStrToDate(str: string) {
  const arr = str.split(' ')
  if (arr.length === 2) {
    let [start, end] = arr
    start = start.replace('：', ':').replace('早上', '').replace('下午', '')
    end = end.replace('：', ':').replace('早上', '').replace('下午', '')
    const d = new Date()
    start = `${d.getFullYear()} ${d.getMonth() + 1} ${d.getDate()} ${start}`
    end = `${d.getFullYear()} ${d.getMonth() + 1} ${d.getDate()} ${end}`
    const startDate = new Date(start)
    const endDate = new Date(end)
    return [startDate, endDate]
  } else {
    return undefined
  }
}
// 早上9：00 下午17:00
export function transfterStrToDateSafari(str: string) {
  const arr = str.split(' ')
  if (arr.length === 2) {
    let [start, end] = arr
    start = start.replace('：', ':').replace('早上', '').replace('下午', '')
    end = end.replace('：', ':').replace('早上', '').replace('下午', '')
    const d = new Date()
    start = `${d.getFullYear()}/${d.getMonth() + 1}/${d.getDate()} ${start}`
    end = `${d.getFullYear()}/${d.getMonth() + 1}/${d.getDate()} ${end}`
    const startDate = new Date(start)
    const endDate = new Date(end)
    return [startDate, endDate]
  } else {
    return undefined
  }
}

export function getOpeningHoursFromMoment(start: Moment, end: Moment) {
  const h1 = start.hour()
  const h2 = end.hour()
  const c1 = h1 <= 12 ? '早上' : '下午'
  const c2 = h2 <= 12 ? '早上' : '下午'
  return `${c1}${start.format('HH:mm')} ${c2}${end.format('HH:mm')}`
}

export function getWeekendStatusFromArr(arr: string[]) {
  let count = 0
  arr.indexOf('6') !== -1 && (count += 1)
  arr.indexOf('7') !== -1 && (count += 2)
  return count
}

export function formatDate(timeStamp: number) {
  const commonLen = new Date().getTime().toString().length
  if (timeStamp.toString().length === commonLen) {
    return moment(timeStamp).format('MM-DD') // September 14th 2021
  } else {
    return ''
  }
}