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/view/BookingCalendarMonthView.tsx
var BookingCalendarMonthView_exports = {};
__export(BookingCalendarMonthView_exports, {
  BookingCalendarMonthView: () => BookingCalendarMonthView
});
module.exports = __toCommonJS(BookingCalendarMonthView_exports);
var import_react = __toESM(require("react"));
var import_bookingCalendarViewShared = require("../bookingCalendarViewShared");
var import_calendarI18n = require("../calendarI18n");
var import_BookingCalendarStyle = require("../utils/BookingCalendarStyle");
var import_bookingCalendarDateUtils = require("../utils/bookingCalendarDateUtils");
var import_bookingCalendarConstants = require("../const/bookingCalendarConstants");
var import_bookingCalendarAxis = require("../utils/bookingCalendarAxis");
var MONTH_GRID_COLS = 7;
function BookingCalendarMonthView({
  isLight,
  anchorDate,
  monthDays,
  bookings,
  goToDayFromCell,
  handleGridDropMove,
  setDraggingItem,
  openBookingDetail,
  setOverflowListModal,
  renderMonthWeekBooking,
  tickFillMode
}) {
  const todayKey = (0, import_bookingCalendarDateUtils.fmtDate)(/* @__PURE__ */ new Date());
  const weekRows = (0, import_react.useMemo)(() => {
    const out = [];
    for (let i = 0; i < monthDays.length; i += MONTH_GRID_COLS) {
      out.push(monthDays.slice(i, i + MONTH_GRID_COLS));
    }
    return out;
  }, [monthDays]);
  const monthGridBodyRef = (0, import_react.useRef)(null);
  const [monthGridBodyHeight, setMonthGridBodyHeight] = (0, import_react.useState)(0);
  (0, import_react.useLayoutEffect)(() => {
    const el = monthGridBodyRef.current;
    if (!el) return;
    const measure = () => {
      const h = el.getBoundingClientRect().height;
      if (h > 0) setMonthGridBodyHeight(h);
    };
    measure();
    const ro = new ResizeObserver(measure);
    ro.observe(el);
    return () => ro.disconnect();
  }, [weekRows.length, anchorDate.getFullYear(), anchorDate.getMonth()]);
  const weekBannerZoneHeightPx = (0, import_react.useMemo)(() => {
    if (monthGridBodyHeight <= 0 || weekRows.length === 0) return 0;
    return Math.max(
      import_bookingCalendarConstants.MONTH_VIEW_BANNER_H,
      monthGridBodyHeight / weekRows.length - import_bookingCalendarConstants.MONTH_VIEW_DAY_HEADER_H
    );
  }, [monthGridBodyHeight, weekRows.length]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      key: `month-${anchorDate.getFullYear()}-${anchorDate.getMonth()}`,
      className: "flex min-h-0 flex-1 flex-col"
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "relative flex min-h-0 flex-1 flex-col overflow-hidden" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "flex min-h-0 min-w-0 flex-1 flex-col overflow-x-auto overflow-y-hidden" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "theme-panel flex min-h-[0] min-w-[980px] flex-1 flex-col overflow-hidden" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "theme-panel-alt grid shrink-0 grid-cols-7 border-b theme-border shadow-[0_6px_12px_rgba(0,0,0,0.08)]" }, import_calendarI18n.CALENDAR_WEEKDAY_IDS.map((id) => /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        key: id,
        className: "px-4 py-2.5 text-center text-sm font-medium"
      },
      (0, import_calendarI18n.tCal)(id)
    ))), /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        ref: monthGridBodyRef,
        className: "grid min-h-0 flex-1 grid-cols-7 gap-0",
        style: {
          gridTemplateRows: `repeat(${weekRows.length}, minmax(0, 1fr))`
        }
      },
      weekRows.map((week, wIdx) => {
        var _a;
        return /* @__PURE__ */ import_react.default.createElement(
          MonthWeekRow,
          {
            key: `week-${wIdx}-${((_a = week[0]) == null ? void 0 : _a.key) ?? wIdx}`,
            week,
            bannerZoneHeightPx: weekBannerZoneHeightPx,
            isLight,
            todayKey,
            bookings,
            goToDayFromCell,
            handleGridDropMove,
            setDraggingItem,
            openBookingDetail,
            setOverflowListModal,
            renderMonthWeekBooking,
            tickFillMode
          }
        );
      })
    ))))
  );
}
function MonthWeekRow({
  week,
  bannerZoneHeightPx,
  isLight,
  todayKey,
  bookings,
  goToDayFromCell,
  handleGridDropMove,
  setDraggingItem,
  openBookingDetail,
  setOverflowListModal,
  renderMonthWeekBooking,
  tickFillMode
}) {
  var _a, _b;
  const weekFirstKey = ((_a = week[0]) == null ? void 0 : _a.key) ?? "";
  const weekLastKey = ((_b = week[week.length - 1]) == null ? void 0 : _b.key) ?? "";
  const dateKeyToCol = (0, import_react.useMemo)(() => {
    const m = /* @__PURE__ */ new Map();
    week.forEach((d, i) => m.set(d.key, i));
    return m;
  }, [week]);
  const bookingById = (0, import_react.useMemo)(() => {
    const m = /* @__PURE__ */ new Map();
    for (const b of bookings) {
      if ((b == null ? void 0 : b.id) != null) m.set(String(b.id), b);
    }
    return m;
  }, [bookings]);
  const weekClipped = (0, import_react.useMemo)(() => {
    const seen = /* @__PURE__ */ new Set();
    const out = [];
    for (const b of bookings) {
      if ((b == null ? void 0 : b.id) == null) continue;
      const idKey = String(b.id);
      if (seen.has(idKey)) continue;
      const clipped = (0, import_bookingCalendarDateUtils.clipBookingDaySpanToWindow)(b, weekFirstKey, weekLastKey);
      if (!clipped) continue;
      seen.add(idKey);
      out.push(clipped);
    }
    return out;
  }, [bookings, weekFirstKey, weekLastKey]);
  const weekDateKeys = (0, import_react.useMemo)(() => week.map((d) => d.key), [week]);
  const { perDateLanes, weekMaxLaneCount } = (0, import_react.useMemo)(() => {
    const { perDateLanes: perDate, laneCount } = (0, import_bookingCalendarDateUtils.assignPerDateLanesCrossDayUnified)(weekClipped, weekDateKeys);
    return { perDateLanes: perDate, weekMaxLaneCount: laneCount };
  }, [weekClipped, weekDateKeys]);
  const effectiveMaxLane = (0, import_react.useMemo)(() => {
    if (bannerZoneHeightPx > 0) {
      return (0, import_bookingCalendarConstants.monthViewEffectiveMaxLanes)(
        bannerZoneHeightPx,
        weekMaxLaneCount
      );
    }
    return import_bookingCalendarConstants.MONTH_VIEW_MAX_LANE;
  }, [bannerZoneHeightPx, weekMaxLaneCount]);
  const { banners, overflowByDate } = (0, import_react.useMemo)(() => {
    var _a2;
    const bannersOut = [];
    const overflowMap = /* @__PURE__ */ new Map();
    const pushOverflow = (dateKey, item) => {
      const idKey = String(item.id);
      const arr = overflowMap.get(dateKey);
      if (arr) {
        if (!arr.some((x) => String(x.id) === idKey)) arr.push(item);
      } else {
        overflowMap.set(dateKey, [item]);
      }
    };
    for (const b of weekClipped) {
      const idKey = String(b.id);
      const original = bookingById.get(idKey) ?? b;
      let displayLane = 0;
      let showBanner = false;
      for (const dateKey of weekDateKeys) {
        const lane = (_a2 = perDateLanes.get(dateKey)) == null ? void 0 : _a2.get(idKey);
        if (lane === void 0) continue;
        displayLane = lane;
        if (lane < effectiveMaxLane) showBanner = true;
        if (lane >= effectiveMaxLane) pushOverflow(dateKey, original);
      }
      if (!showBanner) continue;
      const startCol = dateKeyToCol.get(String(b.date)) ?? 0;
      const endCol = dateKeyToCol.get(String(b.endDate ?? b.date)) ?? startCol;
      const origStart = String((original == null ? void 0 : original.date) ?? b.date);
      const origEnd = String((original == null ? void 0 : original.endDate) ?? (original == null ? void 0 : original.date) ?? b.date);
      const clippedLeft = origStart < weekFirstKey;
      const clippedRight = origEnd > weekLastKey;
      bannersOut.push({
        item: original,
        lane: displayLane,
        startCol,
        endCol,
        clippedLeft,
        clippedRight,
        crossDay: (0, import_bookingCalendarDateUtils.isCrossDayBooking)(original)
      });
    }
    return { banners: bannersOut, overflowByDate: overflowMap };
  }, [
    weekClipped,
    perDateLanes,
    weekDateKeys,
    dateKeyToCol,
    bookingById,
    weekFirstKey,
    weekLastKey,
    effectiveMaxLane
  ]);
  const visibleLanes = Math.min(weekMaxLaneCount, effectiveMaxLane);
  const moreTopOffset = import_bookingCalendarConstants.MONTH_VIEW_BANNER_PAD_Y + visibleLanes * (import_bookingCalendarConstants.MONTH_VIEW_BANNER_H + import_bookingCalendarConstants.MONTH_VIEW_BANNER_GAP);
  const dayGridMin = 0;
  const dayGridSpan = 24;
  const monthPctAxis = {
    originUnit: 0,
    scalePerUnit: 100 / 7,
    tickUnit: 1,
    minSize: 0
  };
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        gridColumn: "1 / -1",
        position: "relative",
        minHeight: 0
      }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "grid h-full grid-cols-7" }, week.map((day) => {
      const isToday = day.key === todayKey;
      return /* @__PURE__ */ import_react.default.createElement(
        "button",
        {
          key: day.key,
          type: "button",
          onClick: () => goToDayFromCell(day.date, null, `month-${day.key}`),
          onDragOver: (e) => e.preventDefault(),
          onDrop: (e) => {
            e.preventDefault();
            handleGridDropMove(day.key);
          },
          className: `flex h-full min-h-0 flex-col overflow-hidden border-r border-b theme-border text-left align-top ${day.inMonth ? isLight ? "theme-grid-cell theme-text" : "bg-[#1a1b21]" : isLight ? "theme-grid-muted theme-text-faint" : "bg-[#111217] text-zinc-600"}`
        },
        /* @__PURE__ */ import_react.default.createElement("div", { className: "flex shrink-0 items-start justify-between gap-1 px-2 pb-1 pt-2" }, /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            className: `text-3xl font-semibold leading-none ${isLight ? "theme-text-soft" : ""}`
          },
          /* @__PURE__ */ import_react.default.createElement(
            "span",
            {
              className: `grid h-10 w-10 place-items-center rounded-full leading-none ${isToday ? isLight ? "ring-2 ring-violet-500/70 bg-violet-50" : "ring-2 ring-violet-400/60 bg-white/[0.06]" : ""}`,
              "aria-label": isToday ? "Today" : void 0
            },
            day.date.getDate()
          )
        ), /* @__PURE__ */ import_react.default.createElement("div", { className: "flex shrink-0 gap-1 pt-0.5" }, day.bookingCount > 0 && /* @__PURE__ */ import_react.default.createElement("span", { className: "h-2.5 w-2.5 rounded-full bg-violet-500" }), (day.blockCount > 0 || day.fullyBooked) && /* @__PURE__ */ import_react.default.createElement("span", { className: "h-2.5 w-2.5 rounded-full bg-red-500" }))),
        /* @__PURE__ */ import_react.default.createElement("div", { className: "min-h-0 flex-1" })
      );
    })),
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        "aria-hidden": true,
        style: {
          position: "absolute",
          top: import_bookingCalendarConstants.MONTH_VIEW_DAY_HEADER_H,
          bottom: 0,
          left: 0,
          right: 0,
          pointerEvents: "none"
        }
      },
      /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          style: {
            position: "relative",
            width: "100%",
            height: "100%"
          }
        },
        banners.map((p) => {
          var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
          const startHourVal = Number.isFinite(Number((_a2 = p.item) == null ? void 0 : _a2.startHour)) ? Number((_b2 = p.item) == null ? void 0 : _b2.startHour) : dayGridMin;
          const endHourVal = Number.isFinite(Number((_c = p.item) == null ? void 0 : _c.endHour)) ? Number((_d = p.item) == null ? void 0 : _d.endHour) : dayGridMin;
          const startUnit = tickFillMode === "snap" ? p.startCol : (0, import_bookingCalendarAxis.dayGridUnit)(p.startCol, startHourVal, dayGridMin, dayGridSpan);
          const endUnit = tickFillMode === "snap" ? p.endCol + 1 : (0, import_bookingCalendarAxis.dayGridUnit)(p.endCol, endHourVal, dayGridMin, dayGridSpan);
          const sp = (0, import_bookingCalendarAxis.mapAxisSpan)(monthPctAxis, startUnit, endUnit, tickFillMode);
          const radiusClass = p.crossDay ? `${p.clippedLeft ? "rounded-l-none" : "rounded-l-md"} ${p.clippedRight ? "rounded-r-none" : "rounded-r-md"}` : "rounded-md";
          const layoutClassName = `flex items-center px-2 text-[11px] leading-none w-full h-full ${radiusClass} ${(0, import_BookingCalendarStyle.getGridBookingRowStateClasses)(
            p.item
          )}`;
          return /* @__PURE__ */ import_react.default.createElement(
            "div",
            {
              key: `${((_e = p.item) == null ? void 0 : _e.id) ?? "b"}-${((_f = week[p.startCol]) == null ? void 0 : _f.key) ?? p.startCol}`,
              style: {
                position: "absolute",
                left: `calc(${sp.offset}% + 4px)`,
                width: `calc(${sp.size}% - 8px)`,
                top: import_bookingCalendarConstants.MONTH_VIEW_BANNER_PAD_Y + p.lane * (import_bookingCalendarConstants.MONTH_VIEW_BANNER_H + import_bookingCalendarConstants.MONTH_VIEW_BANNER_GAP),
                height: import_bookingCalendarConstants.MONTH_VIEW_BANNER_H,
                pointerEvents: "auto"
              },
              onDragOver: (e) => e.preventDefault(),
              onDrop: (e) => {
                var _a3;
                e.preventDefault();
                handleGridDropMove(String(((_a3 = p.item) == null ? void 0 : _a3.date) ?? ""));
              }
            },
            renderMonthWeekBooking ? /* @__PURE__ */ import_react.default.createElement("div", { className: "relative h-full w-full min-h-0" }, renderMonthWeekBooking({
              item: p.item,
              dateKey: (_g = p.item) == null ? void 0 : _g.date,
              view: "month",
              isLight,
              layoutClassName,
              onOpenDetail: openBookingDetail,
              onDragStart: (it) => setDraggingItem(it),
              // 跨日 booking 暂不支持拖拽改约
              draggable: !p.crossDay && ((_h = p.item) == null ? void 0 : _h.kind) !== "block"
            }), /* @__PURE__ */ import_react.default.createElement(
              import_bookingCalendarViewShared.CalendarGridBookingClipMarkers,
              {
                clippedLeft: p.clippedLeft,
                clippedRight: p.clippedRight,
                size: "xs"
              }
            )) : /* @__PURE__ */ import_react.default.createElement(
              "button",
              {
                type: "button",
                onClick: (e) => {
                  e.stopPropagation();
                  openBookingDetail(p.item);
                },
                className: `relative ${layoutClassName}`
              },
              p.clippedLeft && /* @__PURE__ */ import_react.default.createElement(
                "span",
                {
                  "aria-hidden": true,
                  className: "absolute left-0.5 top-1/2 -translate-y-1/2 text-[9px] leading-none opacity-80"
                },
                "◀"
              ),
              p.clippedRight && /* @__PURE__ */ import_react.default.createElement(
                "span",
                {
                  "aria-hidden": true,
                  className: "absolute right-0.5 top-1/2 -translate-y-1/2 text-[9px] leading-none opacity-80"
                },
                "▶"
              ),
              /* @__PURE__ */ import_react.default.createElement("span", { className: "truncate" }, p.crossDay ? `${(0, import_bookingCalendarDateUtils.formatShortMonthDay)((_i = p.item) == null ? void 0 : _i.date)} ${(0, import_bookingCalendarDateUtils.formatHourLabel)(
                (_j = p.item) == null ? void 0 : _j.startHour
              )} ${((_k = p.item) == null ? void 0 : _k.name) ?? ""}` : `${(0, import_bookingCalendarDateUtils.formatHourLabel)((_l = p.item) == null ? void 0 : _l.startHour)} ${((_m = p.item) == null ? void 0 : _m.name) ?? ""}`)
            )
          );
        }),
        Array.from(overflowByDate.entries()).map(([dk, items]) => {
          const colIdx = dateKeyToCol.get(dk) ?? 0;
          const allHits = bookings.filter(
            (b) => (0, import_bookingCalendarDateUtils.getBookingSegmentForDate)(b, dk) != null
          );
          return /* @__PURE__ */ import_react.default.createElement(
            "div",
            {
              key: `more-${dk}`,
              style: {
                position: "absolute",
                left: `calc(${colIdx * 100 / 7}% + 4px)`,
                width: `calc(${100 / 7}% - 8px)`,
                top: moreTopOffset,
                pointerEvents: "auto"
              }
            },
            /* @__PURE__ */ import_react.default.createElement(
              "button",
              {
                type: "button",
                onClick: (e) => {
                  e.stopPropagation();
                  setOverflowListModal(
                    (prev) => prev && prev.scope === "month" && prev.date === dk ? null : {
                      scope: "month",
                      date: dk,
                      title: (0, import_calendarI18n.tCal)(
                        "pisell2.recordBoard.calendar.overflow.listTitleBookings",
                        { date: dk }
                      ),
                      items: allHits
                    }
                  );
                },
                style: { minHeight: import_bookingCalendarConstants.MONTH_VIEW_MORE_TILE_PX },
                className: "flex w-full items-center justify-center rounded-md bg-violet-700/90 px-1 py-0.5 text-[11px] font-semibold text-white"
              },
              "+",
              items.length
            )
          );
        })
      )
    )
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BookingCalendarMonthView
});
