import _noop from "lodash/fp/noop";
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import React, { useCallback, useEffect, useState, useRef } from 'react';
import classnames from 'classnames';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import ButtonLink from '../../atom/button-link';
import ButtonMenu from '../../atom/button-menu';
import propTypes from './types';
import style from './style.css';
const ButtonMenuAction = props => {
  const {
    button,
    menu,
    menuWrapper,
    type = 'button',
    primaryColor,
    containerCustom
  } = props;
  const {
    onClick = _noop
  } = button;
  const [visible, setVisible] = useState(false);
  const containerRef = useRef(null);
  const toggleVisibility = useCallback(() => {
    onClick();
    setVisible(prevState => !prevState);
  }, [onClick]);
  useEffect(() => {
    const handleMouseDown = event => {
      if (visible && containerRef.current && !containerRef.current.contains(event.target)) {
        setVisible(false);
      }
    };
    document.addEventListener('mousedown', handleMouseDown);
    return () => {
      document.removeEventListener('mousedown', handleMouseDown);
    };
  }, [visible, containerRef]);
  const _menu = /*#__PURE__*/React.createElement("div", {
    className: classnames(style.menuWrapper, visible && style.visible),
    "data-name": "menu-wrapper",
    "aria-label": menuWrapper?.ariaLabel,
    style: menuWrapper?.customStyle
  }, /*#__PURE__*/React.createElement(ButtonMenu, menu));
  return /*#__PURE__*/React.createElement("div", {
    className: style.container,
    "data-name": "button-menu-action-wrapper",
    style: containerCustom,
    ref: containerRef
  }, type === 'link' ? /*#__PURE__*/React.createElement("div", {
    className: style.currentOption,
    "data-name": button['data-name'],
    onClick: toggleVisibility,
    "aria-expanded": visible,
    "aria-haspopup": "true"
  }, /*#__PURE__*/React.createElement("span", {
    style: {
      color: visible ? primaryColor : undefined
    }
  }, button.label), /*#__PURE__*/React.createElement(FontAwesomeIcon, {
    icon: visible ? 'chevron-up' : 'chevron-down',
    className: style.caret,
    style: {
      color: visible ? primaryColor : undefined
    },
    "aria-label": button['aria-label']
  })) : /*#__PURE__*/React.createElement(ButtonLink, _extends({}, button, {
    onClick: toggleVisibility
  })), _menu);
};
ButtonMenuAction.propTypes = process.env.NODE_ENV !== "production" ? propTypes : {};
export default ButtonMenuAction;
//# sourceMappingURL=index.js.map