"use strict";

exports.__esModule = true;
exports.default = void 0;
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _buttonLinkIcon = _interopRequireDefault(require("../../atom/button-link-icon"));
var _buttonMenu = _interopRequireDefault(require("../../atom/button-menu"));
var _types = _interopRequireDefault(require("./types"));
var _style = _interopRequireDefault(require("./style.css"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const BulletPointMenuButton = props => {
  const {
    disabled = false,
    buttonAriaLabel,
    menuAriaLabel,
    onClick = _noop2.default,
    buttons,
    menuButtonClassName,
    isBulkMenu,
    'data-name': dataName = 'bullet-point-button'
  } = props;
  const [visible, setVisible] = (0, _react.useState)(false);
  const wrapperRef = (0, _react.useRef)(null);
  const handleOnClick = (0, _react.useCallback)(event => {
    event.stopPropagation();
    event.preventDefault();
    onClick();
    setVisible(!visible);
  }, [onClick, visible]);
  (0, _react.useEffect)(() => {
    const handleClick = clickEvent => {
      if (wrapperRef && wrapperRef.current && !wrapperRef.current.contains(clickEvent.target)) {
        setVisible(false);
      }
    };
    if (visible) {
      document.addEventListener('click', handleClick);
      document.addEventListener('touchstart', handleClick);
    } else {
      document.removeEventListener('click', handleClick);
      document.removeEventListener('touchstart', handleClick);
    }
    return () => {
      document.removeEventListener('click', handleClick);
      document.removeEventListener('touchstart', handleClick);
    };
  }, [visible]);
  const menuProps = {
    'data-name': 'button-menu',
    buttons
  };
  const menuStyle = (0, _classnames.default)(_style.default.bulletPointMenu, isBulkMenu && _style.default.bulkBulletPointMenu, _style.default.visible);
  const menu = visible ? /*#__PURE__*/_react.default.createElement("div", {
    className: menuStyle,
    "data-name": "menu-wrapper",
    "aria-label": menuAriaLabel,
    ref: wrapperRef
  }, /*#__PURE__*/_react.default.createElement(_buttonMenu.default, menuProps)) : null;
  const bulletPointButtonProps = {
    size: 'default',
    'aria-label': buttonAriaLabel,
    'data-name': dataName,
    icon: 'bullet-point',
    onClick: handleOnClick,
    disabled,
    className: menuButtonClassName
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.bulletPointWrapper,
    "data-name": "bullet-point-wrapper"
  }, /*#__PURE__*/_react.default.createElement(_buttonLinkIcon.default, bulletPointButtonProps), menu);
};
BulletPointMenuButton.propTypes = process.env.NODE_ENV !== "production" ? _types.default : {};
var _default = exports.default = BulletPointMenuButton;
//# sourceMappingURL=index.js.map