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

import {
  State,
  initialState,
  probeAdapter,
} from './probe.state';

import { Probe } from '@overflow/commons-typescript/model/probe';

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

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

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

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

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

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

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

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

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

    default: {
      return state;
    }
  }
}
