import {
  ReadAllByDomain,
  ReadAllByDomainFailure,
  ReadAllByDomainSuccess,
  Accept,
  AcceptSuccess,
  AcceptFailure,
  Deny,
  DenySuccess,
  DenyFailure,
  ActionType,
  Actions,
} from './noauth-probe.action';

import {
  State,
  initialState,
  adapter,
} from './noauth-probe.state';

import { NoAuthProbe } from '@overflow/commons-typescript/model/noauth';

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

    case ActionType.ReadAllByDomainSuccess: {
      return adapter.addAll(action.payload, {
        ...state,
        error: null,
        pending: false,
      });
    }

    case ActionType.ReadAllByDomainFailure: {
      return adapter.removeAll({
        ...state,
        error: action.payload,
        pending: false,
      });
    }

    case ActionType.Accept: {
      return {
        ...state,
        error: null,
        pending: true,
      };
    }

    case ActionType.AcceptSuccess: {
      const s = adapter.removeAll(state);
      return adapter.addAll(action.payload, {
        ...s,
        error: null,
        pending: false,
      });
    }

    case ActionType.AcceptFailure: {
      return {
        ...state,
        error: action.payload,
        pending: false,
      };
    }

    case ActionType.Deny: {
      return {
        ...state,
        error: null,
        pending: true,
      };
    }

    case ActionType.DenySuccess: {
      const s = adapter.removeAll(state);
      return adapter.addAll(action.payload, {
        ...s,
        error: null,
        pending: false,
      });
    }

    case ActionType.DenyFailure: {
      return {
        ...state,
        error: action.payload,
        pending: false,
      };
    }
    case ActionType.OnConnect: {
      return {
        ...state,
      };
    }
    case ActionType.OnDisconnect: {
      return {
        ...state,
      };
    }
    default: {
      return state;
    }
  }
}
