/**
 * 日/周视图日历格内单条预约的渲染：拖拽抑制、详情打开、RoomCard 或回退按钮。
 */
import React from 'react';
import { findMergedTableRowForBookingRaw } from "../data/calendarAdapter";
import { ReservationCalendarDayBookingRoomCard } from "./ReservationCalendarDayBookingRoomCard";
/**
 * 日历预约条节点（由 `useReservationCalendarSlot` 的 `renderBooking` 返回）。
 */
export function ReservationCalendarBookingNode(props) {
  var args = props.args,
    tables = props.tables,
    calendarDragSuppressDetailRef = props.calendarDragSuppressDetailRef;
  var item = args.booking.item;
  var armSuppressDetailAfterDrag = function armSuppressDetailAfterDrag() {
    var r = calendarDragSuppressDetailRef.current;
    if (r.timer != null) {
      clearTimeout(r.timer);
      r.timer = null;
    }
    r.bookingId = item.id;
    r.timer = setTimeout(function () {
      r.bookingId = null;
      r.timer = null;
    }, 650);
  };
  var onOverlayBookingClick = function onOverlayBookingClick(e) {
    var r = calendarDragSuppressDetailRef.current;
    if (r.bookingId === item.id) {
      r.bookingId = null;
      if (r.timer != null) {
        clearTimeout(r.timer);
        r.timer = null;
      }
      e.preventDefault();
      e.stopPropagation();
      return;
    }
    e.stopPropagation();
    args.onOpenDetail(item);
  };
  if (item.kind === 'block') {
    return /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: "absolute inset-0 rounded-2xl px-2 py-2 text-left",
      onClick: function onClick(e) {
        e.stopPropagation();
        args.onOpenDetail(item);
      }
    }, /*#__PURE__*/React.createElement("div", {
      className: "truncate text-sm font-medium"
    }, item.name));
  }
  var raw = item.raw;
  if (!raw) {
    return /*#__PURE__*/React.createElement("button", {
      type: "button",
      draggable: true,
      onDragStart: function onDragStart(e) {
        e.stopPropagation();
        args.onDragStart(item);
      },
      onDragEnd: function onDragEnd(e) {
        e.stopPropagation();
        armSuppressDetailAfterDrag();
      },
      onClick: onOverlayBookingClick,
      className: "absolute inset-0 rounded-2xl px-2 py-2 text-left"
    }, /*#__PURE__*/React.createElement("div", {
      className: "truncate text-sm"
    }, item.name));
  }
  var row = findMergedTableRowForBookingRaw(raw, tables);
  return /*#__PURE__*/React.createElement("div", {
    className: "absolute inset-0 min-h-0 cursor-grab overflow-hidden rounded-2xl active:cursor-grabbing",
    draggable: true,
    onDragStart: function onDragStart(e) {
      e.stopPropagation();
      args.onDragStart(item);
    },
    onDragEnd: function onDragEnd(e) {
      e.stopPropagation();
      armSuppressDetailAfterDrag();
    },
    onClick: onOverlayBookingClick,
    onKeyDown: function onKeyDown(e) {
      if (e.key === 'Enter' || e.key === ' ') {
        e.preventDefault();
        e.stopPropagation();
        args.onOpenDetail(item);
      }
    },
    role: "button",
    tabIndex: 0
  }, /*#__PURE__*/React.createElement(ReservationCalendarDayBookingRoomCard, {
    raw: raw,
    tableRow: row !== null && row !== void 0 ? row : null,
    overlayWidthPx: args.overlayWidthPx
  }));
}