const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_floorRoomCardUtils = require("./floorRoomCardUtils.js");
const require_floorRoomCardDom = require("./floorRoomCardDom.js");
const require_buildCardBodyViewModel = require("./buildCardBodyViewModel.js");
const require_floorRoomCardHeaderFormat = require("./floorRoomCardHeaderFormat.js");
const require_RoomCardBodyView = require("./views/RoomCardBodyView.js");
const require_RoomCardHeaderView = require("./views/RoomCardHeaderView.js");
const require_RoomCardShellView = require("./views/RoomCardShellView.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./ReservationFloorRoomCard.less");
//#region src/plus/pisellReservation/floorRoomCard/FloorRoomCardFromData.tsx
/**
* 由 {@link FloorRoomCardData} 渲染 RoomCard 内容（Shell + HeaderView + BodyView），
* 不含根节点 `pisell-res-floor-room-card`、不含每分钟 tick；平面图容器与弹窗分别外包。
*/
const I18N_STATUS_LOCK = "pisell-reservation.room-status-locked";
const I18N_VIEW_ALL = "pisell-reservation.room-view-all";
const I18N_MODAL_BOOKINGS = "pisell-reservation.room-modal-bookings";
const I18N_NEXT = "pisell-reservation.room-next-prefix";
function i18nN(key, n) {
	return _pisell_utils.locales.getText(key).replace(/\{n\}/g, String(n));
}
/** Next 条客人展示：与 {@link buildCardBodyViewModel} guestDisplayLine 一致（预留态 showPhone） */
function nextGuestLineForStrip(b) {
	var _b$holder, _b$holder2;
	const n = (_b$holder = b.holder) === null || _b$holder === void 0 || (_b$holder = _b$holder.name) === null || _b$holder === void 0 ? void 0 : _b$holder.trim();
	if (n) return n;
	const ph = (_b$holder2 = b.holder) === null || _b$holder2 === void 0 ? void 0 : _b$holder2.phone;
	if (ph) {
		const m = require_floorRoomCardUtils.maskPisellReservationPhoneTail(ph);
		if (m) return m;
	}
	return null;
}
const floorCardStopCanvasPanHandlers = {
	onPointerDown: (e) => e.stopPropagation(),
	onPointerMove: (e) => e.stopPropagation(),
	onPointerUp: (e) => e.stopPropagation(),
	onPointerCancel: (e) => e.stopPropagation(),
	onMouseDown: (e) => e.stopPropagation(),
	onMouseMove: (e) => e.stopPropagation(),
	onTouchStart: (e) => e.stopPropagation(),
	onTouchMove: (e) => e.stopPropagation(),
	onTouchEnd: (e) => e.stopPropagation(),
	onWheel: (e) => e.stopPropagation()
};
function scheduleCloseViewAllBookingsModal(setOpen, e) {
	var _e$nativeEvent, _e$nativeEvent$stopIm;
	e === null || e === void 0 || e.stopPropagation();
	e === null || e === void 0 || (_e$nativeEvent = e.nativeEvent) === null || _e$nativeEvent === void 0 || (_e$nativeEvent$stopIm = _e$nativeEvent.stopImmediatePropagation) === null || _e$nativeEvent$stopIm === void 0 || _e$nativeEvent$stopIm.call(_e$nativeEvent);
	window.setTimeout(() => setOpen(false), 50);
}
const FLOOR_CARD_VIEW_ALL_MODAL_MASK_PROPS = {
	onMouseDown: (e) => e.stopPropagation(),
	onClick: (e) => e.stopPropagation()
};
/** 与 `ReservationFloorRoomCard.less` 中 `.pisell-lowcode-modal-body` padding 配套 */
const FLOOR_CARD_VIEW_ALL_MODAL_WRAP_CLASS = "pisell-res-floor-view-all-modal-wrap";
const MULTI_PREVIEW_MAX = 3;
const CIRCLE_MULTI_MORE_BTN_RESERVE_PX = 46;
function circleMultiCompactRowEst(innerDiameterPx) {
	return Math.round(Math.max(42, Math.min(64, innerDiameterPx * .15)));
}
function resolveCircleMultiPreviewCount(shellShape, bookingCount, layoutHeightPx) {
	if (bookingCount < 1) return 0;
	if (shellShape !== "circle") return Math.min(MULTI_PREVIEW_MAX, bookingCount);
	const H = layoutHeightPx != null && Number.isFinite(layoutHeightPx) && layoutHeightPx > 0 ? layoutHeightPx : null;
	if (H == null || H < 48) {
		if (bookingCount > MULTI_PREVIEW_MAX) return 1;
		return Math.min(MULTI_PREVIEW_MAX, bookingCount);
	}
	const padY = Math.round(Math.max(7, Math.min(18, H * .065)));
	const inner = Math.max(0, H - padY * 2);
	const headerReserve = Math.min(115, Math.max(54, inner * .35));
	const listMax = Math.max(0, inner - headerReserve - 10);
	const moreH = CIRCLE_MULTI_MORE_BTN_RESERVE_PX;
	const rowEst = circleMultiCompactRowEst(inner);
	const topK = Math.min(MULTI_PREVIEW_MAX, bookingCount);
	if (listMax < moreH && bookingCount > topK) return 0;
	for (let k = topK; k >= 0; k--) {
		const needMore = bookingCount > k;
		if (k * rowEst + (needMore ? moreH + (k > 0 ? 6 : 0) : 0) <= listMax) return k;
	}
	return 0;
}
function effectiveRoomTitle(data, headerMainFieldOverride) {
	return (headerMainFieldOverride === null || headerMainFieldOverride === void 0 ? void 0 : headerMainFieldOverride.trim()) || data.main_field;
}
function CardHeader(props) {
	const { data, occupancyBookings, hideCapacity = false, headerMainFieldOverride } = props;
	const bookingsForOcc = occupancyBookings !== void 0 ? occupancyBookings : data.bookings;
	const capLabel = hideCapacity ? null : require_floorRoomCardHeaderFormat.formatRoomCapLine(data, bookingsForOcc);
	return /* @__PURE__ */ react.default.createElement(require_RoomCardHeaderView.RoomCardHeaderView, {
		roomTitle: effectiveRoomTitle(data, headerMainFieldOverride),
		capacityLine: capLabel,
		status: data.status,
		statusLabel: require_floorRoomCardHeaderFormat.floorRoomCardStatusTagLabel(data.status)
	});
}
function buildRoomViewAllModalTitle(data, bookingsForOcc, headerMainFieldOverride) {
	const capLabel = require_floorRoomCardHeaderFormat.formatRoomCapLine(data, bookingsForOcc);
	const n = bookingsForOcc.length;
	const bookingsLine = i18nN(I18N_MODAL_BOOKINGS, n);
	const head = effectiveRoomTitle(data, headerMainFieldOverride);
	return /* @__PURE__ */ react.default.createElement("div", { className: "rc-view-all-modal-title" }, head, " · ", bookingsLine, " · ", capLabel);
}
function CardBody(props) {
	const { cardType, booking, data, compact = false, shellShape = "rect" } = props;
	const model = require_buildCardBodyViewModel.buildRoomCardBodyViewModel({
		cardType,
		booking,
		data,
		compact,
		shellShape,
		nowMs: Date.now()
	});
	return /* @__PURE__ */ react.default.createElement(require_RoomCardBodyView.RoomCardBodyView, {
		model,
		scrollSurfaceHandlers: floorCardStopCanvasPanHandlers,
		nextBookingPrefix: _pisell_utils.locales.getText(I18N_NEXT)
	});
}
const CARD_CLASS = {
	not_arrived: "rc rc--reserved",
	late: "rc rc--reserved",
	payed: "rc rc--occupied",
	payment_required: "rc rc--occupied rc--occupied-pay",
	timeout: "rc rc--occupied rc--occupied-timeout"
};
function StandardCard(props) {
	const { data, cardType, hideHeaderCapacity = false, headerMainFieldOverride, shellShape = "rect" } = props;
	const booking = require_floorRoomCardUtils.getFloorRoomCardPrimaryBooking(data);
	if (!booking) return /* @__PURE__ */ react.default.createElement(EmptyCard, {
		data,
		hideHeaderCapacity,
		headerMainFieldOverride
	});
	return /* @__PURE__ */ react.default.createElement(require_RoomCardShellView.RoomCardShellView, {
		className: CARD_CLASS[cardType],
		checkoutBookingKey: require_floorRoomCardDom.floorRoomCardBookingCheckoutDomKey(booking),
		header: /* @__PURE__ */ react.default.createElement(CardHeader, {
			data,
			hideCapacity: hideHeaderCapacity,
			headerMainFieldOverride
		})
	}, /* @__PURE__ */ react.default.createElement(CardBody, {
		cardType,
		booking,
		data,
		shellShape
	}));
}
function EmptyCard(props) {
	const { data, hideHeaderCapacity, headerMainFieldOverride } = props;
	return /* @__PURE__ */ react.default.createElement(require_RoomCardShellView.RoomCardShellView, {
		className: "rc rc--empty",
		header: /* @__PURE__ */ react.default.createElement(CardHeader, {
			data,
			hideCapacity: hideHeaderCapacity,
			headerMainFieldOverride
		})
	});
}
function MultiOccupiedCard(props) {
	const { data, shellShape = "rect", elementLayoutHeightPx, hideHeaderCapacity, headerMainFieldOverride } = props;
	const [modalOpen, setModalOpen] = (0, react.useState)(false);
	const mainBookings = data.bookings.filter((b) => b.isNext !== true);
	const listForPreview = mainBookings.length > 0 ? mainBookings : data.bookings;
	const explicitNextOcc = require_floorRoomCardUtils.getFloorRoomCardExplicitNextBooking(data);
	const n = listForPreview.length;
	const previewCount = resolveCircleMultiPreviewCount(shellShape, n, elementLayoutHeightPx);
	const preview = listForPreview.slice(0, previewCount);
	const hasMore = n > preview.length;
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_RoomCardShellView.RoomCardShellView, {
		className: (0, classnames.default)("rc rc--occupied rc--multi", modalOpen && "rc--floor-detail-open"),
		header: /* @__PURE__ */ react.default.createElement(CardHeader, {
			data,
			occupancyBookings: data.bookings,
			hideCapacity: hideHeaderCapacity,
			headerMainFieldOverride
		})
	}, /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({
		className: "rc-multi-list",
		"data-pisell-floor-room-scroll-surface": ""
	}, floorCardStopCanvasPanHandlers), preview.map((b) => {
		const perType = require_floorRoomCardUtils.resolveFloorRoomCardProgressPercent(b) >= 100 ? "timeout" : data.sub_status === "payed" ? "payed" : "payment_required";
		return /* @__PURE__ */ react.default.createElement(CardBody, {
			key: require_floorRoomCardDom.floorRoomCardBookingCheckoutDomKey(b),
			cardType: perType,
			booking: b,
			data,
			compact: true,
			shellShape
		});
	}), explicitNextOcc ? /* @__PURE__ */ react.default.createElement(require_RoomCardBodyView.RoomCardNextBookingStrip, {
		nextLabel: _pisell_utils.locales.getText(I18N_NEXT),
		guestLine: nextGuestLineForStrip(explicitNextOcc),
		timeRange: `${explicitNextOcc.start_time} - ${explicitNextOcc.end_time}`
	}) : null, hasMore ? /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: "rc-multi-more",
		onClick: (e) => {
			e.stopPropagation();
			setModalOpen(true);
		}
	}, i18nN(I18N_VIEW_ALL, data.bookings.length)) : null)), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open: modalOpen,
		onCancel: (e) => scheduleCloseViewAllBookingsModal(setModalOpen, e),
		maskProps: FLOOR_CARD_VIEW_ALL_MODAL_MASK_PROPS,
		focusTriggerAfterClose: false,
		wrapClassName: FLOOR_CARD_VIEW_ALL_MODAL_WRAP_CLASS,
		title: buildRoomViewAllModalTitle(data, data.bookings, headerMainFieldOverride),
		footer: null,
		destroyOnClose: true,
		width: 560
	}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("pisell-res-floor-room-card", "pisell-res-floor-room-card--modal-shell") }, /* @__PURE__ */ react.default.createElement("div", { className: "rc--occupied rc--multi" }, /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({ className: "rc-multi-modal-body" }, floorCardStopCanvasPanHandlers), data.bookings.map((b, bi) => {
		const perType = require_floorRoomCardUtils.resolveFloorRoomCardProgressPercent(b) >= 100 ? "timeout" : data.sub_status === "payed" ? "payed" : "payment_required";
		return /* @__PURE__ */ react.default.createElement(CardBody, {
			key: `${require_floorRoomCardDom.floorRoomCardBookingCheckoutDomKey(b)}-${bi}`,
			cardType: perType,
			booking: b,
			data,
			compact: true,
			shellShape: "rect"
		});
	}))))));
}
function MultiReservedCard(props) {
	const { data, shellShape = "rect", elementLayoutHeightPx, hideHeaderCapacity, headerMainFieldOverride } = props;
	const [modalOpen, setModalOpen] = (0, react.useState)(false);
	const sorted = require_floorRoomCardUtils.reorderBookingsAllReservedChronological([...data.bookings]);
	const mainReserved = sorted.filter((b) => b.isNext !== true);
	const listForReservedPreview = mainReserved.length > 0 ? mainReserved : sorted;
	const explicitNextRes = require_floorRoomCardUtils.getFloorRoomCardExplicitNextBooking(data);
	const n = listForReservedPreview.length;
	const previewCount = resolveCircleMultiPreviewCount(shellShape, n, elementLayoutHeightPx);
	const preview = listForReservedPreview.slice(0, previewCount);
	const hasMore = n > preview.length;
	const nowMs = Date.now();
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(require_RoomCardShellView.RoomCardShellView, {
		className: (0, classnames.default)("rc rc--reserved rc--multi", modalOpen && "rc--floor-detail-open"),
		header: /* @__PURE__ */ react.default.createElement(CardHeader, {
			data,
			occupancyBookings: sorted,
			hideCapacity: hideHeaderCapacity,
			headerMainFieldOverride
		})
	}, /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({
		className: "rc-multi-list",
		"data-pisell-floor-room-scroll-surface": ""
	}, floorCardStopCanvasPanHandlers), preview.map((b) => {
		const perType = require_floorRoomCardUtils.getFloorRoomCardReservedRowBodyType(b, nowMs);
		return /* @__PURE__ */ react.default.createElement(CardBody, {
			key: require_floorRoomCardDom.floorRoomCardBookingCheckoutDomKey(b),
			cardType: perType,
			booking: b,
			data,
			compact: true,
			shellShape
		});
	}), explicitNextRes ? /* @__PURE__ */ react.default.createElement(require_RoomCardBodyView.RoomCardNextBookingStrip, {
		nextLabel: _pisell_utils.locales.getText(I18N_NEXT),
		guestLine: nextGuestLineForStrip(explicitNextRes),
		timeRange: `${explicitNextRes.start_time} - ${explicitNextRes.end_time}`
	}) : null, hasMore ? /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: "rc-multi-more",
		onClick: (e) => {
			e.stopPropagation();
			setModalOpen(true);
		}
	}, i18nN(I18N_VIEW_ALL, sorted.length)) : null)), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		open: modalOpen,
		onCancel: (e) => scheduleCloseViewAllBookingsModal(setModalOpen, e),
		maskProps: FLOOR_CARD_VIEW_ALL_MODAL_MASK_PROPS,
		focusTriggerAfterClose: false,
		wrapClassName: FLOOR_CARD_VIEW_ALL_MODAL_WRAP_CLASS,
		title: buildRoomViewAllModalTitle(data, sorted, headerMainFieldOverride),
		footer: null,
		destroyOnClose: true,
		width: 560
	}, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("pisell-res-floor-room-card", "pisell-res-floor-room-card--modal-shell") }, /* @__PURE__ */ react.default.createElement("div", { className: "rc--reserved rc--multi" }, /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({ className: "rc-multi-modal-body" }, floorCardStopCanvasPanHandlers), sorted.map((b, bi) => /* @__PURE__ */ react.default.createElement(CardBody, {
		key: `${require_floorRoomCardDom.floorRoomCardBookingCheckoutDomKey(b)}-${bi}`,
		cardType: require_floorRoomCardUtils.getFloorRoomCardReservedRowBodyType(b, Date.now()),
		booking: b,
		data,
		compact: true,
		shellShape: "rect"
	})))))));
}
function LockedCard(props) {
	const { data, hideHeaderCapacity, headerMainFieldOverride } = props;
	const capLabel = hideHeaderCapacity ? null : require_floorRoomCardHeaderFormat.formatRoomCapLine(data, []);
	return /* @__PURE__ */ react.default.createElement(require_RoomCardShellView.RoomCardShellView, {
		className: "rc rc--locked",
		header: /* @__PURE__ */ react.default.createElement(require_RoomCardHeaderView.RoomCardHeaderView, {
			roomTitle: effectiveRoomTitle(data, headerMainFieldOverride),
			capacityLine: capLabel,
			status: "locked",
			statusLabel: "",
			showStatusTag: false
		})
	}, /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2({
		className: "rc-body rc-body--center",
		"data-pisell-floor-room-scroll-surface": ""
	}, floorCardStopCanvasPanHandlers), /* @__PURE__ */ react.default.createElement("div", { className: "rc-lock-icon" }, /* @__PURE__ */ react.default.createElement("svg", {
		width: "28",
		height: "28",
		viewBox: "0 0 24 24",
		fill: "none",
		stroke: "currentColor",
		strokeWidth: "1.5",
		strokeLinecap: "round",
		strokeLinejoin: "round"
	}, /* @__PURE__ */ react.default.createElement("rect", {
		x: "3",
		y: "11",
		width: "18",
		height: "11",
		rx: "2",
		ry: "2"
	}), /* @__PURE__ */ react.default.createElement("path", { d: "M7 11V7a5 5 0 0 1 10 0v4" }))), /* @__PURE__ */ react.default.createElement("span", { className: "rc-locked-text" }, _pisell_utils.locales.getText(I18N_STATUS_LOCK))));
}
const SPECIAL_RENDERERS = {
	empty: EmptyCard,
	multi_occupied: MultiOccupiedCard,
	multi_reserved: MultiReservedCard,
	locked: LockedCard
};
/**
* 与 {@link ReservationFloorRoomCard} 同款 DOM 与分支，无外层主题根、无定时刷新。
*/
function FloorRoomCardFromData(props) {
	const { data, shellShape = "rect", elementLayoutHeightPx, hideHeaderCapacity = false, headerMainFieldOverride } = props;
	const cardType = require_floorRoomCardUtils.getFloorRoomCardType(data);
	const Special = SPECIAL_RENDERERS[cardType];
	if (Special) return /* @__PURE__ */ react.default.createElement(Special, {
		data,
		shellShape,
		elementLayoutHeightPx,
		hideHeaderCapacity,
		headerMainFieldOverride
	});
	if (cardType in CARD_CLASS) return /* @__PURE__ */ react.default.createElement(StandardCard, {
		data,
		cardType,
		hideHeaderCapacity,
		headerMainFieldOverride,
		shellShape
	});
	return /* @__PURE__ */ react.default.createElement(EmptyCard, {
		data,
		shellShape,
		elementLayoutHeightPx,
		hideHeaderCapacity,
		headerMainFieldOverride
	});
}
//#endregion
exports.FloorRoomCardFromData = FloorRoomCardFromData;
