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

interface StatsState {}

export const initialState: StatsState = {};

const reducer = (state: StatsState = initialState, action: StoreAction): StatsState =>
  produce(
    state,
    (draft): StatsState => {
      let name;
      switch (action.type) {
        case actions.get.request.constant:
          handleRequest(draft, action, action.payload.name);
          break;
        case actions.get.result.constant:
          draft[action.payload.name] = draft[action.payload.name] || {};
          draft[action.payload.name].loaded = true;
          draft[action.payload.name].loading = false;
          draft[action.payload.name].data = action.payload.data;
          break;
        case actions.getApollo.result.constant:
          draft[action.payload.name] = draft[action.payload.name] || {};
          draft[action.payload.name].loaded = true;
          draft[action.payload.name].loading = false;
          draft[action.payload.name].data = action.payload.data;
          break;
        case auth.actions.logout.result.constant:
          draft = initialState;
          break;
        default:
      }
    },
  );

export default reducer;
