'use strict';

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

var _object_to_mode = require('../modes/object_to_mode');

var ModeHandler = function ModeHandler(_mode, context) {
  // 定义事件存储器
  var handlers = {};
  _object_to_mode.eventKeys.map(function (key) {
    handlers[key] = [];
  });

  var ctx = {
    on: function on(event, selector, fn) {
      if (handlers[event] === undefined) {
        throw new Error('\u7ED8\u56FEDraw\u7C7B\u4E2D\u4E8B\u4EF6\u7C7B\u578B[' + event + ']\u65E0\u6548;');
      }
      handlers[event].push({
        selector: selector,
        fn: fn
      });
    }
  };

  // 定义事件委托
  var delegate = function delegate(eventName, event) {
    var handles = handlers[eventName];
    var iHandle = handles.length;
    while (iHandle--) {
      var handle = handles[iHandle];
      if (handle.selector(event)) {
        var skipRender = handle.fn.call(ctx, event);
        if (!skipRender) {
          context.store.render();
        }
        break;
      }
    }
  };

  return {
    mode: function mode() {
      return _mode;
    },


    render: _mode.render,

    start: function start() {
      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

      // 激活时聚焦地图区域，解决绘图键盘事件默认不激活
      context.container && context.container.focus && context.container.focus();
      _mode.start.call(ctx, options);
    },
    stop: function stop() {
      _mode.stop && _mode.stop();
    },
    trash: function trash() {
      if (_mode.trash) {
        _mode.trash();
        context.store.render();
      }
    },
    combineFeatures: function combineFeatures() {
      _mode.combineFeatures && _mode.combineFeatures();
    },
    uncombineFeatures: function uncombineFeatures() {
      _mode.uncombineFeatures && _mode.uncombineFeatures();
    },
    drag: function drag(e) {
      delegate('drag', e);
    },
    click: function click(e) {
      delegate('click', e);
    },
    mousedown: function mousedown(e) {
      delegate('mousedown', e);
    },
    mousemove: function mousemove(e) {
      delegate('mousemove', e);
    },
    mouseup: function mouseup(e) {
      delegate('mouseup', e);
    },
    mouseover: function mouseover(e) {
      delegate('mouseover', e);
    },
    mouseout: function mouseout(e) {
      delegate('mouseout', e);
    },
    keydown: function keydown(e) {
      delegate('keydown', e);
    },
    keyup: function keyup(e) {
      delegate('keyup', e);
    },
    touchstart: function touchstart(e) {
      delegate('touchstart', e);
    },
    touchmove: function touchmove(e) {
      delegate('touchmove', e);
    },
    touchend: function touchend(e) {
      delegate('touchend', e);
    },
    tap: function tap(e) {
      delegate('tap', e);
    }
  };
}; /**
    * @文件说明: 定义绘图Mode模式的事件委托处理功能
    * @创建人: pjtools@vip.qq.com
    * @创建时间: 2020-05-22 11:20:22
    */

exports['default'] = ModeHandler;