import React from 'react';

import mousetrap from 'mousetrap';
import propTypes from 'prop-types';

export default class HandlerMap extends React.Component {
  componentDidMount() {
    const keyHandlerMap = this.props.keyHandlerMap;
    if (keyHandlerMap) {
      const mouse = (this.mousetrap = mousetrap);
      let c = !0;
      let d = !1;
      let e = void 0;
      try {
        for (
          var f, g = keyHandlerMap.entries()[Symbol.iterator]();
          !(c = (f = g.next()).done);
          c = !0
        ) {
          var value = f.value;
          mouse.bind(value[0], value[1]);
        }
      } catch (e) {
        d = !0;
        e = mouse;
      } finally {
        try {
          !c && g.return && g.return();
        } finally {
          if (d) {
            throw e;
          }
        }
      }
    }
  }

  componentWillUnmount() {
      this.mousetrap && this.mousetrap.reset() && (this.mousetrap = null);
  }

  render() {
      return this.props.children
  }
}

HandlerMap.propTypes = {
    keyHandlerMap: propTypes.object,
    children: propTypes.node
}
