/* eslint-disable max-len */

import { getDayIdentifier, parseTimestamp } from '../timestamp'
import { isEventHiddenOn, isEventOn, isEventOverlapping, parseEvent } from '../events'

describe('events.ts', () => {
  it('should parse events', () => {
    expect(parseEvent({
      start: '2019-02-13',
      end: '2019-02-14',
    }, 0, 'start', 'end')).toMatchSnapshot()
    expect(parseEvent({
      a: '2019-02-13',
      b: '2019-02-14',
    }, 0, 'a', 'b')).toMatchSnapshot()
    expect(parseEvent({
      start: '2019-02-13',
      end: '2019-02-14',
    }, 1, 'start', 'end')).toMatchSnapshot()
    expect(parseEvent({
      a: '2019-02-13',
      b: '2019-02-14',
    }, 1, 'a', 'b')).toMatchSnapshot()
  })

  it('should parse timed events', () => {
    expect(parseEvent({
      start: '2019-02-13 8:30',
      end: '2019-02-14',
    }, 0, 'start', 'end')).toMatchSnapshot()
  })

  it('should check if event is on', () => {
    const parsed = parseEvent({
      start: '2019-02-13 8:30',
      end: '2019-02-15',
    }, 0, 'start', 'end')

    expect(isEventOn(parsed, getDayIdentifier(parseTimestamp('2019-02-12')!))).toBe(false)
    expect(isEventOn(parsed, getDayIdentifier(parseTimestamp('2019-02-13')!))).toBe(true)
    expect(isEventOn(parsed, getDayIdentifier(parseTimestamp('2019-02-14')!))).toBe(true)
    expect(isEventOn(parsed, getDayIdentifier(parseTimestamp('2019-02-15')!))).toBe(true)
    expect(isEventOn(parsed, getDayIdentifier(parseTimestamp('2019-02-16')!))).toBe(false)
  })

  it('should check if event is hidden if ending but not starting at midnight', () => {
    const parsed = parseEvent({
      start: '2021-05-17 10:30',
      end: '2021-05-18 00:00',
    }, 0, 'start', 'end')

    expect(isEventHiddenOn(parsed, parseTimestamp('2021-05-16')!)).toBe(false)
    expect(isEventHiddenOn(parsed, parseTimestamp('2021-05-17')!)).toBe(false)
    expect(isEventHiddenOn(parsed, parseTimestamp('2021-05-18')!)).toBe(true)
    expect(isEventHiddenOn(parsed, parseTimestamp('2021-05-16')!)).toBe(false)
  })

  it('should check if event is hidden if starting and ending at midnight', () => {
    const parsed = parseEvent({
      start: '2021-05-18 00:00',
      end: '2021-05-18 00:00',
    }, 0, 'start', 'end')

    expect(isEventHiddenOn(parsed, parseTimestamp('2021-05-17')!)).toBe(false)
    expect(isEventHiddenOn(parsed, parseTimestamp('2021-05-18')!)).toBe(false)
    expect(isEventHiddenOn(parsed, parseTimestamp('2021-05-16')!)).toBe(false)
  })

  it('should check if event is overlapping', () => {
    const parsed = parseEvent({
      start: '2019-02-13 8:30',
      end: '2019-02-15',
    }, 0, 'start', 'end')

    expect(isEventOverlapping(parsed, getDayIdentifier(parseTimestamp('2019-02-10')!), getDayIdentifier(parseTimestamp('2019-02-12')!))).toBe(false)
    expect(isEventOverlapping(parsed, getDayIdentifier(parseTimestamp('2019-02-12')!), getDayIdentifier(parseTimestamp('2019-02-18')!))).toBe(true)
    expect(isEventOverlapping(parsed, getDayIdentifier(parseTimestamp('2019-02-16')!), getDayIdentifier(parseTimestamp('2019-02-18')!))).toBe(false)
  })

  it(`should throw an error if start isn't defined`, () => {
    const fn = () => parseEvent({
      end: '2019-02-15',
    }, 0, 'start', 'end')

    expect(fn).toThrow('undefined is not a valid timestamp. It must be a Date, number of milliseconds since Epoch, or a string in the format of YYYY-MM-DD or YYYY-MM-DD hh:mm. Zero-padding is optional and seconds are ignored.')
  })
})
