import {
  enforceActionsColumnHeader,
  getFiltersAsQueryParams,
} from '@/helpers/table.helper';
import { TableHeader } from '@/models';

const bmsUiFilters = {
  size: 25,
  offset: 0,
  page: 0,
  search: '1',
  filters: [
    {
      label: 'Origine',
      key: 'fromPath',
      type: 'input',
      value: '/from',
    },
    {
      label: 'Destination',
      key: 'destination',
      type: 'input',
      value: '/you-have-arrived-',
    },
    {
      label: 'Date de création',
      key: 'created',
      type: 'betweenDate',
      inputType: 'date',
      value: null,
      valueFrom: '2024-11-22',
    },
    {
      label: "Nombre d'utilisation",
      key: 'hitCount',
      type: 'betweenNumber',
      inputType: 'number',
      value: null,
      valueFrom: 2,
      valueTo: 5,
    },
    {
      label: 'Date de dernière utilisation',
      key: 'usedAt',
      type: 'betweenDate',
      inputType: 'date',
      value: null,
    },
  ],
  sort: {
    key: null,
    value: 'default',
  },
};
const bmsHeaders = [
  { label: 'Numéro', key: 'id' },
  { label: 'Name', key: 'name' },
  { label: 'Type', key: 'type' },
] as TableHeader[];

describe('test utils table helpers methods', () => {
  describe('getFiltersAsQueryParams', () => {
    it('should convert filters as query params', async () => {
      //@ts-expect-error typage
      expect(getFiltersAsQueryParams(bmsUiFilters)).toStrictEqual({
        createdFrom: '2024-11-22T00:00:00.000Z',
        destination: '/you-have-arrived-',
        fromPath: '/from',
        hitCountFrom: 2,
        hitCountTo: 5,
        page: 0,
        search: '1',
        size: 25,
      });
    });
    it('should be resiliente', async () => {
      //@ts-expect-error test avec object inattendu
      expect(getFiltersAsQueryParams(null)).toStrictEqual({
        page: 0,
        size: 0,
      });
      //@ts-expect-error test avec object inattendu
      expect(getFiltersAsQueryParams(undefined)).toStrictEqual({
        page: 0,
        size: 0,
      });
    });
  });
  describe('enforceActionsColumnHeader', () => {
    it('should not change the input whotout actions column', async () => {
      expect(enforceActionsColumnHeader(bmsHeaders)).toStrictEqual(bmsHeaders);
    });
    it('should center and add default size for actions column', async () => {
      const headers = [...bmsHeaders];
      headers.push({
        label: 'Action',
        key: 'action',
        action: true,
      });
      expect(
        enforceActionsColumnHeader(headers)[headers.length - 1].label,
      ).toStrictEqual('Action');
      expect(
        enforceActionsColumnHeader(headers)[headers.length - 1].width,
      ).toStrictEqual('75px');
      expect(
        enforceActionsColumnHeader(headers)[headers.length - 1].align,
      ).toStrictEqual('center');
    });
    it('should not override your center and width preference', async () => {
      const headers = [...bmsHeaders];
      headers.push({
        label: 'Action',
        key: 'action',
        action: true,
        width: 'doNotTouch',
        align: 'end',
      });
      expect(
        enforceActionsColumnHeader(headers)[headers.length - 1].label,
      ).toStrictEqual('Action');
      expect(
        enforceActionsColumnHeader(headers)[headers.length - 1].width,
      ).toStrictEqual('doNotTouch');
      expect(
        enforceActionsColumnHeader(headers)[headers.length - 1].align,
      ).toStrictEqual('end');
    });
    it('should put actions at the end of the headers', async () => {
      const headers = [...bmsHeaders];
      headers.unshift({
        label: 'Action',
        key: 'action',
        action: true,
      });
      expect(
        enforceActionsColumnHeader(headers)[headers.length - 1].label,
      ).toStrictEqual('Action');
    });
  });
});
