'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.eventKeys = undefined;

exports['default'] = function (modeObject) {
  var modeObjectKeys = Object.keys(modeObject);

  return function (ctx) {
    var startOpts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var state = {};

    var _mode = modeObjectKeys.reduce(function (m, k) {
      m[k] = modeObject[k];
      return m;
    }, new _mode_interface2['default'](ctx));

    var wrapper = function wrapper(eventName) {
      return function (e) {
        return _mode[eventName](state, e);
      };
    };

    return {
      mode: function mode() {
        return _mode;
      },
      start: function start() {
        var _this = this;

        var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

        var modeOptions = options !== null && options !== undefined ? options : startOpts;
        state = _mode.onSetup(modeOptions);
        eventKeys.forEach(function (key) {
          var modeHandler = eventMapper[key];
          var selector = function selector() {
            return false;
          };
          if (modeObject[modeHandler]) {
            selector = function selector() {
              return true;
            };
          }
          _this.on(key, selector, wrapper(modeHandler));
        });
      },
      stop: function stop() {
        _mode.onStop(state);
      },
      trash: function trash() {
        _mode.onTrash(state);
      },
      combineFeatures: function combineFeatures() {
        _mode.onCombineFeatures(state);
      },
      uncombineFeatures: function uncombineFeatures() {
        _mode.onUncombineFeatures(state);
      },
      render: function render(geojson, push) {
        _mode.toDisplayFeatures(state, geojson, push);
      }
    };
  };
};

var _mode_interface = require('./mode_interface');

var _mode_interface2 = _interopRequireDefault(_mode_interface);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var eventMapper = {
  drag: 'onDrag',
  click: 'onClick',
  mousemove: 'onMouseMove',
  mousedown: 'onMouseDown',
  mouseup: 'onMouseUp',
  mouseover: 'onMouseOver',
  mouseout: 'onMouseOut',
  keyup: 'onKeyUp',
  keydown: 'onKeyDown',
  touchstart: 'onTouchStart',
  touchmove: 'onTouchMove',
  touchend: 'onTouchEnd',
  tap: 'onTap'
}; /**
    * @文件说明: 定义绘图模式的对象作用域转换器
    * @创建人: pjtools@vip.qq.com
    * @创建时间: 2020-05-22 09:06:57
    */

var eventKeys = exports.eventKeys = Object.keys(eventMapper);