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/pisellFloorMapLayout/components/ItemLayer.tsx
var ItemLayer_exports = {};
__export(ItemLayer_exports, {
  ItemLayer: () => ItemLayer,
  default: () => ItemLayer_default
});
module.exports = __toCommonJS(ItemLayer_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_ItemLayer = require("./ItemLayer.less");
var PREFIX = "pisell-floor-map-layout";
var ROOM_SCROLL_SURFACE_SELECTOR = "[data-pisell-floor-room-scroll-surface]";
function getItemKey(item, index, rowKey) {
  if (!rowKey) return item.id;
  if (typeof rowKey === "string")
    return String(item[rowKey] ?? item.id);
  return rowKey(item, index);
}
function getShapeStyle(shape = "rect") {
  switch (shape) {
    case "circle":
      return { borderRadius: "50%" };
    case "triangle":
      return { clipPath: "polygon(50% 0%, 0% 100%, 100% 100%)" };
    case "polygon":
    case "custom":
      return {};
    default:
      return {};
  }
}
function ItemLayer(props) {
  const {
    items,
    renderItem,
    rowKey,
    cellSize,
    itemUnit = "cell",
    itemGap = 0,
    contentWidth,
    contentHeight,
    contentOffsetX = 0,
    contentOffsetY = 0,
    enableHover = true,
    itemHoverOutline = true,
    onItemClick,
    onItemHover,
    onItemLeave,
    getMergedItemWrapperStyle,
    onInteractionChange,
    className
  } = props;
  const notifyRoomScrollSurfaceActive = (0, import_react.useCallback)(() => {
    if (!onInteractionChange) return;
    onInteractionChange(true);
    const finish = () => {
      onInteractionChange(false);
    };
    window.addEventListener("pointerup", finish, { once: true });
    window.addEventListener("pointercancel", finish, { once: true });
    window.addEventListener("touchend", finish, { once: true, passive: true });
    window.addEventListener("touchcancel", finish, {
      once: true,
      passive: true
    });
  }, [onInteractionChange]);
  const handleItemPointerDownCapture = (0, import_react.useCallback)(
    (e) => {
      var _a;
      if (!onInteractionChange || e.button !== 0) return;
      const el = e.target;
      if (!((_a = el == null ? void 0 : el.closest) == null ? void 0 : _a.call(el, ROOM_SCROLL_SURFACE_SELECTOR))) return;
      notifyRoomScrollSurfaceActive();
    },
    [onInteractionChange, notifyRoomScrollSurfaceActive]
  );
  const handleItemTouchStartCapture = (0, import_react.useCallback)(
    (e) => {
      var _a;
      if (!onInteractionChange) return;
      const el = e.target;
      if (!((_a = el == null ? void 0 : el.closest) == null ? void 0 : _a.call(el, ROOM_SCROLL_SURFACE_SELECTOR))) return;
      notifyRoomScrollSurfaceActive();
    },
    [onInteractionChange, notifyRoomScrollSurfaceActive]
  );
  (0, import_react.useEffect)(() => {
    return () => {
      onInteractionChange == null ? void 0 : onInteractionChange(false);
    };
  }, [onInteractionChange]);
  const itemNodes = (0, import_react.useMemo)(() => {
    const isPixel = itemUnit === "pixel";
    const g = itemGap;
    const sorted = [...items].sort((a, b) => (a.zIndex ?? 0) - (b.zIndex ?? 0));
    return sorted.map((item, index) => {
      let left;
      let top;
      let width;
      let height;
      if (isPixel) {
        width = item.width ?? cellSize;
        height = item.height ?? cellSize;
        left = item.x;
        top = item.y;
      } else {
        const w = item.width ?? 1;
        const h = item.height ?? 1;
        left = g > 0 ? item.x * (cellSize + g) + g : item.x * cellSize;
        top = g > 0 ? item.y * (cellSize + g) + g : item.y * cellSize;
        width = g > 0 ? w * cellSize + (w - 1) * g : w * cellSize;
        height = g > 0 ? h * cellSize + (h - 1) * g : h * cellSize;
      }
      const key = getItemKey(item, index, rowKey);
      const shapeStyle = getShapeStyle(item.shape);
      return /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          key,
          className: (0, import_classnames.default)(`${PREFIX}-item`, {
            [`${PREFIX}-item-hoverable`]: enableHover,
            [`${PREFIX}-item-hover-outline`]: enableHover && itemHoverOutline
          }),
          style: {
            position: "absolute",
            left,
            top,
            width,
            height,
            boxSizing: "border-box",
            zIndex: item.zIndex ?? 0,
            ...shapeStyle,
            ...getMergedItemWrapperStyle == null ? void 0 : getMergedItemWrapperStyle(item, index)
          },
          onClick: (e) => onItemClick == null ? void 0 : onItemClick(item, index, e),
          onMouseEnter: (e) => onItemHover == null ? void 0 : onItemHover(item, index, e),
          onMouseLeave: (e) => onItemLeave == null ? void 0 : onItemLeave(item, index, e),
          onPointerDownCapture: handleItemPointerDownCapture,
          onTouchStartCapture: handleItemTouchStartCapture
        },
        renderItem(item, index)
      );
    });
  }, [
    items,
    renderItem,
    rowKey,
    cellSize,
    itemUnit,
    itemGap,
    enableHover,
    itemHoverOutline,
    onItemClick,
    onItemHover,
    onItemLeave,
    getMergedItemWrapperStyle,
    handleItemPointerDownCapture,
    handleItemTouchStartCapture
  ]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(`${PREFIX}-item-layer`, className),
      style: {
        position: "absolute",
        left: 0,
        top: 0,
        width: contentWidth,
        height: contentHeight,
        minWidth: contentWidth,
        minHeight: contentHeight
      }
    },
    contentOffsetX !== 0 || contentOffsetY !== 0 ? /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        style: {
          position: "absolute",
          left: 0,
          top: 0,
          width: contentWidth,
          height: contentHeight,
          transform: `translate(${contentOffsetX}px, ${contentOffsetY}px)`
        }
      },
      itemNodes
    ) : itemNodes
  );
}
var ItemLayer_default = ItemLayer;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ItemLayer
});
