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

interface WorkspaceState {
  list: {} | null;
  detail: {} | null;
  mine: {} | null;
  currentSearch: {} | null;
}

export const initialState: WorkspaceState = {
  list: null,
  detail: null,
  favorite: null,
  mine: null,
  currentSearch: {},
};

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

  if (newItem.featureIds) {
    newItem.features = convertArrayToObject(newItem.featureIds);
  }
  if (newItem.equipmentIds) {
    newItem.equipments = convertArrayToObject(newItem.equipmentIds);
  }

  return newItem;
};

const reducer = (state: WorkspaceState = initialState, action: StoreAction): WorkspaceState => {
  return produce(
    state,
    (draft): WorkspaceState => {
      switch (action.type) {
        // search
        case actions.highlight.constant:
          draft.highlight = action.payload;
          break;
        case actions.list.request.constant:
          draft.currentSearch = action.payload.data;
          handleRequest(draft, action, 'list', true);
          break;
        case actions.list.reset.constant:
          handleReset(draft, action, 'list');
          break;
        case actions.list.result.constant:
          handleCountResponse(
            draft,
            action,
            'list',
            treatWorkspace,
            'workspacesSearchMany',
            'workspacesGetCount',
          );
          break;
        // handling
        case actions.update.result.constant:
          handleUpdateResponse(draft, action, 'mine', treatWorkspace);
          break;
        case actions.add.result.constant:
          handleAddResponse(draft, action, 'mine', treatWorkspace);
          break;
        case actions.delete.result.constant:
          handleDeleteResponse(draft, action, 'mine');
          break;
        case actions.mine.request.constant:
          handleRequest(draft, action, 'mine', true);
          break;
        case actions.mine.result.constant:
          handleResponse(draft, action, 'mine', treatWorkspace);
          handleCountResponse(
            draft,
            action,
            'mine',
            treatWorkspace,
            'workspacesGetMine',
            'workspacesGetMineCount',
          );
          break;
        case actions.detail.request.constant:
          handleRequest(draft, action, 'detail', true);
          break;
        case actions.detail.result.constant:
          handleResponse(draft, action, 'detail', treatWorkspace);
          break;
        case auth.actions.logout.result.constant:
          draft.list = null;
          draft.detail = null;
          draft.mine = null;
          break;
        default:
      }
    },
  );
};

export default reducer;
