const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_floorMapCanvasDisplay = require("../utils/floorMapCanvasDisplay.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
let _ant_design_icons = require("@ant-design/icons");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./CanvasTabs.less");
//#region src/components/pisellFloorMapLayout/components/CanvasTabs.tsx
/**
* CanvasTabs - 多画布 Tab，支持切换、动态添加、删除与重命名（编辑态）
* - 展示名：可选 `nameKey` + `locales.getText`，否则用 `name`（字符串或多语言对象）
* - 角标：可选 `tabBadge` 或由布局层注入合并后的数量
*/
const PREFIX = "pisell-floor-map-layout";
const I18N_EMPTY = "pisell-floor-map-layout.canvas-tabs.empty";
const I18N_ADD = "pisell-floor-map-layout.canvas-tabs.add";
const I18N_ADD_TITLE = "pisell-floor-map-layout.canvas-tabs.add-title";
const I18N_HINT = "pisell-floor-map-layout.canvas-tabs.hint";
const I18N_RENAME_ARIA = "pisell-floor-map-layout.canvas-tabs.rename-aria";
const I18N_REMOVE_ARIA = "pisell-floor-map-layout.canvas-tabs.remove-aria";
const I18N_RENAME_TITLE = "pisell-floor-map-layout.canvas-tabs.rename-title";
const I18N_RENAME_BTN_ARIA = "pisell-floor-map-layout.canvas-tabs.rename-button-aria";
const I18N_RENAME_BTN_TITLE = "pisell-floor-map-layout.canvas-tabs.rename-button-title";
const I18N_RENAME_MODAL_TITLE = "pisell-floor-map-layout.canvas-tabs.rename-modal-title";
const I18N_RENAME_MODAL_OK = "pisell-floor-map-layout.canvas-tabs.rename-modal-ok";
const I18N_RENAME_MODAL_CANCEL = "pisell-floor-map-layout.canvas-tabs.rename-modal-cancel";
/** 与 Translation 弹层语言列表兜底一致（无 store 时） */
const FALLBACK_LANG_LIST = [
	{
		code: "zh-CN",
		text: "简体中文"
	},
	{
		code: "en",
		text: "English"
	},
	{
		code: "zh-HK",
		text: "繁體中文"
	},
	{
		code: "ja",
		text: "日本語"
	},
	{
		code: "pt",
		text: "Português"
	}
];
function CanvasTabs(props) {
	var _engineCtx$appHelper$, _engineCtx$appHelper;
	const { canvases, currentCanvasId, onTabChange, canEdit = false, onAddCanvas, onRemoveCanvas, onRenameCanvas, className } = props;
	const engineCtx = require_useEngineContext.default();
	const engineLocale = require_floorMapCanvasDisplay.getFloorMapDisplayLocale(engineCtx);
	require_floorMapCanvasDisplay.initFloorMapLayoutLocales(engineLocale);
	const languageList = (_engineCtx$appHelper$ = engineCtx === null || engineCtx === void 0 || (_engineCtx$appHelper = engineCtx.appHelper) === null || _engineCtx$appHelper === void 0 || (_engineCtx$appHelper = _engineCtx$appHelper.store) === null || _engineCtx$appHelper === void 0 || (_engineCtx$appHelper = _engineCtx$appHelper.getState()) === null || _engineCtx$appHelper === void 0 || (_engineCtx$appHelper = _engineCtx$appHelper.global) === null || _engineCtx$appHelper === void 0 || (_engineCtx$appHelper = _engineCtx$appHelper.shopLanguages) === null || _engineCtx$appHelper === void 0 ? void 0 : _engineCtx$appHelper.list) !== null && _engineCtx$appHelper$ !== void 0 ? _engineCtx$appHelper$ : FALLBACK_LANG_LIST;
	const enabledCanvases = canvases.filter((c) => c.enabled).sort((a, b) => a.order - b.order);
	const labelMap = (0, react.useMemo)(() => {
		const m = /* @__PURE__ */ new Map();
		enabledCanvases.forEach((c) => {
			m.set(c.id, require_floorMapCanvasDisplay.getFloorMapCanvasDisplayName(c, engineLocale));
		});
		return m;
	}, [enabledCanvases, engineLocale]);
	const canRemove = canEdit && enabledCanvases.length > 1 && Boolean(onRemoveCanvas);
	const canRename = canEdit && Boolean(onRenameCanvas);
	const [renameModal, setRenameModal] = (0, react.useState)(null);
	const handleRemove = (e, canvasId) => {
		e.stopPropagation();
		onRemoveCanvas === null || onRemoveCanvas === void 0 || onRemoveCanvas(canvasId);
	};
	const openRenameForCanvas = (0, react.useCallback)((c) => {
		var _labelMap$get;
		if (!canRename) return;
		setRenameModal({
			canvas: c,
			value: require_floorMapCanvasDisplay.seedFloorMapCanvasNameI18n(c, (_labelMap$get = labelMap.get(c.id)) !== null && _labelMap$get !== void 0 ? _labelMap$get : require_floorMapCanvasDisplay.getFloorMapCanvasDisplayName(c, engineLocale))
		});
	}, [
		canRename,
		labelMap,
		engineLocale
	]);
	const openRenameModal = (e, c) => {
		e.stopPropagation();
		openRenameForCanvas(c);
	};
	const commitRenameModal = () => {
		if (!renameModal) return;
		const v = renameModal.value;
		if (Object.values(v).some((s) => typeof s === "string" && s.trim())) onRenameCanvas === null || onRenameCanvas === void 0 || onRenameCanvas(renameModal.canvas.id, v);
		setRenameModal(null);
	};
	if (enabledCanvases.length === 0 && !canEdit) return /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)(`${PREFIX}-canvas-tabs`, `${PREFIX}-canvas-tabs-empty`, className) }, _pisell_utils.locales.getText(I18N_EMPTY));
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)(`${PREFIX}-canvas-tabs`, className),
		role: "tablist"
	}, enabledCanvases.map((c) => {
		var _labelMap$get2;
		const displayLabel = (_labelMap$get2 = labelMap.get(c.id)) !== null && _labelMap$get2 !== void 0 ? _labelMap$get2 : require_floorMapCanvasDisplay.getFloorMapCanvasDisplayName(c, engineLocale);
		const badge = c.tabBadge !== void 0 && c.tabBadge !== null ? c.tabBadge : null;
		return /* @__PURE__ */ react.default.createElement("div", {
			key: c.id,
			role: "tab",
			"aria-selected": currentCanvasId === c.id,
			className: (0, classnames.default)(`${PREFIX}-canvas-tabs-tab`, { [`${PREFIX}-canvas-tabs-tab-active`]: currentCanvasId === c.id }),
			onClick: () => onTabChange(c.id)
		}, /* @__PURE__ */ react.default.createElement("span", {
			className: `${PREFIX}-canvas-tabs-tab-label`,
			title: canRename ? _pisell_utils.locales.getText(I18N_RENAME_TITLE) : void 0,
			onDoubleClick: canRename ? (e) => openRenameModal(e, c) : void 0
		}, displayLabel, badge != null ? /* @__PURE__ */ react.default.createElement("span", { className: `${PREFIX}-canvas-tabs-tab-badge` }, " ", "(", badge, ")") : null), canRename ? /* @__PURE__ */ react.default.createElement("button", {
			type: "button",
			className: `${PREFIX}-canvas-tabs-tab-rename`,
			"aria-label": `${_pisell_utils.locales.getText(I18N_RENAME_BTN_ARIA)} · ${displayLabel}`,
			title: _pisell_utils.locales.getText(I18N_RENAME_BTN_TITLE),
			onClick: (e) => {
				e.stopPropagation();
				openRenameForCanvas(c);
			}
		}, /* @__PURE__ */ react.default.createElement(_ant_design_icons.EditOutlined, null)) : null, canRemove && /* @__PURE__ */ react.default.createElement("button", {
			type: "button",
			"aria-label": `${_pisell_utils.locales.getText(I18N_REMOVE_ARIA)} · ${displayLabel}`,
			className: `${PREFIX}-canvas-tabs-tab-remove`,
			onClick: (e) => handleRemove(e, c.id)
		}));
	}), canEdit && onAddCanvas && /* @__PURE__ */ react.default.createElement("button", {
		type: "button",
		className: `${PREFIX}-canvas-tabs-add`,
		onClick: onAddCanvas,
		title: _pisell_utils.locales.getText(I18N_ADD_TITLE)
	}, _pisell_utils.locales.getText(I18N_ADD)), enabledCanvases.length === 0 && canEdit && /* @__PURE__ */ react.default.createElement("span", { className: `${PREFIX}-canvas-tabs-empty-hint` }, _pisell_utils.locales.getText(I18N_HINT))), /* @__PURE__ */ react.default.createElement(antd.Modal, {
		title: _pisell_utils.locales.getText(I18N_RENAME_MODAL_TITLE),
		open: renameModal != null,
		onOk: commitRenameModal,
		onCancel: () => setRenameModal(null),
		okText: _pisell_utils.locales.getText(I18N_RENAME_MODAL_OK),
		cancelText: _pisell_utils.locales.getText(I18N_RENAME_MODAL_CANCEL),
		destroyOnClose: true,
		wrapClassName: `${PREFIX}-canvas-tabs-rename-modal-wrap`,
		styles: {
			header: {
				margin: 0,
				padding: "10px 14px 6px"
			},
			body: { padding: "8px 14px 10px" },
			footer: {
				margin: 0,
				padding: "8px 14px 10px"
			}
		}
	}, renameModal ? /* @__PURE__ */ react.default.createElement(antd.Form, {
		layout: "vertical",
		className: `${PREFIX}-canvas-tabs-rename-form`,
		requiredMark: false
	}, languageList.map((d) => {
		var _renameModal$value;
		return /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
			label: d.text,
			key: d.code
		}, /* @__PURE__ */ react.default.createElement(antd.Input, {
			value: (_renameModal$value = renameModal.value[d.code]) !== null && _renameModal$value !== void 0 ? _renameModal$value : "",
			onChange: (e) => setRenameModal((prev) => prev ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { value: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev.value), {}, { [d.code]: e.target.value }) }) : null),
			"aria-label": `${_pisell_utils.locales.getText(I18N_RENAME_ARIA)} · ${d.text}`
		}));
	})) : null));
}
//#endregion
exports.default = CanvasTabs;
