'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

exports['default'] = getHotkeyManager;

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

var hotkeyManager = void 0;

var HotkeyManager = function () {
  function HotkeyManager(handlers) {
    (0, _classCallCheck3['default'])(this, HotkeyManager);

    this.handlers = handlers || {};
    this.dictionary = [];
  }

  (0, _createClass3['default'])(HotkeyManager, [{
    key: 'addHandlers',
    value: function addHandlers(handlers) {
      var o = (0, _extends3['default'])({}, this.handlers, handlers);
      this.handlers = o;
    }
  }, {
    key: 'deleteHandlers',
    value: function deleteHandlers(handlerKey) {
      if (this.handlers.handlerKey) {
        this.handlers['delete'](handlerKey);
      }
    }
  }, {
    key: 'init',
    value: function init(dictionary) {
      this.dictionary = dictionary;
    }
  }, {
    key: 'clearManger',
    value: function clearManger() {
      this.handlers = {};
      this.dictionary = [];
    }
  }, {
    key: 'emit',
    value: function emit(handlerKey, hotkeyCodeString, event) {
      var hotkeyCode = this.dictionary.find(function (v) {
        return v.hotkey === hotkeyCodeString;
      });
      if (!hotkeyCode) {
        return;
      }
      if (this.handlers[handlerKey]) {
        var func = this.handlers[handlerKey][hotkeyCode.code];
        if (typeof func === 'function') {
          func();
          event.stopPropagation();
          event.preventDefault();
        }
      }
    }
  }]);
  return HotkeyManager;
}();

function getHotkeyManager() {
  if (!hotkeyManager) {
    hotkeyManager = new HotkeyManager();
  }
  return hotkeyManager;
}