const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_HistoryIcons = require("./HistoryIcons.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_floorMapCanvasDisplay = require("../utils/floorMapCanvasDisplay.js");
const require_CanvasTabs = require("./CanvasTabs.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./FloorMapEditTopBar.less");
//#region src/components/pisellFloorMapLayout/components/FloorMapEditTopBar.tsx
/**
* FloorMapEditTopBar - 编辑态顶栏
* - `mode="compact"`：画布 Tab + 可选 `canvasTabsBarTrailing`（全屏入口在左下 Dock）
* - `mode="full"`：画布 Tab + `canvasTabsBarTrailing` + 保存（仅非浏览器全屏时）+ 扩展操作
*/
const PREFIX = "pisell-floor-map-layout";
const I18N_TB = "pisell-floor-map-layout.edit-top-bar";
/** 检测 macOS / iPad，决定快捷键提示用 ⌘ 还是 Ctrl */
function detectIsMacLike() {
	if (typeof navigator === "undefined") return false;
	const p = navigator.platform || "";
	const ua = navigator.userAgent || "";
	return /Mac|iPhone|iPad|iPod/i.test(p) || /Macintosh/i.test(ua);
}
function FloorMapEditTopBar(props) {
	const { mode = "full", config, canvases, currentCanvasId, onTabChange, canEditTabs = false, onAddCanvas, onRemoveCanvas, onRenameCanvas, onSave, saving, saveError, layoutDirty, isFullscreen = false, extraActions, canvasTabsBarTrailing, hideCanvasTabs = false, onUndo, onRedo, canUndo = false, canRedo = false, onExitEdit, onCancelEdit, className, phoneLayout = false } = props;
	const engineLocale = require_floorMapCanvasDisplay.getFloorMapDisplayLocale(require_useEngineContext.default());
	const topBarLabels = (0, react.useMemo)(() => {
		require_floorMapCanvasDisplay.initFloorMapLayoutLocales(engineLocale);
		return {
			aria: _pisell_utils.locales.getText(`${I18N_TB}.aria`),
			unsaved: _pisell_utils.locales.getText(`${I18N_TB}.unsaved`),
			save: _pisell_utils.locales.getText(`${I18N_TB}.save`),
			saving: _pisell_utils.locales.getText(`${I18N_TB}.saving`),
			undo: _pisell_utils.locales.getText(`${I18N_TB}.undo`),
			redo: _pisell_utils.locales.getText(`${I18N_TB}.redo`),
			cancelEdit: _pisell_utils.locales.getText(`${I18N_TB}.cancel-edit`),
			finishEdit: _pisell_utils.locales.getText(`${I18N_TB}.finish-edit`),
			cancelConfirmTitle: _pisell_utils.locales.getText(`${I18N_TB}.cancel-confirm-title`),
			cancelConfirmContent: _pisell_utils.locales.getText(`${I18N_TB}.cancel-confirm-content`),
			cancelConfirmOk: _pisell_utils.locales.getText(`${I18N_TB}.cancel-confirm-ok`),
			cancelConfirmCancel: _pisell_utils.locales.getText(`${I18N_TB}.cancel-confirm-cancel`)
		};
	}, [engineLocale]);
	const showHistory = Boolean(onUndo || onRedo);
	/** 历史按钮组（撤销/重做）：full、compact 共用同一段渲染 */
	const historyGroup = (0, react.useMemo)(() => {
		if (!showHistory) return null;
		const cmd = detectIsMacLike() ? "⌘" : "Ctrl";
		const undoTitle = `${topBarLabels.undo} (${cmd}+Z)`;
		const redoTitle = `${topBarLabels.redo} (${cmd}+Shift+Z)`;
		return /* @__PURE__ */ react.default.createElement("div", {
			className: `${PREFIX}-edit-top-bar-history`,
			role: "group",
			"aria-label": `${topBarLabels.undo} / ${topBarLabels.redo}`
		}, /* @__PURE__ */ react.default.createElement(antd.Button, {
			type: "text",
			size: "small",
			className: `${PREFIX}-edit-top-bar-history-btn`,
			icon: /* @__PURE__ */ react.default.createElement(require_HistoryIcons.UndoHistoryIcon, null),
			disabled: !canUndo,
			onClick: onUndo,
			"aria-label": undoTitle
		}, topBarLabels.undo), /* @__PURE__ */ react.default.createElement(antd.Button, {
			type: "text",
			size: "small",
			className: `${PREFIX}-edit-top-bar-history-btn`,
			icon: /* @__PURE__ */ react.default.createElement(require_HistoryIcons.RedoHistoryIcon, null),
			disabled: !canRedo,
			onClick: onRedo,
			"aria-label": redoTitle
		}, topBarLabels.redo));
	}, [
		showHistory,
		topBarLabels.undo,
		topBarLabels.redo,
		canUndo,
		canRedo,
		onUndo,
		onRedo
	]);
	/** 浏览器全屏时顶栏不展示保存，改由侧栏承担（见 PisellFloorMapLayout 对 Toolbar onSave 的互斥逻辑） */
	const showSave = Boolean(onSave && config.canvasUi.canEdit) && !isFullscreen;
	/**
	* 业务层提供 onExitEdit 时改用「取消编辑 + 完成」组合，
	* 此时不再展示独立的「保存」按钮（完成 = 保存 + 退出，避免冗余）。
	*/
	const showExitGroup = Boolean(onExitEdit && config.canvasUi.canEdit);
	/**
	* 「未保存」徽章独立成一段：放到 undo / redo 左边，与「编辑历史」视觉上同属一个状态区。
	* 触发条件保持原样：必须 layoutDirty 且当前确实有保存通道（showSave 或 showExitGroup），
	* 否则纯阅读态出现「未保存」会让用户困惑。
	*/
	const dirtyBadge = layoutDirty && (showExitGroup || showSave) ? /* @__PURE__ */ react.default.createElement("span", {
		className: `${PREFIX}-edit-top-bar-dirty-badge`,
		role: "status",
		"aria-live": "polite"
	}, topBarLabels.unsaved) : null;
	/**
	* 取消编辑：layoutDirty 时弹二次确认，避免误触丢失草稿；
	* 干净状态直接走原本的退出路径，不打扰用户。
	* onOk 里复用「优先 onCancelEdit，回退 onExitEdit」的语义，与保存按钮链路对称。
	*
	* 走 {@link Modal.useModal} 拿到的实例 + 同级渲染的 `modalContextHolder`，确保：
	* - OK 按钮颜色继承当前 ConfigProvider 主题主色（静态 `Modal.confirm` 不会继承）；
	* - 不依赖外层是否包了 PisellConfigProvider/`App` —— 这个组件可能被低代码引擎
	*   单独挂在画布层，并不一定有上述 Provider，原先走 `PisellModal.confirm`
	*   会因为 `setModal` 没被调用拿到 `undefined.confirm` 直接抛错。
	*/
	const [modalApi, modalContextHolder] = antd.Modal.useModal();
	const performCancel = (0, react.useCallback)(() => {
		if (onCancelEdit) onCancelEdit();
		else onExitEdit === null || onExitEdit === void 0 || onExitEdit();
	}, [onCancelEdit, onExitEdit]);
	const handleCancelClick = (0, react.useCallback)(() => {
		if (!layoutDirty) {
			performCancel();
			return;
		}
		modalApi.confirm({
			title: topBarLabels.cancelConfirmTitle,
			content: topBarLabels.cancelConfirmContent,
			okText: topBarLabels.cancelConfirmOk,
			cancelText: topBarLabels.cancelConfirmCancel,
			onOk: () => performCancel(),
			autoFocusButton: "cancel"
		});
	}, [
		layoutDirty,
		performCancel,
		modalApi,
		topBarLabels.cancelConfirmTitle,
		topBarLabels.cancelConfirmContent,
		topBarLabels.cancelConfirmOk,
		topBarLabels.cancelConfirmCancel
	]);
	const hasTrailing = canvasTabsBarTrailing != null && canvasTabsBarTrailing !== false;
	const hasFullActions = showSave || showExitGroup || Boolean(saveError) || extraActions != null && extraActions !== false || hasTrailing || showHistory;
	const tabsHiddenClass = hideCanvasTabs ? `${PREFIX}-edit-top-bar--tabs-hidden` : void 0;
	if (mode === "compact") {
		const showCompactActions = hasTrailing || showHistory;
		const tabsInner = /* @__PURE__ */ react.default.createElement(require_CanvasTabs.default, {
			canvases,
			currentCanvasId,
			onTabChange,
			canEdit: canEditTabs,
			onAddCanvas,
			onRemoveCanvas,
			onRenameCanvas,
			className: `${PREFIX}-edit-top-bar-canvas-tabs`
		});
		const actionsInner = /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, historyGroup, canvasTabsBarTrailing);
		/**
		* 手机：第一行与阅读态相同（`read-canvas-tabs-bar` + 仅 CanvasTabs），
		* 撤销/图例等放第二行，且左对齐（非居中）。
		* 桌面：单行与阅读态相同结构（左 Tabs 右操作）。
		*/
		if (phoneLayout) return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, !hideCanvasTabs ? /* @__PURE__ */ react.default.createElement("div", {
			className: (0, classnames.default)(`${PREFIX}-edit-top-bar`, `${PREFIX}-read-canvas-tabs-bar`, `${PREFIX}-edit-top-bar--compact`, `${PREFIX}-edit-top-bar--phone-edit-canvas-row`, className),
			role: "toolbar",
			"aria-label": topBarLabels.aria
		}, /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-tabs` }, tabsInner)) : null, showCompactActions ? /* @__PURE__ */ react.default.createElement("div", {
			className: (0, classnames.default)(`${PREFIX}-edit-top-bar`, `${PREFIX}-edit-top-bar--compact`, `${PREFIX}-edit-top-bar--phone-actions-row`),
			role: "toolbar"
		}, /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-actions` }, actionsInner)) : null, modalContextHolder);
		return /* @__PURE__ */ react.default.createElement("div", {
			className: (0, classnames.default)(`${PREFIX}-edit-top-bar`, `${PREFIX}-read-canvas-tabs-bar`, `${PREFIX}-edit-top-bar--compact`, tabsHiddenClass, className),
			role: "toolbar",
			"aria-label": topBarLabels.aria
		}, !hideCanvasTabs ? /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-tabs` }, tabsInner) : null, showCompactActions ? /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-actions` }, actionsInner) : null, modalContextHolder);
	}
	if (hideCanvasTabs && !hasFullActions) return null;
	const fullTabsInner = /* @__PURE__ */ react.default.createElement(require_CanvasTabs.default, {
		canvases,
		currentCanvasId,
		onTabChange,
		canEdit: canEditTabs,
		onAddCanvas,
		onRemoveCanvas,
		onRenameCanvas,
		className: `${PREFIX}-edit-top-bar-canvas-tabs`
	});
	const fullActionsInner = /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, saveError ? /* @__PURE__ */ react.default.createElement("span", {
		className: `${PREFIX}-edit-top-bar-error`,
		title: typeof saveError === "string" ? saveError : saveError === null || saveError === void 0 ? void 0 : saveError.message
	}, typeof saveError === "string" ? saveError : saveError === null || saveError === void 0 ? void 0 : saveError.message) : null, dirtyBadge, historyGroup, canvasTabsBarTrailing, extraActions, showExitGroup ? /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-save-group` }, /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: `${PREFIX}-edit-top-bar-btn`,
		disabled: saving,
		onClick: handleCancelClick
	}, topBarLabels.cancelEdit), /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: (0, classnames.default)(`${PREFIX}-edit-top-bar-btn`, "primary"),
		disabled: saving,
		onClick: () => {
			/**
			* 完成 = 保存 + 退出；保存按 onSave 同步触发，
			* 即便 onSave 是异步上报也立即调 onExitEdit，
			* 业务层若需「保存成功后再退出」可在自身 onExitEdit 内做控制。
			*/
			onSave === null || onSave === void 0 || onSave();
			onExitEdit === null || onExitEdit === void 0 || onExitEdit();
		}
	}, saving ? topBarLabels.saving : topBarLabels.finishEdit)) : showSave ? /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-save-group` }, /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: (0, classnames.default)(`${PREFIX}-edit-top-bar-btn`, "primary"),
		disabled: saving,
		onClick: onSave
	}, saving ? topBarLabels.saving : topBarLabels.save)) : null);
	if (phoneLayout) return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, !hideCanvasTabs ? /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)(`${PREFIX}-edit-top-bar`, `${PREFIX}-read-canvas-tabs-bar`, `${PREFIX}-edit-top-bar--full-chrome`, `${PREFIX}-edit-top-bar--phone-edit-canvas-row`, className),
		role: "toolbar",
		"aria-label": topBarLabels.aria
	}, /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-tabs` }, fullTabsInner)) : null, hasFullActions ? /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)(`${PREFIX}-edit-top-bar`, `${PREFIX}-edit-top-bar--full-chrome`, `${PREFIX}-edit-top-bar--phone-actions-row`),
		role: "toolbar"
	}, /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-actions` }, fullActionsInner)) : null, modalContextHolder);
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)(`${PREFIX}-edit-top-bar`, `${PREFIX}-read-canvas-tabs-bar`, `${PREFIX}-edit-top-bar--full-chrome`, tabsHiddenClass, className),
		role: "toolbar",
		"aria-label": topBarLabels.aria
	}, !hideCanvasTabs ? /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-tabs` }, fullTabsInner) : null, hasFullActions ? /* @__PURE__ */ react.default.createElement("div", { className: `${PREFIX}-edit-top-bar-actions` }, fullActionsInner) : null, modalContextHolder);
}
//#endregion
exports.default = FloorMapEditTopBar;
