"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 _reactFontawesome = require("@fortawesome/react-fontawesome");
var _buttonLink = _interopRequireDefault(require("../../atom/button-link"));
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 }; }
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); }
const ButtonMenuAction = props => {
  const {
    button,
    menu,
    menuWrapper,
    type = 'button',
    primaryColor,
    containerCustom
  } = props;
  const {
    onClick = _noop2.default
  } = button;
  const [visible, setVisible] = (0, _react.useState)(false);
  const containerRef = (0, _react.useRef)(null);
  const toggleVisibility = (0, _react.useCallback)(() => {
    onClick();
    setVisible(prevState => !prevState);
  }, [onClick]);
  (0, _react.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.default.createElement("div", {
    className: (0, _classnames.default)(_style.default.menuWrapper, visible && _style.default.visible),
    "data-name": "menu-wrapper",
    "aria-label": menuWrapper?.ariaLabel,
    style: menuWrapper?.customStyle
  }, /*#__PURE__*/_react.default.createElement(_buttonMenu.default, menu));
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.container,
    "data-name": "button-menu-action-wrapper",
    style: containerCustom,
    ref: containerRef
  }, type === 'link' ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.currentOption,
    "data-name": button['data-name'],
    onClick: toggleVisibility,
    "aria-expanded": visible,
    "aria-haspopup": "true"
  }, /*#__PURE__*/_react.default.createElement("span", {
    style: {
      color: visible ? primaryColor : undefined
    }
  }, button.label), /*#__PURE__*/_react.default.createElement(_reactFontawesome.FontAwesomeIcon, {
    icon: visible ? 'chevron-up' : 'chevron-down',
    className: _style.default.caret,
    style: {
      color: visible ? primaryColor : undefined
    },
    "aria-label": button['aria-label']
  })) : /*#__PURE__*/_react.default.createElement(_buttonLink.default, _extends({}, button, {
    onClick: toggleVisibility
  })), _menu);
};
ButtonMenuAction.propTypes = process.env.NODE_ENV !== "production" ? _types.default : {};
var _default = exports.default = ButtonMenuAction;
//# sourceMappingURL=index.js.map