import { DateTime, Settings } from 'luxon';
import { describe, expect, it } from 'vitest';
import { FilterValues } from '../Filters.model';
import { createDateRangeFilterValidators } from './DateRangeFilterValidator';

interface DateRangeFilterValidatorTestData {
  created: any;
}

const defaultFilters: FilterValues<DateRangeFilterValidatorTestData> = {
  created: [
    DateTime.local().toISO(),
    DateTime.local().plus({ months: 1 }).toISO(),
  ],
};

describe('createDateRangeFilterValidators', () => {
  Settings.defaultLocale = navigator.language;

  it('should return two functions', () => {
    const result =
      createDateRangeFilterValidators<DateRangeFilterValidatorTestData>();
    expect(Array.isArray(result)).toBeTruthy();
    expect(result).toHaveLength(2);
  });

  it('should validate From and To date correctly', () => {
    const [createFromValidator, createToValidator] =
      createDateRangeFilterValidators<DateRangeFilterValidatorTestData>();

    expect(
      createFromValidator('created')(
        DateTime.local().minus({ months: 1 }).toISO(),
        defaultFilters,
      ),
    ).toBeUndefined();

    expect(
      createFromValidator('created')(
        DateTime.local().plus({ months: 2 }).toISO(),
        defaultFilters,
      ),
    ).toBeDefined();

    expect(
      createToValidator('created')(
        DateTime.local().plus({ months: 2 }).toISO(),
        defaultFilters,
      ),
    ).toBeUndefined();

    expect(
      createToValidator('created')(
        DateTime.local().minus({ months: 3 }).toISO(),
        defaultFilters,
      ),
    ).toBeDefined();
  });

  it('should accept and apply options parameters', () => {
    const [createFromValidator, createToValidator] =
      createDateRangeFilterValidators<DateRangeFilterValidatorTestData>({
        fromDateIndex: 1,
        toDateIndex: 0,
      });

    const filters: FilterValues<DateRangeFilterValidatorTestData> = {
      created: [
        DateTime.local().plus({ months: 1 }).toISO(),
        DateTime.local().toISO(),
      ],
    };

    expect(
      createFromValidator('created')(
        DateTime.local().minus({ months: 1 }).toISO(),
        filters,
      ),
    ).toBeUndefined();

    expect(
      createToValidator('created')(
        DateTime.local().plus({ months: 2 }).toISO(),
        filters,
      ),
    ).toBeUndefined();
  });
});
