const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_index = require("../../../../components/booking/editBookingModal/index.js");
const require_floorRoomCardUtils = require("../../floorRoomCard/floorRoomCardUtils.js");
const require_RoomCardTooltipTimeLayout = require("../../floorRoomCard/views/RoomCardTooltipTimeLayout.js");
const require_reservationTicketBookingHostParams = require("../../utils/reservationTicketBookingHostParams.js");
const require_bookingDetailModalConstants = require("./bookingDetailModalConstants.js");
const require_BookingDetailUnit = require("./BookingDetailUnit.js");
const require_bookingDetailResourceBookingItem = require("./bookingDetailResourceBookingItem.js");
const require_useBookingDetailActions = require("./useBookingDetailActions.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./ReservationBookingDetailModal.less");
//#region src/plus/pisellReservation/components/bookingDetailModal/ReservationBookingDetailModal.tsx
/**
* 平面图只读点击：预约详情弹窗。
* 内容由宿主 sales.getResourceBookingItem 返回的三段子列表驱动；
* 每条 booking 渲染为「RoomCard + 操作栏 + 商品」一个完整单元。
*/
function ReservationBookingDetailModal(props) {
	var _engineContext$appHel;
	const { open, onClose, onActionSuccess, raw, tableRow, sceneElementName, reservationBiz, associatedMenuListIds, reservationRuleProduct, floorDayBookingList, currentTime, followWallClock } = props;
	const [activeTab, setActiveTab] = (0, react.useState)("current-occupied");
	const engineContext = require_useEngineContext.default();
	const appHelperUtils = engineContext === null || engineContext === void 0 || (_engineContext$appHel = engineContext.appHelper) === null || _engineContext$appHel === void 0 ? void 0 : _engineContext$appHel.utils;
	const { resourceBookingItem, resourceBookingItemLoading, activeTabBookings } = require_bookingDetailResourceBookingItem.useResourceBookingItem(activeTab, {
		open,
		raw,
		tableRow,
		floorDayBookingList,
		currentTime,
		followWallClock,
		pisellos: appHelperUtils === null || appHelperUtils === void 0 ? void 0 : appHelperUtils.pisellos
	});
	/**
	* 各 Tab 当前数量：与 `useResourceBookingItem.activeTabBookings` 同源选段，
	* 用于在 Tab 名后追加 `(N)`。`resourceBookingItem == null`（首次加载未回）
	* 时不计算计数，避免在 Tab 标签上闪过 `(0)`。
	*/
	const tabCounts = (0, react.useMemo)(() => {
		if (resourceBookingItem == null) return null;
		return {
			"current-occupied": resourceBookingItem.bookings.length,
			ended: resourceBookingItem.completedBookings.length,
			unpaid: resourceBookingItem.unpaidBookings.length
		};
	}, [resourceBookingItem]);
	const { editBookingModalRef, isCancellingOrder, isCompletingBooking, handleResourceChangeConfirmed, handleActionCellClick } = require_useBookingDetailActions.useBookingDetailActions({
		raw,
		tableRow,
		reservationBiz,
		associatedMenuListIds,
		onActionSuccess,
		appHelperUtils
	});
	(0, react.useEffect)(() => {
		setActiveTab("current-occupied");
	}, [
		raw === null || raw === void 0 ? void 0 : raw.id,
		raw === null || raw === void 0 ? void 0 : raw.booking_uid,
		raw === null || raw === void 0 ? void 0 : raw.booking_id
	]);
	const detailModalTitle = (0, react.useMemo)(() => {
		var _tableRow$code;
		if (tableRow == null) return _pisell_utils.locales.getText(`${require_bookingDetailModalConstants.I18N_BD}.modal-title`);
		const roomTitle = require_floorRoomCardUtils.displayRoomTitleFromRow(tableRow, sceneElementName != null && String(sceneElementName).trim() !== "" ? String(sceneElementName).trim() : void 0).trim();
		const code = (_tableRow$code = tableRow.code) === null || _tableRow$code === void 0 ? void 0 : _tableRow$code.trim();
		/**
		* 标题尾部按 `(N/M)` 拼当前资源订单计数，与平面图大卡 header / 小卡 tooltip
		* 同源；helper 接口为通用 `BookingCountSource`，`PisellReservationTableRow`
		* 的 `bookingCount` / `completeBookingCount` 字段结构化命中即可。
		* `bookingCount` 缺失或为 0 时不渲染计数，标题与之前完全一致。
		*/
		return require_RoomCardTooltipTimeLayout.appendBookingCountSuffix(roomTitle || code || _pisell_utils.locales.getText("pisell-reservation.room-kind-bound"), tableRow);
	}, [tableRow, sceneElementName]);
	const canShow = Boolean(open && raw != null);
	const handleHeaderCloseClick = (0, react.useCallback)((e) => {
		require_bookingDetailModalConstants.stopUiClick(e);
		onClose(e);
	}, [onClose]);
	const handleHeaderNewOrderClick = (0, react.useCallback)((e) => {
		require_bookingDetailModalConstants.stopUiClick(e);
		require_reservationTicketBookingHostParams.openNewReservationBookingFromHost({
			appHelperUtils,
			reservationBiz,
			associatedMenuListIds,
			reservationRuleProduct,
			row: tableRow !== null && tableRow !== void 0 ? tableRow : null
		});
	}, [
		appHelperUtils,
		reservationBiz,
		associatedMenuListIds,
		reservationRuleProduct,
		tableRow
	]);
	const detailModalHeader = (0, react.useMemo)(() => /* @__PURE__ */ react.default.createElement("div", { className: `${require_bookingDetailModalConstants.PREFIX}__header` }, /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: `${require_bookingDetailModalConstants.PREFIX}__header-action ${require_bookingDetailModalConstants.PREFIX}__header-action--left`,
		onClick: handleHeaderCloseClick
	}, require_bookingDetailModalConstants.t("header-close")), /* @__PURE__ */ react.default.createElement("div", { className: `${require_bookingDetailModalConstants.PREFIX}__header-title` }, detailModalTitle), /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: `${require_bookingDetailModalConstants.PREFIX}__header-action ${require_bookingDetailModalConstants.PREFIX}__header-action--right`,
		onClick: handleHeaderNewOrderClick
	}, require_bookingDetailModalConstants.t("header-new-order"))), [
		detailModalTitle,
		handleHeaderCloseClick,
		handleHeaderNewOrderClick
	]);
	const getApp = appHelperUtils === null || appHelperUtils === void 0 ? void 0 : appHelperUtils.getApp;
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		platform: "pc",
		open: canShow,
		onCancel: onClose,
		maskProps: require_bookingDetailModalConstants.BD_MODAL_MASK,
		focusTriggerAfterClose: false,
		title: detailModalHeader,
		closable: false,
		footer: null,
		destroyOnClose: true,
		width: 560,
		wrapClassName: `${require_bookingDetailModalConstants.PREFIX}-wrap`,
		zIndex: 900
	}, raw ? /* @__PURE__ */ react.default.createElement("div", { className: require_bookingDetailModalConstants.PREFIX }, /* @__PURE__ */ react.default.createElement("div", { className: `${require_bookingDetailModalConstants.PREFIX}__body` }, /* @__PURE__ */ react.default.createElement("div", {
		className: `${require_bookingDetailModalConstants.PREFIX}__tabs`,
		role: "tablist",
		"aria-label": require_bookingDetailModalConstants.t("tabs-aria")
	}, require_bookingDetailModalConstants.DETAIL_TAB_ITEMS.map(({ key, labelKey }) => {
		const active = activeTab === key;
		const count = tabCounts === null || tabCounts === void 0 ? void 0 : tabCounts[key];
		return /* @__PURE__ */ react.default.createElement("button", {
			key,
			type: "button",
			role: "tab",
			"aria-selected": active,
			className: `${require_bookingDetailModalConstants.PREFIX}__tab${active ? ` ${require_bookingDetailModalConstants.PREFIX}__tab--active` : ""}`,
			onClick: (e) => {
				require_bookingDetailModalConstants.stopUiClick(e);
				setActiveTab(key);
			}
		}, require_bookingDetailModalConstants.t(labelKey), count != null ? /* @__PURE__ */ react.default.createElement("span", { className: `${require_bookingDetailModalConstants.PREFIX}__tab-count` }, " ", "(", count, ")") : null);
	})), /* @__PURE__ */ react.default.createElement("div", {
		className: `${require_bookingDetailModalConstants.PREFIX}__bookings-list`,
		role: "tabpanel",
		"aria-label": require_bookingDetailModalConstants.t(`tab-${activeTab}`)
	}, activeTabBookings.length === 0 ? !resourceBookingItemLoading ? /* @__PURE__ */ react.default.createElement("div", { className: `${require_bookingDetailModalConstants.PREFIX}__bookings-empty` }, require_bookingDetailModalConstants.t("empty-tab-bookings")) : null : activeTabBookings.map((bookingRaw, idx) => {
		var _ref, _ref2, _bookingRaw$id;
		const unitKey = String((_ref = (_ref2 = (_bookingRaw$id = bookingRaw.id) !== null && _bookingRaw$id !== void 0 ? _bookingRaw$id : bookingRaw.booking_uid) !== null && _ref2 !== void 0 ? _ref2 : bookingRaw.booking_id) !== null && _ref !== void 0 ? _ref : idx);
		return /* @__PURE__ */ react.default.createElement(require_BookingDetailUnit.BookingDetailUnit, {
			key: unitKey,
			bookingRaw,
			tableRow,
			isCancellingOrder,
			isCompletingBooking,
			getApp,
			onActionCellClick: handleActionCellClick
		});
	})))) : null), /* @__PURE__ */ react.default.createElement(require_index.default, {
		ref: editBookingModalRef,
		open: false,
		onCancel: () => {},
		onOk: handleResourceChangeConfirmed
	}));
}
//#endregion
exports.ReservationBookingDetailModal = ReservationBookingDetailModal;
