import { atom, useAtom } from 'jotai';
// import { formatDate } from '../shared/utlis';
import { format } from 'date-fns';

const date = atom(new Date());
export const usedate = () => {
  return useAtom(date);
};

interface Filter {
  field: string;
  op: string;
  value: any;
}

interface Pagination {
  page: number;
  per_page: number;
}

interface FilterState {
  'x-filters': Filter[];
  'x-pagination': Pagination;
}
// let todayDate = new Date();
const initialState: FilterState = {
  'x-filters': [{ field: 'isdeleted', op: '=', value: 0 }],
  'x-pagination': { page: 1, per_page: 50 },
};

const filterReducer = (state: FilterState, action: any): FilterState => {
  switch (action.type) {
    case 'SET_DATE_FILTER':
      return {
        ...state,
        'x-filters': [
          ...state['x-filters'].filter(
            f => f.field !== 'action_date' && f.field !== 'status'
          ),
          {
            field: 'action_date',
            op: '>=',
            value: `${format(
              new Date(action.payload),
              'yyyy-MM-dd'
            )}T00:00:00+00:00`,
          },
          {
            field: 'action_date',
            op: '<=',
            value: `${format(
              new Date(action.payload),
              'yyyy-MM-dd'
            )}T23:59:59+00:00`,
          },
          { field: 'status', op: 'count', value: '' },
        ],
      };

    case 'SET_CLIENT_FILTER':
      return {
        ...state,
        'x-filters': [
          ...state['x-filters'].filter(f => f.field !== 'customer_id'),
          {
            field: 'customer_id',
            op: '=',
            value: action.payload,
          },
        ],
      };

    case 'SET_ACTIONS_FILTER':
      return {
        ...state,
        'x-filters': [
          ...state['x-filters'].filter(
            f => f.field !== 'product_return_action_id'
          ),
          {
            field: 'product_return_action_id',
            op: '=',
            value: action.payload,
          },
        ],
      };

    case 'SET_LOCATION_FILTER':
      return {
        ...state,
        'x-filters': [
          ...state['x-filters'].filter(f => f.field !== 'return_location'),
          {
            field: 'return_location',
            op: 'in',
            value: action.payload,
          },
        ],
      };

    case 'CLEAR_CLIENT':
      return {
        ...state,
        'x-filters': state['x-filters'].filter(f => f.field !== 'customer_id'),
      };

    case 'CLEAR_ACTION':
      return {
        ...state,
        'x-filters': state['x-filters'].filter(
          f => f.field !== 'product_return_action_id'
        ),
      };

    case 'CLEAR_LOC':
      return {
        ...state,
        'x-filters': state['x-filters'].filter(
          f => f.field !== 'return_location'
        ),
      };
    case 'CLEAR_FILTERS':
      return {
        ...state,
      };

    case 'CLEAR_ALL_FILTER':
      return {
        ...state,
      };

    case 'SET_PAGE':
      return {
        ...state,
        'x-pagination': {
          ...state['x-pagination'],
          page: action.payload,
        },
      };

    case 'SET_PERPAGE':
      return {
        ...state,
        'x-pagination': {
          ...state['x-pagination'],
          per_page: action.payload,
        },
      };
    default:
      return state;
  }
};

const filtersStateAtom = atom(initialState);

export const filtersReducerAtom = atom(
  get => get(filtersStateAtom),
  (get, set, action) =>
    set(filtersStateAtom, filterReducer(get(filtersStateAtom), action))
);
