import { ActionType, Actions } from '../../entity/sensor';
import {
  State,
  initialState,
  sensorListContainerAdapter,
} from './sensor-list.state';

export function reducer(state = initialState, action: Actions): State {
  switch (action.type) {
    case ActionType.ReadAllByDomainID: {
      return {
        ...state,
        pending: true,
      };
    }

    case ActionType.ReadAllByDomainIDSuccess: {
      return sensorListContainerAdapter.setAll(action.payload.content, { ...state, page: action.payload, pending: false });
    }

    case ActionType.ReadAllByDomainIDFailure: {
      return sensorListContainerAdapter.setError(action.payload, { ...state, page: null, pending: false });
    }

    case ActionType.Read: {
      return {
        ...state,
        error: null,
      };
    }

    case ActionType.ReadSuccess: {
      return sensorListContainerAdapter.setOne(action.payload, { ...state, page: null, pending: false });
    }

    case ActionType.ReadFailure: {
      return sensorListContainerAdapter.setError(action.payload, { ...state, pending: false });
    }

    case ActionType.Modify: {
      return {
        ...state,
        error: null,
      };
    }

    case ActionType.ModifySuccess: {
      return sensorListContainerAdapter.upsertOne(action.payload, state);
    }

    case ActionType.ModifyFailure: {
      return sensorListContainerAdapter.setError(action.payload, state);
    }

    default:
      return state;
  }
}
