"use strict";

exports.__esModule = true;
exports["default"] = exports.annotationSelect = exports.annotationDeselectAll = exports.annotationDeselect = void 0;
exports.replacementLayerRightClicked = replacementLayerRightClicked;
exports.updateSelectedAnnotation = void 0;
var _teselagenReactComponents = require("teselagen-react-components");
var _without = _interopRequireDefault(require("lodash/without"));
var _reduxAct = require("redux-act");
var _createMetaAction = _interopRequireDefault(require("./utils/createMetaAction"));
var _createReducer;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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); }
// ------------------------------------
// Actions
// ------------------------------------
var annotationSelect = (0, _createMetaAction["default"])("VE_ANNOTATION_SELECT");
exports.annotationSelect = annotationSelect;
var updateSelectedAnnotation = (0, _createMetaAction["default"])("VE_ANNOTATION_UPDATE_SELECTED");
exports.updateSelectedAnnotation = updateSelectedAnnotation;
var annotationDeselect = (0, _createMetaAction["default"])("VE_ANNOTATION_DESELECT");
exports.annotationDeselect = annotationDeselect;
var annotationDeselectAll = (0, _createMetaAction["default"])("VE_ANNOTATION_DESELECT_ALL");
exports.annotationDeselectAll = annotationDeselectAll;
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)
        });
      }
    }];
    (0, _teselagenReactComponents.showContextMenu)(items, undefined, event);
  };
}

// ------------------------------------
// Reducer
// ------------------------------------
var startingState = {
  idMap: {},
  idStack: []
};
var _default = (0, _reduxAct.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 = (0, _without["default"])(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);
exports["default"] = _default;