export default {
  name: "basicToolbar",

  getReducer: () => {
    const initialData = {
      activeTool: null,
      open: false,
    };

    return (state = initialData, { type, payload }) => {
      switch (type) {
        case "BASIC_TOOL_ACTIVATE":
        case "BASIC_TOOL_DEACTIVATE":
          return Object.assign({}, state, payload);
        default:
          return state;
      }
    };
  },

  doBasicToolbarActivate: ({ activeTool }) => ({ dispatch }) => {
    dispatch({
      type: "BASIC_TOOL_ACTIVATE",
      payload: {
        activeTool: activeTool,
        open: true,
      },
    });
  },
  doBasicToolbarToggle: (activeTool, newTool) => ({ store }) => {
    if (activeTool !== newTool) {
      store.doBasicToolbarDeactivate();
      store.doBasicToolbarActivate({
        activeTool: newTool,
      });
    } else {
      store.doBasicToolbarDeactivate();
    }
  },

  doBasicToolbarDeactivate: () => ({ dispatch }) => {
    dispatch({
      type: "BASIC_TOOL_DEACTIVATE",
      payload: {
        activeTool: null,
        open: false,
      },
    });
  },

  selectBasicToolbarActiveTool: (state) => {
    return state.basicToolbar.activeTool;
  },
  selectBasicToolbarOpen: (state) => {
    return state.basicToolbar.open;
  },
};
