import { uuid } from '@vincent_za/utils';
import { ADD_UNDO, REMOVE_UNDO } from './actions';

const INITIAL_STATE = {
  undos: [],
};

export default (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case ADD_UNDO: {
      const { title, undoCall, params } = action;
      return {
        ...state,
        undos: [
          ...state.undos,
          {
            id: uuid(),
            title,
            undoCall,
            params,
          },
        ]
      };
    }

    case REMOVE_UNDO: {
      const { id } = action;
      return {
        ...state,
        undos: state.undos.filter(undo => undo.id !== id),
      };
    }

    default: {
      return state;
    }
  }
};
