import { createReducer, on } from '@ngrx/store';
import { ToastStore } from '../../interfaces/store/toast.interface';
import { initialState } from './toast.store';
import { toastActions } from './toast.actions';

export const toastReducers = createReducer<ToastStore>(
  initialState,

  on(
    toastActions.set_toasts,
    (state, action): ToastStore => ({ ...state, ...action.toasts }),
  ),
  on(toastActions.reset_toasts, (): ToastStore => initialState),

  on(
    toastActions.show_toast_success,
    (state, action): ToastStore => ({
      ...state,
      success: { ...state.success, ...action.toast },
    }),
  ),
  on(
    toastActions.hide_toast_success,
    (state): ToastStore => ({ ...state, success: initialState.success }),
  ),

  on(
    toastActions.show_toast_warn,
    (state, action): ToastStore => ({
      ...state,
      warn: { ...state.warn, ...action.toast },
    }),
  ),
  on(
    toastActions.hide_toast_warn,
    (state): ToastStore => ({ ...state, warn: initialState.warn }),
  ),

  on(
    toastActions.show_toast_error,
    (state, action): ToastStore => ({
      ...state,
      error: { ...state.error, ...action.toast },
    }),
  ),
  on(
    toastActions.hide_toast_error,
    (state): ToastStore => ({ ...state, error: initialState.error }),
  ),
);
