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/pisellRecordBoard/shellFrame/Calendar/bookingCalendarViewShared.tsx
var bookingCalendarViewShared_exports = {};
__export(bookingCalendarViewShared_exports, {
  BookingCard: () => BookingCard,
  buildGroupedCourtRows: () => buildGroupedCourtRows,
  getBlockMetaLine: () => getBlockMetaLine,
  getRemainingToneClass: () => getRemainingToneClass,
  statusTagMeta: () => statusTagMeta
});
module.exports = __toCommonJS(bookingCalendarViewShared_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_calendarI18n = require("./calendarI18n");
var import_bookingCalendarDateUtils = require("./utils/bookingCalendarDateUtils");
var import_bookingCalendarConstants = require("./const/bookingCalendarConstants");
var import_BookingCalendarStyle = require("./utils/BookingCalendarStyle");
function statusTagMeta(tag) {
  const p = (0, import_bookingCalendarDateUtils.statusTagMetaParts)(tag);
  return p ? { label: (0, import_calendarI18n.tCal)(p.labelI18nKey), className: p.className } : null;
}
function getBlockMetaLine(item) {
  if (item.kind !== "block") return "";
  const bits = [];
  if (item.blockBy)
    bits.push(
      (0, import_calendarI18n.tCal)("pisell2.recordBoard.calendar.card.blockBy", { name: item.blockBy })
    );
  if (item.blockedAt) bits.push((0, import_bookingCalendarDateUtils.formatDateLoose)(item.blockedAt));
  return bits.join(" · ");
}
function getRemainingToneClass(ratio, isLight = false) {
  if (ratio <= 0.2) return isLight ? "text-red-600" : "text-red-300";
  if (ratio <= 0.4) return isLight ? "text-amber-600" : "text-amber-300";
  return isLight ? "theme-text-muted" : "text-zinc-400";
}
function buildGroupedCourtRows(courts, collapsedGroups = {}, options = {}) {
  const { flattenWhenAllGroupsCollapsed = false } = options;
  const predefined = ["VIP", "Standard"];
  const rows = [];
  const consumedIds = /* @__PURE__ */ new Set();
  predefined.forEach((group) => {
    const items = courts.filter((court) => court.type === group);
    if (items.length === 0) return;
    items.forEach((c) => consumedIds.add(c.id));
    rows.push({
      kind: "group",
      id: `group-${group}`,
      label: group,
      count: items.length,
      collapsed: !!collapsedGroups[group]
    });
    if (!collapsedGroups[group]) {
      items.forEach((court) => rows.push({ kind: "court", ...court, group }));
    }
  });
  const remainder = courts.filter((c) => !consumedIds.has(c.id));
  const byType = {};
  remainder.forEach((court) => {
    const label = court.type && String(court.type).trim() !== "" ? String(court.type) : (0, import_calendarI18n.tCal)("pisell2.recordBoard.calendar.common.resourceFallback");
    if (!byType[label]) byType[label] = [];
    byType[label].push(court);
  });
  Object.keys(byType).forEach((label) => {
    const items = byType[label];
    if (!items.length) return;
    const idSafe = String(label).replace(/\s+/g, "-");
    rows.push({
      kind: "group",
      id: `group-${idSafe}`,
      label,
      count: items.length,
      collapsed: !!collapsedGroups[label]
    });
    if (!collapsedGroups[label]) {
      items.forEach(
        (court) => rows.push({ kind: "court", ...court, group: label })
      );
    }
  });
  const groupRowCount = rows.filter((r) => r.kind === "group").length;
  if (groupRowCount === 1 && courts.length > 0) {
    const fallbackLabel = (0, import_calendarI18n.tCal)(
      "pisell2.recordBoard.calendar.common.resourceFallback"
    );
    return courts.map((c) => ({
      kind: "court",
      ...c,
      group: c.type && String(c.type).trim() !== "" ? String(c.type) : fallbackLabel
    }));
  }
  const courtRowsOnly = rows.filter((r) => r.kind === "court");
  if (flattenWhenAllGroupsCollapsed && courtRowsOnly.length === 0 && courts.length > 0) {
    return courts.map((c) => ({
      kind: "court",
      id: c.id,
      name: c.name,
      type: c.type,
      group: c.type
    }));
  }
  return rows;
}
function BookingCard({
  booking,
  dayKey,
  onOpenDetail,
  onDragStart,
  onJumpNext,
  overlayWidthPx
}) {
  const suppressDetailClickAfterDragRef = (0, import_react.useRef)(false);
  const suppressDetailClickTimerRef = (0, import_react.useRef)(
    null
  );
  (0, import_react.useEffect)(() => {
    return () => {
      if (suppressDetailClickTimerRef.current != null) {
        window.clearTimeout(suppressDetailClickTimerRef.current);
      }
    };
  }, []);
  const armSuppressDetailClickAfterDrag = () => {
    suppressDetailClickAfterDragRef.current = true;
    if (suppressDetailClickTimerRef.current != null) {
      window.clearTimeout(suppressDetailClickTimerRef.current);
    }
    suppressDetailClickTimerRef.current = window.setTimeout(() => {
      suppressDetailClickAfterDragRef.current = false;
      suppressDetailClickTimerRef.current = null;
    }, 500);
  };
  const { item, span, start, continuesNext } = booking;
  const displayTag = (0, import_bookingCalendarDateUtils.getDisplayStatusTag)(item, dayKey);
  const lateMinutes = (0, import_bookingCalendarDateUtils.getLateMinutes)(item, dayKey);
  const stateClass = (0, import_BookingCalendarStyle.getBookingStateClasses)(item, dayKey);
  const w = overlayWidthPx != null && Number.isFinite(overlayWidthPx) ? overlayWidthPx : Number.POSITIVE_INFINITY;
  const narrow = w < import_bookingCalendarConstants.DAY_BOOKING_NARROW_PX;
  const micro = w < import_bookingCalendarConstants.DAY_BOOKING_MICRO_PX;
  const microMark = Array.from(String(item.name ?? "").trim())[0] || "·";
  const tooltipTitle = /* @__PURE__ */ import_react.default.createElement("div", { className: "max-w-[260px] space-y-1 text-left" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "text-sm font-semibold" }, item.name), item.kind === "block" && item.note && /* @__PURE__ */ import_react.default.createElement("div", { className: "text-xs opacity-90" }, item.note), /* @__PURE__ */ import_react.default.createElement("div", { className: "text-xs tabular-nums opacity-90" }, (0, import_bookingCalendarDateUtils.formatHourLabel)(start), " - ", (0, import_bookingCalendarDateUtils.formatHourLabel)(start + span)), item.kind === "block" && getBlockMetaLine(item) && /* @__PURE__ */ import_react.default.createElement("div", { className: "text-[11px] opacity-80" }, getBlockMetaLine(item)), continuesNext && /* @__PURE__ */ import_react.default.createElement("div", { className: "text-[11px] text-violet-200" }, (0, import_calendarI18n.tCal)("pisell2.recordBoard.calendar.card.continuesNext")), item.kind !== "block" && displayTag && statusTagMeta(displayTag) && /* @__PURE__ */ import_react.default.createElement("div", { className: "text-[11px] opacity-90" }, statusTagMeta(displayTag).label, lateMinutes && displayTag === "late" ? ` · ${(0, import_calendarI18n.tCal)("pisell2.recordBoard.calendar.card.lateMinutesDetail", {
    n: lateMinutes
  })}` : ""));
  const wrapNarrow = (node) => narrow ? /* @__PURE__ */ import_react.default.createElement(
    import_antd.Tooltip,
    {
      title: tooltipTitle,
      placement: "top",
      mouseEnterDelay: 0.12,
      destroyTooltipOnHide: true
    },
    node
  ) : node;
  if (item.kind === "block") {
    const blockWide = /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "truncate text-sm font-medium" }, item.name), item.note && /* @__PURE__ */ import_react.default.createElement("div", { className: "mt-1 truncate text-xs text-zinc-200/90" }, item.note), getBlockMetaLine(item) && /* @__PURE__ */ import_react.default.createElement("div", { className: "mt-1 truncate text-[10px] text-zinc-300/80" }, getBlockMetaLine(item)));
    const blockCompact = micro ? /* @__PURE__ */ import_react.default.createElement("span", { className: "flex h-full w-full items-center justify-center text-[11px] font-semibold leading-none" }, microMark) : /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "truncate text-[11px] font-medium leading-tight" }, item.name), getBlockMetaLine(item) && /* @__PURE__ */ import_react.default.createElement("div", { className: "mt-0.5 truncate text-[10px] opacity-80" }, getBlockMetaLine(item)));
    const blockPad = micro ? "px-0.5 py-0.5 flex items-center justify-center" : narrow ? "px-2 py-2" : "px-3 py-3 text-left";
    return wrapNarrow(
      /* @__PURE__ */ import_react.default.createElement(
        "button",
        {
          type: "button",
          "aria-label": narrow ? `${item.name}, ${(0, import_bookingCalendarDateUtils.formatHourLabel)(start)} - ${(0, import_bookingCalendarDateUtils.formatHourLabel)(
            start + span
          )}` : void 0,
          onClick: (e) => {
            e.stopPropagation();
            onOpenDetail(item);
          },
          className: `absolute inset-x-0 inset-y-0 rounded-2xl overflow-hidden ${blockPad} ${narrow && !micro ? "text-left" : ""} ${stateClass}`
        },
        narrow ? blockCompact : blockWide
      )
    );
  }
  const bookingWide = /* @__PURE__ */ import_react.default.createElement("div", { className: "flex min-h-0 min-w-0 w-full flex-col gap-1 overflow-hidden text-left" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "truncate text-sm font-medium leading-tight" }, item.name), /* @__PURE__ */ import_react.default.createElement("div", { className: "truncate text-xs opacity-90 tabular-nums" }, (0, import_bookingCalendarDateUtils.formatHourLabel)(start), " - ", (0, import_bookingCalendarDateUtils.formatHourLabel)(start + span)), continuesNext && /* @__PURE__ */ import_react.default.createElement(
    "span",
    {
      role: "button",
      tabIndex: 0,
      onClick: (e) => {
        e.stopPropagation();
        onJumpNext();
      },
      onKeyDown: (e) => {
        if (e.key === "Enter" || e.key === " ") {
          e.preventDefault();
          e.stopPropagation();
          onJumpNext();
        }
      },
      className: "inline-flex max-w-full cursor-pointer truncate rounded-lg bg-white/10 px-2 py-1 text-left text-[10px] leading-tight text-violet-100 sm:text-[11px]"
    },
    (0, import_calendarI18n.tCal)("pisell2.recordBoard.calendar.card.continuesNext")
  ), (displayTag || lateMinutes) && /* @__PURE__ */ import_react.default.createElement("div", { className: "flex shrink-0 flex-wrap items-center justify-end gap-1" }, lateMinutes && displayTag === "late" && /* @__PURE__ */ import_react.default.createElement("span", { className: "rounded-md bg-black/25 px-1.5 py-0.5 text-[10px] font-semibold text-white/90 backdrop-blur-[2px] opacity-80 sm:px-2" }, (0, import_calendarI18n.tCal)("pisell2.recordBoard.calendar.card.lateMinutesBadge", {
    n: lateMinutes
  })), displayTag && statusTagMeta(displayTag) && /* @__PURE__ */ import_react.default.createElement(
    "span",
    {
      className: `rounded-lg px-1.5 py-0.5 text-[10px] font-semibold backdrop-blur-[2px] sm:px-2 ${statusTagMeta(displayTag).className} ${lateMinutes ? "opacity-80" : ""}`
    },
    statusTagMeta(displayTag).label
  )));
  const bookingCompact = micro ? /* @__PURE__ */ import_react.default.createElement("span", { className: "flex h-full w-full items-center justify-center text-[11px] font-semibold leading-none" }, microMark) : /* @__PURE__ */ import_react.default.createElement("div", { className: "flex min-h-0 min-w-0 flex-col justify-start overflow-hidden" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "truncate text-[11px] font-medium leading-tight tabular-nums" }, item.name, " · ", (0, import_bookingCalendarDateUtils.formatHourLabel)(start), "–", (0, import_bookingCalendarDateUtils.formatHourLabel)(start + span)));
  const bookingPad = micro ? "px-0.5 py-0.5" : narrow ? "px-1.5 py-1.5" : "px-2 py-2 sm:px-4 sm:py-3";
  const bookingFlexMain = micro ? "flex items-center justify-center" : "flex min-h-0 flex-col justify-start";
  const bookingAlign = !narrow ? "text-left" : micro ? "" : "justify-start";
  return wrapNarrow(
    /* @__PURE__ */ import_react.default.createElement(
      "button",
      {
        type: "button",
        draggable: true,
        "aria-label": narrow ? `${item.name}, ${(0, import_bookingCalendarDateUtils.formatHourLabel)(start)} - ${(0, import_bookingCalendarDateUtils.formatHourLabel)(
          start + span
        )}` : void 0,
        onDragStart: (e) => {
          e.stopPropagation();
          onDragStart(item);
        },
        onDragEnd: (e) => {
          e.stopPropagation();
          armSuppressDetailClickAfterDrag();
        },
        onClick: (e) => {
          if (suppressDetailClickAfterDragRef.current) {
            suppressDetailClickAfterDragRef.current = false;
            if (suppressDetailClickTimerRef.current != null) {
              window.clearTimeout(suppressDetailClickTimerRef.current);
              suppressDetailClickTimerRef.current = null;
            }
            e.preventDefault();
            e.stopPropagation();
            return;
          }
          e.stopPropagation();
          onOpenDetail(item);
        },
        className: `absolute inset-x-0 inset-y-0 rounded-2xl overflow-hidden ${bookingFlexMain} ${bookingAlign} ${bookingPad} ${stateClass}`
      },
      narrow ? bookingCompact : bookingWide
    )
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BookingCard,
  buildGroupedCourtRows,
  getBlockMetaLine,
  getRemainingToneClass,
  statusTagMeta
});
