var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/user-action/Menu.tsx
var Menu_exports = {};
__export(Menu_exports, {
  Menu: () => Menu,
  MenuItem: () => MenuItem
});
module.exports = __toCommonJS(Menu_exports);
var import_react3 = require("react");
var import_clsx = __toESM(require("clsx"), 1);

// src/utils/bagFunctions.ts
var resolve = (children, bag) => {
  if (typeof children === "function") {
    return children(bag);
  }
  return children ?? void 0;
};
var BagFunctionUtil = {
  resolve
};

// src/components/user-action/Menu.tsx
var import_react_dom = require("react-dom");

// src/hooks/usePopoverPosition.ts
var defaultPopoverPositionOptions = {
  edgePadding: 16,
  outerGap: 4,
  horizontalAlignment: "leftInside",
  verticalAlignment: "bottomOutside",
  disabled: false
};
var usePopoverPosition = (trigger, options) => {
  const {
    edgePadding,
    outerGap,
    verticalAlignment,
    horizontalAlignment,
    disabled
  } = { ...defaultPopoverPositionOptions, ...options };
  if (disabled || !trigger) {
    return {};
  }
  const left = {
    leftOutside: trigger.left - outerGap,
    leftInside: trigger.left,
    rightOutside: trigger.right + outerGap,
    rightInside: trigger.right,
    center: trigger.left + trigger.width / 2
  }[horizontalAlignment];
  const top = {
    topOutside: trigger.top - outerGap,
    topInside: trigger.top,
    bottomOutside: trigger.bottom + outerGap,
    bottomInside: trigger.bottom,
    center: trigger.top + trigger.height / 2
  }[verticalAlignment];
  const translateX = {
    leftOutside: "-100%",
    leftInside: void 0,
    rightOutside: void 0,
    rightInside: "-100%",
    center: "-50%"
  }[horizontalAlignment];
  const translateY = {
    topOutside: "-100%",
    topInside: void 0,
    bottomOutside: void 0,
    bottomInside: "-100%",
    center: "-50%"
  }[verticalAlignment];
  return {
    left: Math.max(left, edgePadding),
    top: Math.max(top, edgePadding),
    translate: [translateX ?? "0", translateY ?? "0"].join(" ")
  };
};

// src/hooks/useHoverState.ts
var import_react = require("react");
var defaultUseHoverStateProps = {
  closingDelay: 200,
  isDisabled: false
};
var useHoverState = (props = void 0) => {
  const { closingDelay, isDisabled } = { ...defaultUseHoverStateProps, ...props };
  const [isHovered, setIsHovered] = (0, import_react.useState)(false);
  const [timer, setTimer] = (0, import_react.useState)();
  const onMouseEnter = () => {
    if (isDisabled) {
      return;
    }
    clearTimeout(timer);
    setIsHovered(true);
  };
  const onMouseLeave = () => {
    if (isDisabled) {
      return;
    }
    setTimer(setTimeout(() => {
      setIsHovered(false);
    }, closingDelay));
  };
  (0, import_react.useEffect)(() => {
    if (timer) {
      return () => {
        clearTimeout(timer);
      };
    }
  });
  (0, import_react.useEffect)(() => {
    if (timer) {
      clearTimeout(timer);
    }
  }, [isDisabled]);
  return {
    isHovered,
    setIsHovered,
    handlers: { onMouseEnter, onMouseLeave }
  };
};

// src/hooks/useOutsideClick.ts
var import_react2 = require("react");
var useOutsideClick = (refs, handler) => {
  (0, import_react2.useEffect)(() => {
    const listener = (event) => {
      if (event.target === null) return;
      if (refs.some((ref) => !ref.current || ref.current.contains(event.target))) {
        return;
      }
      handler();
    };
    document.addEventListener("mousedown", listener);
    document.addEventListener("touchstart", listener);
    return () => {
      document.removeEventListener("mousedown", listener);
      document.removeEventListener("touchstart", listener);
    };
  }, [refs, handler]);
};

// src/components/user-action/Menu.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var MenuItem = ({
  children,
  onClick,
  alignment = "left",
  isDisabled = false,
  className
}) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
  "div",
  {
    className: (0, import_clsx.default)("block px-3 py-1.5 first:rounded-t-md last:rounded-b-md text-sm font-semibold text-nowrap", {
      "text-right": alignment === "right",
      "text-left": alignment === "left",
      "text-disabled cursor-not-allowed": isDisabled,
      "text-menu-text hover:bg-primary/20": !isDisabled,
      "cursor-pointer": !!onClick
    }, className),
    onClick,
    children
  }
);
function getScrollableParents(element) {
  const scrollables = [];
  let parent = element.parentElement;
  while (parent) {
    scrollables.push(parent);
    parent = parent.parentElement;
  }
  return scrollables;
}
var Menu = ({
  trigger,
  children,
  alignmentHorizontal = "leftInside",
  alignmentVertical = "bottomOutside",
  showOnHover = false,
  disabled = false,
  menuClassName = ""
}) => {
  const { isHovered: isOpen, setIsHovered: setIsOpen } = useHoverState({ isDisabled: !showOnHover || disabled });
  const triggerRef = (0, import_react3.useRef)(null);
  const menuRef = (0, import_react3.useRef)(null);
  useOutsideClick([triggerRef, menuRef], () => setIsOpen(false));
  const [isHidden, setIsHidden] = (0, import_react3.useState)(true);
  const bag = {
    isOpen,
    close: () => setIsOpen(false),
    toggleOpen: () => setIsOpen((prevState) => !prevState),
    disabled
  };
  const menuPosition = usePopoverPosition(
    triggerRef.current?.getBoundingClientRect(),
    { verticalAlignment: alignmentVertical, horizontalAlignment: alignmentHorizontal, disabled }
  );
  (0, import_react3.useEffect)(() => {
    if (!isOpen) return;
    const triggerEl = triggerRef.current;
    if (!triggerEl) return;
    const scrollableParents = getScrollableParents(triggerEl);
    const close = () => setIsOpen(false);
    scrollableParents.forEach((parent) => {
      parent.addEventListener("scroll", close);
    });
    window.addEventListener("resize", close);
    return () => {
      scrollableParents.forEach((parent) => {
        parent.removeEventListener("scroll", close);
      });
      window.removeEventListener("resize", close);
    };
  }, [isOpen, setIsOpen]);
  (0, import_react3.useEffect)(() => {
    if (isOpen) {
      setIsHidden(false);
    }
  }, [isOpen]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    trigger(bag, triggerRef),
    (0, import_react_dom.createPortal)(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      "div",
      {
        ref: menuRef,
        onClick: (e) => e.stopPropagation(),
        className: (0, import_clsx.default)(
          "absolute rounded-md bg-menu-background text-menu-text shadow-around-lg shadow-strong z-[300]",
          {
            "animate-pop-in": isOpen,
            "animate-pop-out": !isOpen,
            "hidden": isHidden
          },
          menuClassName
        ),
        onAnimationEnd: () => {
          if (!isOpen) {
            setIsHidden(true);
          }
        },
        style: {
          ...menuPosition
        },
        children: BagFunctionUtil.resolve(children, bag)
      }
    ), document.body)
  ] });
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Menu,
  MenuItem
});
//# sourceMappingURL=Menu.cjs.map