var _createReducer;
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import { showContextMenu } from "teselagen-react-components";
import without from "lodash/without";
import { createReducer } from "redux-act";
import createAction from "./utils/createMetaAction";

// ------------------------------------
// Actions
// ------------------------------------
export var annotationSelect = createAction("VE_ANNOTATION_SELECT");
export var updateSelectedAnnotation = createAction("VE_ANNOTATION_UPDATE_SELECTED");
export var annotationDeselect = createAction("VE_ANNOTATION_DESELECT");
export var annotationDeselectAll = createAction("VE_ANNOTATION_DESELECT_ALL");
export function replacementLayerRightClicked(_ref, meta) {
  var event = _ref.event,
    annotation = _ref.annotation;
  event.preventDefault();
  event.stopPropagation();
  return function (dispatch /* getState */) {
    var items = [{
      text: "Remove Edit",
      onClick: function onClick() {
        dispatch({
          type: "REPLACEMENT_LAYER_DELETE",
          meta: meta,
          payload: _extends({}, annotation)
        });
      }
    }];
    showContextMenu(items, undefined, event);
  };
}

// ------------------------------------
// Reducer
// ------------------------------------
var startingState = {
  idMap: {},
  idStack: []
};
export default createReducer((_createReducer = {}, _createReducer[annotationSelect] = function (state, payload) {
  var _extends2;
  if (!payload.id) throw new Error("Annotation must have an id!");
  var newState = {
    idMap: _extends({}, state.idMap, (_extends2 = {}, _extends2[payload.id] = payload, _extends2)),
    idStack: [].concat(state.idStack, [payload.id])
  };
  return newState;
}, _createReducer[annotationDeselect] = function (state, payload) {
  var id = payload.id || payload;
  var idMap = _extends({}, state.idMap);
  delete idMap[id];
  var idStack = without(state.idStack, id);
  return {
    idMap: idMap,
    idStack: idStack
  };
}, _createReducer[updateSelectedAnnotation] = function (state, payload) {
  var _extends3;
  var id = payload.id;
  var idMap = _extends({}, state.idMap);
  if (!idMap[id]) {
    return state;
  }
  return {
    idMap: _extends({}, idMap, (_extends3 = {}, _extends3[id] = _extends({}, idMap[id], payload), _extends3)),
    idStack: state.idStack
  };
}, _createReducer[annotationDeselectAll] = function () {
  return startingState;
}, _createReducer), startingState);