function pad2(value: number) {
  return String(value).padStart(2, '0')
}

export function currentMonthValue(date = new Date()) {
  return `${date.getFullYear()}-${pad2(date.getMonth() + 1)}`
}

export function previousMonthValue(month = currentMonthValue()) {
  const [year, monthIndex] = month.split('-').map(Number)
  if (!year || !monthIndex) return currentMonthValue(new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1))
  const date = new Date(year, monthIndex - 2, 1)
  return currentMonthValue(date)
}

export function monthBounds(month: string) {
  if (!/^\d{4}-\d{2}$/.test(month)) return { from: '', to: '' }
  const [year, monthIndex] = month.split('-').map(Number)
  const lastDay = new Date(year, monthIndex, 0).getDate()
  return {
    from: `${month}-01`,
    to: `${month}-${pad2(lastDay)}`,
  }
}

export function monthLabel(month: string) {
  if (!month) return '全部月份'
  const [year, monthIndex] = month.split('-').map(Number)
  if (!year || !monthIndex) return '自定义日期'
  return `${year}年${monthIndex}月`
}
