import { findReservationRow } from "./reservationCards.js";
import React, { useMemo } from "react";
import { locales } from "@pisell/utils";
import { FLOOR_MAP_IMAGE_ELEMENT_KIND, FLOOR_MAP_STAGE_ELEMENT_KIND } from "@pisell/materials";
import classNames from "classnames";
import { DownOutlined, UpOutlined } from "@ant-design/icons";
import "./ReservationFloorMapHud.less";
//#region src/plus/pisellReservation/floorMap/ReservationFloorMapHud.tsx
/**
* 预约平面图视口悬浮：当前画布桌位总数与各状态数量（空闲 / 预约 / 使用中等）
*/
/** 与 RoomCard 右上角状态标签共用文案键，保证画布统计与图元一致 */
const I18N_TOTAL = "pisell-reservation.floor-hud.total";
const I18N_INTERACTIVE_TITLE = "pisell-reservation.floor-hud.interactive-title";
const I18N_STATUS_EMPTY = "pisell-reservation.room-status-empty";
const I18N_STATUS_RESERVED = "pisell-reservation.room-status-reserved";
const I18N_STATUS_OCCUPIED = "pisell-reservation.room-status-occupied";
const I18N_STATUS_LOCKED = "pisell-reservation.room-status-locked";
const I18N_STATUS_UNBOUND = "pisell-reservation.room-status-unbound";
function isSeatLikeElement(item) {
	const k = item.elementKind;
	if (!k || k === FLOOR_MAP_IMAGE_ELEMENT_KIND || k === FLOOR_MAP_STAGE_ELEMENT_KIND) return false;
	return true;
}
function computeHudSeatStats(items, dataSources) {
	let total = 0;
	let idle = 0;
	let booked = 0;
	let occupied = 0;
	let maintenance = 0;
	let unbound = 0;
	const ds = dataSources !== null && dataSources !== void 0 ? dataSources : {};
	for (const item of items) {
		if (!isSeatLikeElement(item)) continue;
		total += 1;
		const row = findReservationRow(item, ds);
		if (!row) {
			unbound += 1;
			continue;
		}
		switch (row.cardStatus) {
			case "available":
				idle += 1;
				break;
			case "booked":
				booked += 1;
				break;
			case "occupied":
			case "barBusy":
				occupied += 1;
				break;
			case "maintenance":
				maintenance += 1;
				break;
			default: idle += 1;
		}
	}
	return {
		total,
		idle,
		booked,
		occupied,
		maintenance,
		unbound
	};
}
/** 场控左下抽屉：无台位且未展开时可隐藏整块 */
function reservationFloorMapHudSeatStatsTotal(items, dataSources) {
	return computeHudSeatStats(items, dataSources).total;
}
function buildHudStatRows(stats) {
	const rows = [
		{
			label: locales.getText(I18N_TOTAL),
			value: stats.total
		},
		{
			label: locales.getText(I18N_STATUS_EMPTY),
			value: stats.idle
		},
		{
			label: locales.getText(I18N_STATUS_RESERVED),
			value: stats.booked
		}
	];
	if (stats.occupied > 0) rows.push({
		label: locales.getText(I18N_STATUS_OCCUPIED),
		value: stats.occupied
	});
	if (stats.maintenance > 0) rows.push({
		label: locales.getText(I18N_STATUS_LOCKED),
		value: stats.maintenance
	});
	if (stats.unbound > 0) rows.push({
		label: locales.getText(I18N_STATUS_UNBOUND),
		value: stats.unbound
	});
	return rows;
}
/**
* 场控：单行标题栏 = 标题 + 内联统计 + 展开/收起，与 Sheet 顶栏共用。
*/
function ReservationFloorMapHudTitleBar(props) {
	const { items, dataSources, actionMode, onAction, embedded, className } = props;
	const stats = useMemo(() => computeHudSeatStats(items, dataSources), [items, dataSources]);
	const rows = useMemo(() => buildHudStatRows(stats), [stats]);
	if (stats.total === 0) {
		if (actionMode === "collapse") {
			const collapseAria = locales.getText("pisell-reservation.hud-collapse-aria");
			const regionLabel = locales.getText("pisell-reservation.hud-table-drawer-title");
			return /* @__PURE__ */ React.createElement("div", {
				className: classNames("pisell-reservation-floor-map-hud", "pisell-reservation-floor-map-hud--interactive-title-bar", embedded && "pisell-reservation-floor-map-hud--embedded", "pisell-reservation-floor-map-hud-title-bar--minimal", className),
				role: "region",
				"aria-label": regionLabel
			}, /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-floor-map-hud-title-bar-layout" }, /* @__PURE__ */ React.createElement("span", { className: "pisell-reservation-floor-map-hud-heading" }, locales.getText(I18N_INTERACTIVE_TITLE)), /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-floor-map-hud-title-bar-stats" }), /* @__PURE__ */ React.createElement("button", {
				type: "button",
				className: "pisell-reservation-floor-map-hud-chevron-btn",
				"aria-expanded": true,
				"aria-label": collapseAria,
				onClick: (e) => {
					e.stopPropagation();
					onAction();
				}
			}, /* @__PURE__ */ React.createElement(UpOutlined, null))));
		}
		return null;
	}
	const actionAria = actionMode === "expand" ? locales.getText("pisell-reservation.hud-expand-aria") : locales.getText("pisell-reservation.hud-collapse-aria");
	const regionAria = locales.getText("pisell-reservation.hud-table-drawer-title");
	return /* @__PURE__ */ React.createElement("div", {
		className: classNames("pisell-reservation-floor-map-hud", "pisell-reservation-floor-map-hud--interactive-title-bar", embedded && "pisell-reservation-floor-map-hud--embedded", className),
		role: "region",
		"aria-label": regionAria
	}, /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-floor-map-hud-title-bar-layout" }, /* @__PURE__ */ React.createElement("span", { className: "pisell-reservation-floor-map-hud-heading" }, locales.getText(I18N_INTERACTIVE_TITLE)), /* @__PURE__ */ React.createElement("div", { className: "pisell-reservation-floor-map-hud-title-bar-stats" }, rows.map((r, i) => /* @__PURE__ */ React.createElement(React.Fragment, { key: r.label }, i > 0 ? /* @__PURE__ */ React.createElement("span", {
		className: "pisell-reservation-floor-map-hud-sep",
		"aria-hidden": true
	}, "·") : null, /* @__PURE__ */ React.createElement("span", { className: "pisell-reservation-floor-map-hud-inline-item" }, /* @__PURE__ */ React.createElement("span", { className: "pisell-reservation-floor-map-hud-label" }, r.label), /* @__PURE__ */ React.createElement("span", { className: "pisell-reservation-floor-map-hud-value" }, r.value))))), /* @__PURE__ */ React.createElement("button", {
		type: "button",
		className: "pisell-reservation-floor-map-hud-chevron-btn",
		"aria-expanded": actionMode === "collapse",
		"aria-label": actionAria,
		onClick: (e) => {
			e.stopPropagation();
			onAction();
		}
	}, actionMode === "expand" ? /* @__PURE__ */ React.createElement(DownOutlined, null) : /* @__PURE__ */ React.createElement(UpOutlined, null))));
}
function ReservationFloorMapHud(props) {
	const stats = useMemo(() => computeHudSeatStats(props.items, props.dataSources), [props.items, props.dataSources]);
	if (props.interactive && props.onRequestOpenTable) {
		const expanded = Boolean(props.tableDrawerOpen);
		if (stats.total === 0 && !expanded) return null;
		return /* @__PURE__ */ React.createElement(ReservationFloorMapHudTitleBar, {
			items: props.items,
			dataSources: props.dataSources,
			actionMode: expanded ? "collapse" : "expand",
			onAction: () => {
				var _props$onRequestClose;
				return expanded ? (_props$onRequestClose = props.onRequestCloseTable) === null || _props$onRequestClose === void 0 ? void 0 : _props$onRequestClose.call(props) : props.onRequestOpenTable();
			}
		});
	}
	if (stats.total === 0) return null;
	const rows = buildHudStatRows(stats);
	return /* @__PURE__ */ React.createElement("div", {
		className: "pisell-reservation-floor-map-hud",
		role: "status"
	}, /* @__PURE__ */ React.createElement("ul", { className: "pisell-reservation-floor-map-hud-list" }, rows.map((r) => /* @__PURE__ */ React.createElement("li", {
		key: r.label,
		className: "pisell-reservation-floor-map-hud-row"
	}, /* @__PURE__ */ React.createElement("span", { className: "pisell-reservation-floor-map-hud-label" }, r.label), /* @__PURE__ */ React.createElement("span", { className: "pisell-reservation-floor-map-hud-value" }, r.value)))));
}
//#endregion
export { ReservationFloorMapHud, ReservationFloorMapHudTitleBar, reservationFloorMapHudSeatStatsTotal };
