import '@testing-library/jest-dom'
import { render } from '@testing-library/react'

import { PktCalendar, IPktCalendar } from './Calendar'

const createCalendar = (props: Partial<IPktCalendar> = {}) => {
  return render(<PktCalendar {...props} />)
}

describe('PktCalendar', () => {
  describe('Date range functionality', () => {
    test('handles earliest date property correctly', () => {
      const { container } = createCalendar({
        earliest: '2024-01-15',
        currentmonth: '2024-01-01',
      })

      // Check that dates before earliest are disabled (dates 1-14 should be disabled)
      const disabledDates = container.querySelectorAll('.pkt-calendar__date--disabled')
      expect(disabledDates.length).toBeGreaterThan(0)
    })

    test('handles latest date property correctly', () => {
      const { container } = createCalendar({ latest: '2024-12-15' })

      // Should render without errors
      expect(container.querySelector('.pkt-calendar')).toBeInTheDocument()
    })

    test('restricts selectable dates between earliest and latest', () => {
      const { container } = createCalendar({
        earliest: '2024-06-10',
        latest: '2024-06-20',
        currentmonth: '2024-06-01',
      })

      const enabledDates = container.querySelectorAll(
        '.pkt-calendar__date:not(.pkt-calendar__date--disabled)',
      )
      const disabledDates = container.querySelectorAll('.pkt-calendar__date--disabled')

      expect(enabledDates.length).toBeGreaterThan(0)
      expect(disabledDates.length).toBeGreaterThan(0)
    })
  })

  describe('Date exclusion functionality', () => {
    test('excludes specific dates correctly', () => {
      const { container } = createCalendar({
        excludedates: ['2024-06-15', '2024-06-20'],
        currentmonth: '2024-06-01',
      })

      // The excluded dates should be disabled
      const date15 = container.querySelector('button[data-date="2024-06-15"]')
      const date20 = container.querySelector('button[data-date="2024-06-20"]')
      expect(date15).toHaveClass('pkt-calendar__date--disabled')
      expect(date20).toHaveClass('pkt-calendar__date--disabled')
    })

    test('excludes weekdays correctly', () => {
      const { container } = createCalendar({ excludeweekdays: ['0', '6'] })

      // Check that weekend dates are disabled
      const disabledDates = container.querySelectorAll('.pkt-calendar__date--disabled')
      expect(disabledDates.length).toBeGreaterThan(0)
    })

    test('excludes both specific dates and weekdays', () => {
      const { container } = createCalendar({
        excludedates: ['2024-06-15'],
        excludeweekdays: ['0', '6'],
        currentmonth: '2024-06-01',
      })

      // Both specific dates and weekends should be disabled
      const disabledDates = container.querySelectorAll('.pkt-calendar__date--disabled')
      expect(disabledDates.length).toBeGreaterThan(0)

      const date15 = container.querySelector('button[data-date="2024-06-15"]')
      expect(date15).toHaveClass('pkt-calendar__date--disabled')
    })
  })
})
