import { ActionType, AuthActions, EmployerActions, OfferActions } from '../actions';
import { EmployerData } from '../models';

const INITIAL_STATE: EmployerData = {
  applications: [],
  employees: {},
};

export function employerDataReducer(state = INITIAL_STATE, action: ActionType): EmployerData {
  switch (action.type) {
    case AuthActions.ActionKey.LOGOUT_REQUESTED:
      return INITIAL_STATE;
    case EmployerActions.ActionKey.EMPLOYEE_DETAILS_SUCCESS: {
      return {
        ...state,
        employees: {
          ...state.employees,
          [action.user.id]: action.user,
        },
      };
    }
    case OfferActions.ActionKey.EMPLOYER_APPLICATIONS_RECEIVED:
      return {
        ...state,
        applications: action.applications,
      };
    case EmployerActions.ActionKey.EMPLOYEE_LIST_RECEIVED:
      return {
        ...state,
        employees: action.employees.reduce((buffer, item) => ({ ...buffer, [item.id]: item }), {}),
      };
    default:
      return state;
  }
}
