import moment from 'moment-jalaali';
import { ADD_ERROR, REMOVE_ERROR } from '../constants/errors';

function data(state = [], action) {
  if (action.type === ADD_ERROR) {
    const id = `${moment() - moment().startOf('day')}-${Math.floor(Math.random() * 1e8)}`;
    return [...state, { id, ...action.payload }];
  }
  if (action.type === REMOVE_ERROR) return state.filter(error => error.id !== action.payload.id);
  return state;
}

export default data;
