var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

import React, { forwardRef, useRef, useImperativeHandle, useEffect, Fragment } from 'react';
import Instance from './instance.js';
import { createPortal } from 'react-dom';

var IndexBar = forwardRef(function (_ref, ref) {
  var portal = _ref.portal,
      overflowContainer = _ref.overflowContainer,
      _ref$indexs = _ref.indexs,
      indexs = _ref$indexs === undefined ? ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'G', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] : _ref$indexs,
      others = _objectWithoutProperties(_ref, ['portal', 'overflowContainer', 'indexs']);

  var refEl = useRef(null);
  var refElTip = useRef(null);
  useImperativeHandle(ref, function () {
    return refEl.current;
  });
  var instance = useRef(null);

  useEffect(function () {
    instance.current = new Instance({
      overflowContainer: overflowContainer || refEl.current.parentNode,
      parent: portal || refEl.current.parentNode,
      bar: refEl.current,
      tooltip: refElTip.current
    });
  }, []); // eslint-disable-line

  var DOM = React.createElement(
    Fragment,
    null,
    React.createElement(
      'div',
      _extends({ ref: refEl }, others, { className: 'indexbar' + (others.className ? ' ' + others.className : '') }),
      indexs.map(function (index, i) {
        return React.createElement(
          'span',
          { className: 'indexbar-button', key: 'btn' + i },
          index
        );
      })
    ),
    React.createElement('div', { ref: refElTip, className: 'indexbar-tooltip' })
  );
  if (portal) {
    return createPortal(DOM, portal);
  }
  return DOM;
});

export default IndexBar;