import * as React from 'react'
import { ChevronLeft, ChevronRight } from 'lucide-react'
import { DayPicker } from 'react-day-picker'

import { cn } from '@/lib/utils'
import { buttonVariants } from './button'

export type CalendarProps = React.ComponentProps<typeof DayPicker>

function Calendar({
  className,
  classNames,
  showOutsideDays = true,
  ...props
}: CalendarProps) {
  return (
    <DayPicker
      showOutsideDays={showOutsideDays}
      className={cn('p-1.5', className)}
      classNames={{
        months: 'flex flex-col sm:flex-row gap-2',
        month: 'flex flex-col gap-1.5',
        caption: 'flex items-center justify-between px-0.5 pt-0.5',
        caption_label: 'text-xs font-medium text-text',
        caption_dropdowns: 'flex items-center justify-center gap-1.5',
        dropdown_month:
          'relative inline-flex items-center rounded px-0.5 text-xs font-medium text-text hover:bg-surface-2',
        dropdown_year:
          'relative inline-flex items-center rounded px-0.5 text-xs font-medium text-text hover:bg-surface-2',
        dropdown:
          'absolute inset-0 z-10 cursor-pointer appearance-none bg-transparent opacity-0',
        dropdown_icon: 'ml-0.5 h-3 w-3 opacity-60',
        vhidden:
          '!absolute !-m-px !h-px !w-px !overflow-hidden !whitespace-nowrap !border-0 !p-0 ![clip:rect(0,0,0,0)]',
        nav: 'flex items-center gap-0.5',
        nav_button: cn(
          buttonVariants({ variant: 'ghost' }),
          'h-5 w-5 p-0 text-text-muted hover:text-text',
        ),
        nav_button_previous: '',
        nav_button_next: '',
        table: 'w-full border-collapse',
        head_row: 'flex',
        head_cell:
          'text-text-muted rounded w-6 font-normal text-[9px] uppercase tracking-wide',
        row: 'flex w-full mt-0.5',
        cell: cn(
          'relative p-0 text-center text-xs focus-within:relative focus-within:z-20',
          '[&:has([aria-selected])]:bg-surface-2 [&:has([aria-selected].day-outside)]:bg-surface-2/50',
        ),
        day: cn(
          buttonVariants({ variant: 'ghost' }),
          'h-6 w-6 p-0 text-xs font-normal aria-selected:opacity-100',
        ),
        day_range_end: 'day-range-end',
        day_selected:
          'bg-primary text-bg hover:bg-primary hover:text-bg focus:bg-primary focus:text-bg',
        day_today: 'text-star',
        day_outside:
          'day-outside text-text-faint aria-selected:bg-surface-2/50 aria-selected:text-text-faint',
        day_disabled: 'text-text-faint opacity-50',
        day_range_middle:
          'aria-selected:bg-surface-2 aria-selected:text-text',
        day_hidden: 'invisible',
        ...classNames,
      }}
      components={{
        IconLeft: () => <ChevronLeft className="h-4 w-4" />,
        IconRight: () => <ChevronRight className="h-4 w-4" />,
      }}
      {...props}
    />
  )
}
Calendar.displayName = 'Calendar'

export { Calendar }
