function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).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), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
/**
 * ItemLayer - 楼层图展示层
 * 按 items 的 (x, y) 与 cellSize 定位，用 renderItem 渲染每项内容
 */
import React, { useCallback, useEffect, useMemo } from 'react';
import classNames from 'classnames';
import "./ItemLayer.less";
var PREFIX = 'pisell-floor-map-layout';

/** 图元内可滚动区标记（如预约房间卡 body / multi-list），与 private-materials 约定一致 */
var ROOM_SCROLL_SURFACE_SELECTOR = '[data-pisell-floor-room-scroll-surface]';
function getItemKey(item, index, rowKey) {
  var _rowKey;
  if (!rowKey) return item.id;
  if (typeof rowKey === 'string') return String((_rowKey = item[rowKey]) !== null && _rowKey !== void 0 ? _rowKey : item.id);
  return rowKey(item, index);
}

/**
 * 根据形状返回 item 容器的 style（clip-path / border-radius）
 */
function getShapeStyle() {
  var shape = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '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 {};
  }
}
export function ItemLayer(props) {
  var items = props.items,
    renderItem = props.renderItem,
    rowKey = props.rowKey,
    cellSize = props.cellSize,
    _props$itemUnit = props.itemUnit,
    itemUnit = _props$itemUnit === void 0 ? 'cell' : _props$itemUnit,
    _props$itemGap = props.itemGap,
    itemGap = _props$itemGap === void 0 ? 0 : _props$itemGap,
    contentWidth = props.contentWidth,
    contentHeight = props.contentHeight,
    _props$contentOffsetX = props.contentOffsetX,
    contentOffsetX = _props$contentOffsetX === void 0 ? 0 : _props$contentOffsetX,
    _props$contentOffsetY = props.contentOffsetY,
    contentOffsetY = _props$contentOffsetY === void 0 ? 0 : _props$contentOffsetY,
    _props$enableHover = props.enableHover,
    enableHover = _props$enableHover === void 0 ? true : _props$enableHover,
    _props$itemHoverOutli = props.itemHoverOutline,
    itemHoverOutline = _props$itemHoverOutli === void 0 ? true : _props$itemHoverOutli,
    onItemClick = props.onItemClick,
    onItemHover = props.onItemHover,
    onItemLeave = props.onItemLeave,
    getMergedItemWrapperStyle = props.getMergedItemWrapperStyle,
    onInteractionChange = props.onInteractionChange,
    className = props.className;

  /** 触控/指针在房间卡滚动区内按下时禁用画布 pan，松手恢复 */
  var notifyRoomScrollSurfaceActive = useCallback(function () {
    if (!onInteractionChange) return;
    onInteractionChange(true);
    var finish = function 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]);
  var handleItemPointerDownCapture = useCallback(function (e) {
    var _el$closest;
    if (!onInteractionChange || e.button !== 0) return;
    var el = e.target;
    if (!(el !== null && el !== void 0 && (_el$closest = el.closest) !== null && _el$closest !== void 0 && _el$closest.call(el, ROOM_SCROLL_SURFACE_SELECTOR))) return;
    notifyRoomScrollSurfaceActive();
  }, [onInteractionChange, notifyRoomScrollSurfaceActive]);
  var handleItemTouchStartCapture = useCallback(function (e) {
    var _el$closest2;
    if (!onInteractionChange) return;
    var el = e.target;
    if (!(el !== null && el !== void 0 && (_el$closest2 = el.closest) !== null && _el$closest2 !== void 0 && _el$closest2.call(el, ROOM_SCROLL_SURFACE_SELECTOR))) return;
    notifyRoomScrollSurfaceActive();
  }, [onInteractionChange, notifyRoomScrollSurfaceActive]);
  useEffect(function () {
    return function () {
      onInteractionChange === null || onInteractionChange === void 0 || onInteractionChange(false);
    };
  }, [onInteractionChange]);
  var itemNodes = useMemo(function () {
    var isPixel = itemUnit === 'pixel';
    var g = itemGap;
    var sorted = _toConsumableArray(items).sort(function (a, b) {
      var _a$zIndex, _b$zIndex;
      return ((_a$zIndex = a.zIndex) !== null && _a$zIndex !== void 0 ? _a$zIndex : 0) - ((_b$zIndex = b.zIndex) !== null && _b$zIndex !== void 0 ? _b$zIndex : 0);
    });
    return sorted.map(function (item, index) {
      var _item$zIndex;
      var left;
      var top;
      var width;
      var height;
      if (isPixel) {
        var _item$width, _item$height;
        width = (_item$width = item.width) !== null && _item$width !== void 0 ? _item$width : cellSize;
        height = (_item$height = item.height) !== null && _item$height !== void 0 ? _item$height : cellSize;
        left = item.x;
        top = item.y;
      } else {
        var _item$width2, _item$height2;
        var w = (_item$width2 = item.width) !== null && _item$width2 !== void 0 ? _item$width2 : 1;
        var h = (_item$height2 = item.height) !== null && _item$height2 !== void 0 ? _item$height2 : 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;
      }
      var key = getItemKey(item, index, rowKey);
      var shapeStyle = getShapeStyle(item.shape);
      return /*#__PURE__*/React.createElement("div", {
        key: key,
        className: classNames("".concat(PREFIX, "-item"), _defineProperty(_defineProperty({}, "".concat(PREFIX, "-item-hoverable"), enableHover), "".concat(PREFIX, "-item-hover-outline"), enableHover && itemHoverOutline)),
        style: _objectSpread(_objectSpread({
          position: 'absolute',
          left: left,
          top: top,
          width: width,
          height: height,
          boxSizing: 'border-box',
          zIndex: (_item$zIndex = item.zIndex) !== null && _item$zIndex !== void 0 ? _item$zIndex : 0
        }, shapeStyle), getMergedItemWrapperStyle === null || getMergedItemWrapperStyle === void 0 ? void 0 : getMergedItemWrapperStyle(item, index)),
        onClick: function onClick(e) {
          return onItemClick === null || onItemClick === void 0 ? void 0 : onItemClick(item, index, e);
        },
        onMouseEnter: function onMouseEnter(e) {
          return onItemHover === null || onItemHover === void 0 ? void 0 : onItemHover(item, index, e);
        },
        onMouseLeave: function onMouseLeave(e) {
          return onItemLeave === null || onItemLeave === void 0 ? 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__*/React.createElement("div", {
    className: classNames("".concat(PREFIX, "-item-layer"), className),
    style: {
      position: 'absolute',
      left: 0,
      top: 0,
      width: contentWidth,
      height: contentHeight,
      minWidth: contentWidth,
      minHeight: contentHeight
    }
  }, contentOffsetX !== 0 || contentOffsetY !== 0 ? /*#__PURE__*/React.createElement("div", {
    style: {
      position: 'absolute',
      left: 0,
      top: 0,
      width: contentWidth,
      height: contentHeight,
      transform: "translate(".concat(contentOffsetX, "px, ").concat(contentOffsetY, "px)")
    }
  }, itemNodes) : itemNodes);
}
export default ItemLayer;