import { describe, test, it, expect } from 'vitest'
import DateUtils, {supportedLocales} from "~/services/utils/dateUtils";

describe('format', () => {
    test('simple formatting', () => {
      const input = new Date(2020, 4, 12)

      expect(DateUtils.format(input, 'y-MM-dd')).toEqual('2020-05-12')
    })
})

describe('formatDatesAndConcat', () => {
    test('simple array and default sep', () => {
      const input = [
          new Date(2023, 0, 10),
          new Date(2023, 0, 11),
          new Date(2023, 0, 12)
      ]

      const result = DateUtils.formatAndConcat(input, 'dd/MM/y')

      expect(result).toEqual('10/01/2023 - 11/01/2023 - 12/01/2023')
    })

    test('single date and default sep', () => {
      const input = new Date(2023, 0, 10)

      const result = DateUtils.formatAndConcat(input, 'dd/MM/y')

      expect(result).toEqual('10/01/2023')
    })

    test('simple array with other format and custom sep', () => {
      const input = [
          new Date(2023, 0, 10),
          new Date(2023, 0, 11),
          new Date(2023, 0, 12)
      ]

      const result = DateUtils.formatAndConcat(input, 'yMMdd', '|')

      expect(result).toEqual('20230110|20230111|20230112')
    })

    test ('empty array', () => {
        expect(DateUtils.formatAndConcat([], 'dd-MM-y')).toEqual('')
    })
})

describe('sortDate', () => {
    test('sort', () => {
        test('simple array', () => {
            const date1 = new Date(2023, 0, 10)
            const date2 = new Date(2023, 0, 14)
            const date3 = new Date(2023, 0, 12)

            const input = [date1, date2, date3]

            const result = DateUtils.sort(input)

            expect(result).toEqual([date1, date3, date2])
        })

        test('simple array reverse', () => {
            const date1 = new Date(2023, 0, 10)
            const date2 = new Date(2023, 0, 14)
            const date3 = new Date(2023, 0, 12)

            const input = [date1, date2, date3]

            const result = DateUtils.sort(input)

            expect(result).toEqual([date2, date3, date1])
        })
    })
})

describe('getFnsLocale', () => {
    test('standard call', () => {
        expect(DateUtils.getFnsLocale(supportedLocales.FR).code).toEqual('fr')
        expect(DateUtils.getFnsLocale(supportedLocales.EN).code).toEqual('en-US')
    })

    test('unsupported locale', () => {
        // @ts-ignore
        expect(DateUtils.getFnsLocale('xx').code).toEqual('fr')
    })
})

describe('getShortFormatPattern', () => {
    test('standard call', () => {
        expect(DateUtils.getShortFormatPattern(supportedLocales.FR)).toEqual('dd/MM/yyyy')
        expect(DateUtils.getShortFormatPattern(supportedLocales.EN)).toEqual('MM/dd/yyyy')
    })

    test('unsupported locale', () => {
        // @ts-ignore
        expect(DateUtils.getShortFormatPattern('xx')).toEqual('dd/MM/yyyy')
    })
})

describe('getFormatPattern', () => {
    test('standard call', () => {
        expect(DateUtils.getFormatPattern(supportedLocales.FR)).toEqual('dd/MM/yyyy HH:mm')
        expect(DateUtils.getFormatPattern(supportedLocales.EN)).toEqual('MM/dd/yyyy HH:mm')
    })

    test('unsupported locale', () => {
        // @ts-ignore
        expect(DateUtils.getFormatPattern('xx')).toEqual('dd/MM/yyyy HH:mm')
    })
})

describe('formatIsoShortDate', () => {
    test('standard call', () => {
        expect(DateUtils.formatIsoShortDate(new Date(2023, 0, 10))).toEqual('2023-01-10')
        expect(DateUtils.formatIsoShortDate(new Date(2023, 11, 20))).toEqual('2023-12-20')
    })
})


