const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_index = require("../../../../locales/index.js");
const require_venueWallUtils = require("./venueWallUtils.js");
const require_ResourceWallControlPanelColorTab = require("./ResourceWallControlPanelColorTab.js");
const require_ResourceWallControlPanelLayoutTab = require("./ResourceWallControlPanelLayoutTab.js");
let react = require("react");
react = require_runtime.__toESM(react);
let react_dom = require("react-dom");
//#region src/components/pisellRecordBoard/shellFrame/ResourceWall/ResourceWallControlPanel.tsx
/** 将面板内草稿压成一次 `onApply` 负载（行列、秒数、色值等做安全钳位） */
const buildApplyPayload = (resourceIds, draft, boardingSoonBeforeMin) => ({
	resourceIds: [...resourceIds],
	layoutKey: draft.draftLayoutKey,
	customCols: Math.max(1, Math.min(6, draft.draftCustomCols)),
	customRows: Math.max(1, Math.min(6, draft.draftCustomRows)),
	pageDurationSec: Math.max(3, Math.min(60, Math.round(draft.draftPageSec))),
	occupiedCardColor: draft.draftOcc,
	availableCardColor: draft.draftAvail,
	boardingSoonBeforeMin: Math.max(1, Math.min(60, Math.round(boardingSoonBeforeMin))),
	statusToneOverrides: require_venueWallUtils.diffVenueWallStatusToneOverrides(draft.draftTones),
	heroMaxWidthRatio: Math.max(.4, Math.min(.8, draft.draftHeroRatio))
});
/**
* 资源墙管理台（Portal）：选资源、网格预设/自定义行列、翻页秒数、占用/空闲色、六态说明与色值；
* `onApply` 回传负载中含 `diffVenueWallStatusToneOverrides` 压缩结果。
* 源自 test `showAdmin` 对接；关闭（Close / Escape / 遮罩）时提交草稿并关闭。
*/
const ResourceWallControlPanel = ({ open, onClose, isDark, layoutPresets, layoutKey, customCols, customRows, pageDurationSec, occupiedCardColor, availableCardColor, boardingSoonBeforeMin, statusToneOverrides, selectedResourceIds, onApply, carouselPager, heroMaxWidthRatio: heroMaxWidthRatioProp = .6 }) => {
	const [adminTab, setAdminTab] = (0, react.useState)("resource_layout");
	const [draftLayoutKey, setDraftLayoutKey] = (0, react.useState)(layoutKey);
	const [draftCustomCols, setDraftCustomCols] = (0, react.useState)(customCols);
	const [draftCustomRows, setDraftCustomRows] = (0, react.useState)(customRows);
	const [draftPageSec, setDraftPageSec] = (0, react.useState)(pageDurationSec);
	const [draftOcc, setDraftOcc] = (0, react.useState)(occupiedCardColor);
	const [draftAvail, setDraftAvail] = (0, react.useState)(availableCardColor);
	const [draftHeroRatio, setDraftHeroRatio] = (0, react.useState)(heroMaxWidthRatioProp);
	const [draftTones, setDraftTones] = (0, react.useState)(() => require_venueWallUtils.getVenueWallStatusToneMap());
	/**
	* 弹层打开瞬间从 props 灌入草稿；effect **只依赖 open**，避免父组件每秒重绘（时钟等）时
	* 因 layoutKey 等引用变化再次执行 effect 把版式/配色草稿刷回 props。
	*/
	const draftBootstrapRef = (0, react.useRef)({
		layoutKey,
		customCols,
		customRows,
		pageDurationSec,
		occupiedCardColor,
		availableCardColor,
		statusToneOverrides,
		heroMaxWidthRatioProp
	});
	draftBootstrapRef.current = {
		layoutKey,
		customCols,
		customRows,
		pageDurationSec,
		occupiedCardColor,
		availableCardColor,
		statusToneOverrides,
		heroMaxWidthRatioProp
	};
	const hasOpenedRef = (0, react.useRef)(false);
	(0, react.useEffect)(() => {
		if (!open) {
			hasOpenedRef.current = false;
			return;
		}
		if (hasOpenedRef.current) return;
		hasOpenedRef.current = true;
		const b = draftBootstrapRef.current;
		setDraftLayoutKey(b.layoutKey);
		setDraftCustomCols(b.customCols);
		setDraftCustomRows(b.customRows);
		setDraftPageSec(b.pageDurationSec);
		setDraftOcc(b.occupiedCardColor);
		setDraftAvail(b.availableCardColor);
		setDraftHeroRatio(b.heroMaxWidthRatioProp);
		setDraftTones(require_venueWallUtils.getVenueWallStatusToneMap(b.statusToneOverrides));
		setAdminTab("resource_layout");
	}, [open]);
	const flushApplyAndClose = (0, react.useCallback)(() => {
		onApply(buildApplyPayload(selectedResourceIds, {
			draftLayoutKey,
			draftCustomCols,
			draftCustomRows,
			draftPageSec,
			draftOcc,
			draftAvail,
			draftTones,
			draftHeroRatio
		}, boardingSoonBeforeMin));
		onClose();
	}, [
		selectedResourceIds,
		draftLayoutKey,
		draftCustomCols,
		draftCustomRows,
		draftPageSec,
		draftOcc,
		draftAvail,
		draftTones,
		draftHeroRatio,
		boardingSoonBeforeMin,
		onApply,
		onClose
	]);
	/** Escape：与关闭按钮一致，提交当前草稿并关闭（非「取消放弃」语义） */
	(0, react.useEffect)(() => {
		if (!open) return;
		const onKey = (e) => {
			if (e.key === "Escape") flushApplyAndClose();
		};
		window.addEventListener("keydown", onKey);
		return () => window.removeEventListener("keydown", onKey);
	}, [open, flushApplyAndClose]);
	const shellBg = isDark ? "bg-slate-900 border-slate-800" : "bg-white border-slate-200";
	const divider = isDark ? "border-slate-800" : "border-slate-200";
	const headerText = isDark ? "text-slate-100" : "text-slate-900";
	const mutedText = isDark ? "text-slate-500" : "text-slate-400";
	const adminInputBg = isDark ? "bg-slate-900 border-slate-700 text-slate-100" : "bg-white border-slate-200 text-slate-700";
	const adminSectionBg = isDark ? "bg-slate-950 border-slate-800" : "bg-slate-50 border-slate-200";
	const cur = carouselPager.currentPage + 1;
	const tot = Math.max(1, carouselPager.totalPages);
	if (!open) return null;
	const modal = /* @__PURE__ */ react.default.createElement("div", {
		className: "fixed inset-0 z-50 bg-slate-900/40 backdrop-blur-[2px] p-4 md:p-8",
		onClick: flushApplyAndClose,
		role: "presentation"
	}, /* @__PURE__ */ react.default.createElement("div", {
		className: `mx-auto max-w-[900px] rounded-[30px] border shadow-[0_30px_80px_rgba(15,23,42,0.16)] overflow-hidden ${shellBg}`,
		onClick: (e) => e.stopPropagation(),
		role: "dialog",
		"aria-modal": "true"
	}, /* @__PURE__ */ react.default.createElement("div", { className: `border-b px-5 md:px-6 py-4 flex items-center justify-between gap-4 ${divider}` }, /* @__PURE__ */ react.default.createElement("div", null, /* @__PURE__ */ react.default.createElement("div", { className: `text-[11px] uppercase tracking-[0.24em] ${mutedText}` }, require_index.getText("pisell-resource-wall-control-eyebrow")), /* @__PURE__ */ react.default.createElement("div", { className: `mt-1 text-2xl font-semibold tracking-tight ${headerText}` }, require_index.getText("pisell-resource-wall-control-subtitle")), /* @__PURE__ */ react.default.createElement("div", { className: "mt-3 inline-flex items-center rounded-full border p-1 border-slate-200 bg-white" }, /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		onClick: () => setAdminTab("resource_layout"),
		className: `rounded-full px-3 py-1.5 text-sm transition ${adminTab === "resource_layout" ? "bg-slate-900 text-white" : "text-slate-600 hover:bg-slate-100"}`
	}, require_index.getText("pisell-resource-wall-tab-layout")), /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		onClick: () => setAdminTab("color"),
		className: `rounded-full px-3 py-1.5 text-sm transition ${adminTab === "color" ? "bg-slate-900 text-white" : "text-slate-600 hover:bg-slate-100"}`
	}, require_index.getText("pisell-resource-wall-tab-colors")))), /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		onClick: flushApplyAndClose,
		className: `rounded-full px-4 py-2 text-sm border ${isDark ? "border-slate-700 text-slate-300 hover:bg-slate-800" : "border-slate-200 text-slate-600 hover:bg-slate-50"}`
	}, require_index.getText("pisell-resource-wall-control-close"))), /* @__PURE__ */ react.default.createElement("div", { className: "p-5 md:p-6 space-y-6 max-h-[78vh] overflow-auto" }, adminTab === "resource_layout" ? /* @__PURE__ */ react.default.createElement(require_ResourceWallControlPanelLayoutTab.ResourceWallControlPanelLayoutTab, {
		adminSectionBg,
		adminInputBg,
		headerText,
		mutedText,
		layoutPresets,
		draftLayoutKey,
		setDraftLayoutKey,
		draftCustomCols,
		setDraftCustomCols,
		draftCustomRows,
		setDraftCustomRows,
		draftPageSec,
		setDraftPageSec,
		draftHeroRatio,
		setDraftHeroRatio,
		carouselCur: cur,
		carouselTot: tot,
		isDark
	}) : /* @__PURE__ */ react.default.createElement(require_ResourceWallControlPanelColorTab.ResourceWallControlPanelColorTab, {
		adminSectionBg,
		adminInputBg,
		headerText,
		mutedText,
		divider,
		draftOcc,
		setDraftOcc,
		draftAvail,
		setDraftAvail,
		draftTones,
		setDraftTones
	}))));
	return typeof document !== "undefined" ? (0, react_dom.createPortal)(modal, document.body) : null;
};
//#endregion
exports.ResourceWallControlPanel = ResourceWallControlPanel;
