import React from 'react';
import { BaseComponent } from '@wisdom-utils/components';
import mousetrap from 'mousetrap';
import propTypes from 'prop-types';

/* eslint-disable */
export default class HandlerMap extends BaseComponent {
  componentDidMount() {
    const { keyHandlerMap } = this.props;
    if (keyHandlerMap) {
      // eslint-disable-next-line no-multi-assign
      const mouse = (this.mousetrap = mousetrap);
      let c = !0;
      let d = !1;
      // eslint-disable-next-line no-void
      const e = void 0;
      try {
        for (
          // eslint-disable-next-line no-var,vars-on-top
          var f, g = keyHandlerMap.entries()[Symbol.iterator]();
          !(c = (f = g.next()).done);
          c = !0
        ) {
          const { value } = f;
          mouse.bind(value[0], value[1]);
        }
        // eslint-disable-next-line no-shadow
      } catch (e) {
        d = !0;
        // eslint-disable-next-line no-ex-assign
        e = mouse;
      } finally {
        try {
          // eslint-disable-next-line no-unused-expressions,block-scoped-var
          !c && g.return && g.return();
        } finally {
          if (d) {
            // eslint-disable-next-line no-unsafe-finally
            throw e;
          }
        }
      }
    }
  }

  componentWillUnmount() {
    // eslint-disable-next-line no-unused-expressions
    this.mousetrap && this.mousetrap.reset() && (this.mousetrap = null);
  }

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

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