import {
  startOfDay, endOfDay, subDays,
  startOfWeek, endOfWeek, subWeeks,
  startOfMonth, endOfMonth, subMonths,
  startOfQuarter, endOfQuarter,
  startOfYear, endOfYear,
  format,
} from 'date-fns'

const fmt = d => format(d, 'yyyy-MM-dd')

export const DATE_PRESETS = [
  {
    key: 'today',
    labelKey: 'dates.today',
    getRange: () => {
      const now = new Date()
      return { from: fmt(startOfDay(now)), to: fmt(endOfDay(now)) }
    },
  },
  {
    key: 'yesterday',
    labelKey: 'dates.yesterday',
    getRange: () => {
      const d = subDays(new Date(), 1)
      return { from: fmt(startOfDay(d)), to: fmt(endOfDay(d)) }
    },
  },
  {
    key: 'thisWeek',
    labelKey: 'dates.thisWeek',
    getRange: () => {
      const now = new Date()
      return { from: fmt(startOfWeek(now, { weekStartsOn: 0 })), to: fmt(endOfWeek(now, { weekStartsOn: 0 })) }
    },
  },
  {
    key: 'lastWeek',
    labelKey: 'dates.lastWeek',
    getRange: () => {
      const d = subWeeks(new Date(), 1)
      return { from: fmt(startOfWeek(d, { weekStartsOn: 0 })), to: fmt(endOfWeek(d, { weekStartsOn: 0 })) }
    },
  },
  {
    key: 'thisMonth',
    labelKey: 'dates.thisMonth',
    getRange: () => {
      const now = new Date()
      return { from: fmt(startOfMonth(now)), to: fmt(endOfMonth(now)) }
    },
  },
  {
    key: 'lastMonth',
    labelKey: 'dates.lastMonth',
    getRange: () => {
      const d = subMonths(new Date(), 1)
      return { from: fmt(startOfMonth(d)), to: fmt(endOfMonth(d)) }
    },
  },
  {
    key: 'thisQuarter',
    labelKey: 'dates.thisQuarter',
    getRange: () => {
      const now = new Date()
      return { from: fmt(startOfQuarter(now)), to: fmt(endOfQuarter(now)) }
    },
  },
  {
    key: 'thisYear',
    labelKey: 'dates.thisYear',
    getRange: () => {
      const now = new Date()
      return { from: fmt(startOfYear(now)), to: fmt(endOfYear(now)) }
    },
  },
]
