import { StoreAction } from 'store/utils/actions';
import produce from 'immer';
import {
  handleRequest,
  handleResponse,
  handleUpdateResponse,
  handleAddResponse,
  handleDeleteResponse,
  handleCountResponse,
} from 'store/utils/reducers';
import auth from 'store/auth';
import actions from './actions';

interface GeneralState {
  lists: {} | null;
  categories: {} | null;
  position: {} | null;
}

export const initialState: GeneralState = {
  lists: null,
  categories: null,
};

const treatList = (item): {} => {
  const newItem = { ...item };

  return newItem;
};

const reducer = (state: GeneralState = initialState, action: StoreAction): GeneralState => {
  return produce(
    state,
    (draft): GeneralState => {
      switch (action.type) {
        case actions.setPosition.constant:
          draft.position = action.payload;
          break;
        case actions.equipment.result.constant:
          handleResponse(draft, action, 'equipment', treatList);
          break;
        case actions.feature.result.constant:
          handleResponse(draft, action, 'feature', treatList);
          break;
        // case actions.categories.result.constant:
        //   handleAddResponse(draft, action, 'categories', treatList);
        //   break;
        case auth.actions.logout.result.constant:
          draft.list = null;
          draft.detail = null;
          break;
        default:
      }
    },
  );
};

export default reducer;
