import {
  DefaultHoverOverlay_default
} from "./chunk-DB2Y3HTL.mjs";
import {
  ThreeDotsMenuHoverOverlay_default
} from "./chunk-6C7SUEVR.mjs";
import {
  HOVER_OVERLAY_MODES
} from "./chunk-YS6QEOB5.mjs";
import {
  IconButtonContentContainer
} from "./chunk-TSAVJPG4.mjs";

// tmp-babel/__shared__/web/ActionsOverlay/index.js
import { forwardRef, useState } from "react";
import { IconButton } from "@baseapp-frontend/design-system/components/web/buttons";
import { ConfirmDialog } from "@baseapp-frontend/design-system/components/web/dialogs";
import { SwipeableDrawer as DefaultSwipeableDrawer } from "@baseapp-frontend/design-system/components/web/drawers";
import { TrashCanIcon } from "@baseapp-frontend/design-system/components/web/icons";
import { usePopover } from "@baseapp-frontend/design-system/hooks/common";
import { LoadingButton } from "@mui/lab";
import { Box, Divider, Typography } from "@mui/material";
import { LongPressCallbackReason, useLongPress } from "use-long-press";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
function _typeof(o) {
  "@babel/helpers - typeof";
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o2) {
    return typeof o2;
  } : function(o2) {
    return o2 && "function" == typeof Symbol && o2.constructor === Symbol && o2 !== Symbol.prototype ? "symbol" : typeof o2;
  }, _typeof(o);
}
function ownKeys(e, r) {
  var t = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var o = Object.getOwnPropertySymbols(e);
    r && (o = o.filter(function(r2) {
      return Object.getOwnPropertyDescriptor(e, r2).enumerable;
    })), t.push.apply(t, o);
  }
  return t;
}
function _objectSpread(e) {
  for (var r = 1; r < arguments.length; r++) {
    var t = null != arguments[r] ? arguments[r] : {};
    r % 2 ? ownKeys(Object(t), true).forEach(function(r2) {
      _defineProperty(e, r2, t[r2]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r2) {
      Object.defineProperty(e, r2, Object.getOwnPropertyDescriptor(t, r2));
    });
  }
  return e;
}
function _defineProperty(e, r, t) {
  return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
}
function _toPropertyKey(t) {
  var i = _toPrimitive(t, "string");
  return "symbol" == _typeof(i) ? i : i + "";
}
function _toPrimitive(t, r) {
  if ("object" != _typeof(t) || !t) return t;
  var e = t[Symbol.toPrimitive];
  if (void 0 !== e) {
    var i = e.call(t, r || "default");
    if ("object" != _typeof(i)) return i;
    throw new TypeError("@@toPrimitive must return a primitive value.");
  }
  return ("string" === r ? String : Number)(t);
}
function _slicedToArray(r, e) {
  return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
}
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 _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;
  }
}
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 _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) {
        if (Object(t) !== t) return;
        f = false;
      } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = true) ;
    } catch (r2) {
      o = true, n = r2;
    } finally {
      try {
        if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return;
      } finally {
        if (o) throw n;
      }
    }
    return a;
  }
}
function _arrayWithHoles(r) {
  if (Array.isArray(r)) return r;
}
var ActionsOverlay = /* @__PURE__ */ forwardRef(function(_ref, ref) {
  var _ref$actions = _ref.actions, actions = _ref$actions === void 0 ? [] : _ref$actions, children = _ref.children, _ref$title = _ref.title, title = _ref$title === void 0 ? "Item" : _ref$title, _ref$showDeleteButton = _ref.showDeleteButton, showDeleteButton = _ref$showDeleteButton === void 0 ? false : _ref$showDeleteButton, _ref$isDeletingItem = _ref.isDeletingItem, isDeletingItem = _ref$isDeletingItem === void 0 ? false : _ref$isDeletingItem, _ref$disableDeleteBut = _ref.disableDeleteButton, disableDeleteButton = _ref$disableDeleteBut === void 0 ? false : _ref$disableDeleteBut, _ref$handleDeleteItem = _ref.handleDeleteItem, handleDeleteItem = _ref$handleDeleteItem === void 0 ? function() {
  } : _ref$handleDeleteItem, _ref$offsetTop = _ref.offsetTop, offsetTop = _ref$offsetTop === void 0 ? 0 : _ref$offsetTop, _ref$offsetRight = _ref.offsetRight, offsetRight = _ref$offsetRight === void 0 ? 0 : _ref$offsetRight, _ref$ContainerProps = _ref.ContainerProps, ContainerProps = _ref$ContainerProps === void 0 ? {} : _ref$ContainerProps, _ref$SwipeableDrawerP = _ref.SwipeableDrawerProps, SwipeableDrawerProps = _ref$SwipeableDrawerP === void 0 ? {} : _ref$SwipeableDrawerP, _ref$DeleteDialogProp = _ref.DeleteDialogProps, DeleteDialogProps = _ref$DeleteDialogProp === void 0 ? {} : _ref$DeleteDialogProp, _ref$SwipeableDrawer = _ref.SwipeableDrawer, SwipeableDrawer = _ref$SwipeableDrawer === void 0 ? DefaultSwipeableDrawer : _ref$SwipeableDrawer, _ref$hoverOverlayMode = _ref.hoverOverlayMode, hoverOverlayMode = _ref$hoverOverlayMode === void 0 ? HOVER_OVERLAY_MODES["default"] : _ref$hoverOverlayMode;
  var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), isDeleteDialogOpen = _useState2[0], setIsDeleteDialogOpen = _useState2[1];
  var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), isHoveringItem = _useState4[0], setIsHoveringItem = _useState4[1];
  var _useState5 = useState({
    isLongPressingItem: false,
    shouldOpenItemOptions: false
  }), _useState6 = _slicedToArray(_useState5, 2), longPressHandler = _useState6[0], setLongPressHandler = _useState6[1];
  var popover = usePopover();
  var handleClosePopover = function handleClosePopover2() {
    setIsHoveringItem(false);
    popover.onClose();
  };
  var longPressHandlers = useLongPress(function(e) {
    e.stopPropagation();
    setLongPressHandler({
      isLongPressingItem: true,
      shouldOpenItemOptions: true
    });
  }, {
    onCancel: function onCancel(e, _ref2) {
      var _e$target;
      var reason = _ref2.reason;
      var className = (e === null || e === void 0 || (_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.className) || "";
      var classNameString = typeof className === "string" ? className : "";
      var isClickOnBackdrop = classNameString.includes("MuiBackdrop");
      if (reason === LongPressCallbackReason.CancelledByRelease && isClickOnBackdrop) {
        setLongPressHandler(function(prevState) {
          return _objectSpread(_objectSpread({}, prevState), {}, {
            isLongPressingItem: false
          });
        });
      }
    },
    cancelOutsideElement: true,
    threshold: 400
  });
  var handleLongPressItemOptionsClose = function handleLongPressItemOptionsClose2() {
    setLongPressHandler({
      isLongPressingItem: false,
      shouldOpenItemOptions: false
    });
  };
  var handleDeleteDialogOpen = function handleDeleteDialogOpen2() {
    setIsDeleteDialogOpen(true);
  };
  var handleDeleteDialogClose = function handleDeleteDialogClose2() {
    setIsDeleteDialogOpen(false);
    setIsHoveringItem(false);
    popover.onClose();
  };
  var deviceHasHover = typeof window !== "undefined" && window.matchMedia("(hover: hover)").matches;
  var onDeleteItemClick = function onDeleteItemClick2() {
    setIsDeleteDialogOpen(false);
    handleDeleteItem === null || handleDeleteItem === void 0 || handleDeleteItem();
    handleLongPressItemOptionsClose();
    setIsHoveringItem(false);
    popover.onClose();
  };
  var renderDeleteDialog = function renderDeleteDialog2() {
    return /* @__PURE__ */ _jsx(ConfirmDialog, _objectSpread({
      title: "Delete ".concat(title, "?"),
      content: "Are you sure you want to delete? This action cannot be undone.",
      action: /* @__PURE__ */ _jsx(LoadingButton, {
        color: "error",
        onClick: onDeleteItemClick,
        disabled: isDeletingItem,
        loading: isDeletingItem,
        children: "Delete"
      }),
      onClose: handleDeleteDialogClose,
      open: isDeleteDialogOpen
    }, DeleteDialogProps));
  };
  var renderActionsOverlay = function renderActionsOverlay2() {
    if (!deviceHasHover) {
      var handleDrawerClose = function handleDrawerClose2() {
        if (!longPressHandler.isLongPressingItem) {
          handleLongPressItemOptionsClose();
        }
      };
      return /* @__PURE__ */ _jsx(SwipeableDrawer, _objectSpread(_objectSpread({
        open: longPressHandler.shouldOpenItemOptions && longPressHandler.isLongPressingItem,
        onClose: handleDrawerClose,
        "aria-label": "actions overlay"
      }, SwipeableDrawerProps), {}, {
        children: /* @__PURE__ */ _jsxs(Box, {
          display: "grid",
          gridTemplateColumns: "1fr",
          justifySelf: "start",
          gap: 1,
          width: "100%",
          children: [actions === null || actions === void 0 ? void 0 : actions.map(function(_ref3) {
            var label = _ref3.label, icon = _ref3.icon, onClick = _ref3.onClick, disabled = _ref3.disabled, hasPermission = _ref3.hasPermission, closeOnClick = _ref3.closeOnClick;
            if (!hasPermission) return null;
            var handleClick = function handleClick2() {
              onClick === null || onClick === void 0 || onClick();
              if (closeOnClick) {
                handleLongPressItemOptionsClose();
              }
            };
            return /* @__PURE__ */ _jsx(IconButton, {
              onClick: handleClick,
              disabled,
              sx: {
                width: "fit-content"
              },
              "aria-label": label,
              children: /* @__PURE__ */ _jsxs(IconButtonContentContainer, {
                children: [/* @__PURE__ */ _jsx(Box, {
                  display: "grid",
                  justifySelf: "center",
                  height: "min-content",
                  children: icon
                }), /* @__PURE__ */ _jsx(Typography, {
                  variant: "body2",
                  color: "text.primary",
                  children: label
                })]
              })
            }, label);
          }), showDeleteButton && /* @__PURE__ */ _jsxs(_Fragment, {
            children: [/* @__PURE__ */ _jsx(Divider, {}), /* @__PURE__ */ _jsx(IconButton, {
              onClick: handleDeleteDialogOpen,
              disabled: isDeletingItem || disableDeleteButton,
              sx: {
                width: "fit-content"
              },
              "aria-label": "delete item",
              children: /* @__PURE__ */ _jsxs(IconButtonContentContainer, {
                children: [/* @__PURE__ */ _jsx(Box, {
                  display: "grid",
                  justifySelf: "center",
                  height: "min-content",
                  children: /* @__PURE__ */ _jsx(TrashCanIcon, {
                    sx: {
                      color: disableDeleteButton ? "text.disabled" : "error.main"
                    }
                  })
                }), /* @__PURE__ */ _jsx(Typography, {
                  variant: "body2",
                  color: disableDeleteButton ? "text.disabled" : "error.main",
                  children: "Delete"
                })]
              })
            })]
          })]
        })
      }));
    }
    if (deviceHasHover && isHoveringItem) {
      if (hoverOverlayMode === HOVER_OVERLAY_MODES["default"]) {
        return /* @__PURE__ */ _jsx(DefaultHoverOverlay_default, {
          offsetRight,
          offsetTop,
          showDeleteButton,
          isDeletingItem,
          disableDeleteButton,
          handleDeleteDialogOpen,
          actions,
          handleLongPressItemOptionsClose
        });
      }
      if (hoverOverlayMode === HOVER_OVERLAY_MODES.threeDotsMenu) {
        return /* @__PURE__ */ _jsx(ThreeDotsMenuHoverOverlay_default, {
          offsetRight,
          offsetTop,
          showDeleteButton,
          isDeletingItem,
          disableDeleteButton,
          handleDeleteDialogOpen,
          actions,
          handleClosePopover,
          popover
        });
      }
    }
    return /* @__PURE__ */ _jsx("div", {});
  };
  return /* @__PURE__ */ _jsxs(Box, _objectSpread(_objectSpread(_objectSpread(_objectSpread({
    ref,
    onMouseEnter: function onMouseEnter() {
      return setIsHoveringItem(true);
    },
    onMouseLeave: handleClosePopover,
    position: "relative"
  }, longPressHandlers()), hoverOverlayMode === HOVER_OVERLAY_MODES.threeDotsMenu && {
    sx: {
      width: "100%",
      display: "flex",
      justifyContent: "flex-end"
    }
  }), ContainerProps), {}, {
    children: [renderDeleteDialog(), renderActionsOverlay(), children]
  }));
});
var ActionsOverlay_default = ActionsOverlay;

export {
  ActionsOverlay_default
};
//# sourceMappingURL=chunk-O5CLX3PU.mjs.map