import {
  ActionType,
  Actions,
} from './sensor.action';

import {
  State,
  initialState,
  sensorAdapter,
} from './sensor.state';

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

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

    case ActionType.ReadAllByDomainIDFailure: {
      return sensorAdapter.setError(action.payload, state);
    }

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

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

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

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

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

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

    default: {
      return state;
    }
  }
}
