'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var react = require('react');
var framerMotion = require('framer-motion');
var reactDom = require('@floating-ui/react-dom');
var jsxRuntime = require('react/jsx-runtime');

function _arrayLikeToArray(r, a) {
  (null == a || a > r.length) && (a = r.length);
  for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
  return n;
}
function _arrayWithHoles(r) {
  if (Array.isArray(r)) return r;
}
function _iterableToArrayLimit(r, l) {
  var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  if (null != t) {
    var e,
      n,
      i,
      u,
      a = [],
      f = true,
      o = false;
    try {
      if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
    } catch (r) {
      o = true, n = r;
    } finally {
      try {
        if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
      } finally {
        if (o) throw n;
      }
    }
    return a;
  }
}
function _nonIterableRest() {
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _slicedToArray(r, e) {
  return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
}
function _unsupportedIterableToArray(r, a) {
  if (r) {
    if ("string" == typeof r) return _arrayLikeToArray(r, a);
    var t = {}.toString.call(r).slice(8, -1);
    return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
  }
}

var Menu = function Menu(_ref) {
  var anchor = _ref.anchor,
    children = _ref.children,
    _ref$placement = _ref.placement,
    placement = _ref$placement === void 0 ? 'bottom-start' : _ref$placement;
  var _useState = react.useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  var _useFloating = reactDom.useFloating({
      placement: placement,
      middleware: [reactDom.offset(8), reactDom.flip(), reactDom.shift({
        padding: 8
      })]
    }),
    refs = _useFloating.refs,
    x = _useFloating.x,
    y = _useFloating.y,
    strategy = _useFloating.strategy,
    update = _useFloating.update;

  // Auto-update on layout changes
  react.useEffect(function () {
    if (open && refs.reference.current && refs.floating.current) {
      return reactDom.autoUpdate(refs.reference.current, refs.floating.current, update);
    }
  }, [open, refs.reference, refs.floating, update]);

  // Close when clicking outside
  react.useEffect(function () {
    var handleClickOutside = function handleClickOutside(event) {
      if (refs.reference.current && refs.floating.current && !refs.floating.current.contains(event.target)) {
        setOpen(false);
      }
    };
    document.addEventListener('mousedown', handleClickOutside);
    return function () {
      return document.removeEventListener('mousedown', handleClickOutside);
    };
  }, [refs]);
  return /*#__PURE__*/jsxRuntime.jsxs("div", {
    style: {
      display: 'inline-block'
    },
    children: [/*#__PURE__*/jsxRuntime.jsx("div", {
      ref: refs.setReference,
      onClick: function onClick() {
        return setOpen(function (prev) {
          return !prev;
        });
      },
      style: {
        cursor: 'pointer'
      },
      children: anchor
    }), /*#__PURE__*/jsxRuntime.jsx(framerMotion.AnimatePresence, {
      children: open && /*#__PURE__*/jsxRuntime.jsx(framerMotion.motion.div, {
        ref: refs.setFloating,
        initial: {
          opacity: 0,
          scale: 0.95,
          y: -5
        },
        animate: {
          opacity: 1,
          scale: 1,
          y: 0
        },
        exit: {
          opacity: 0,
          scale: 0.95,
          y: -5
        },
        transition: {
          duration: 0.2,
          ease: 'easeOut'
        },
        style: {
          position: strategy,
          top: y !== null && y !== void 0 ? y : 0,
          left: x !== null && x !== void 0 ? x : 0,
          zIndex: 1000,
          boxShadow: '0 2px 8px rgba(0,0,0,0.15)',
          minWidth: '150px'
        },
        children: typeof children === 'function' ? children(function () {
          return setOpen(false);
        }) : children
      })
    })]
  });
};

exports.default = Menu;
