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_PisellProResourcePickerModal = require("./PisellProResourcePickerModal.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
//#region src/plus/pisellProResourcePickerModal/PisellFloorMapPaletteViewAllModal.tsx
/**
* 平面图侧栏 palette「查看全部」弹窗（私有物料）
* - 复用 `PisellProResourcePickerModal` 的 UI 与 Selector 数量选择能力
* - Selector 使用 valueType="object" 以启用数量选择器（max>1）
*/
/**
* 模块加载时即用安全的默认 locale（zh-CN）初始化 floor map 文案字典，
* 让首次 render 的 `useMemo`（如 tabbarDataSource 读「图片 / 图标」）就能拿到值，
* 避免 React render 期 side-effect。引擎语言变化由组件内 `useEffect` 再次 init 切换。
*/
_pisell_utils.locales.init(_pisell_materials.floorMapLayoutLocales, "zh-CN");
/** 与 materials 内 FloorMapElementKindConfig.value 对齐的「图片」kind 值 */
const IMAGE_KIND_VALUE = "floorMapImage";
/** View All 顶部 Tabbar 的两个分类标识，写入 record.__category 与 Tabbar dataSource.id */
const CATEGORY_IMAGE = "image";
const CATEGORY_ICON = "icon";
const I18N_VA = "pisell-floor-map-layout.toolbar";
function expandEntriesByQuantity(entries, quantities) {
	const out = [];
	for (const e of entries) {
		var _quantities$e$key;
		const n = Math.max(0, Math.floor((_quantities$e$key = quantities[e.key]) !== null && _quantities$e$key !== void 0 ? _quantities$e$key : 0));
		for (let i = 0; i < n; i += 1) out.push(require_objectSpread2._objectSpread2({ elementKind: e.kind.value }, e.builtinShapeKey ? { builtinShapeKey: e.builtinShapeKey } : {}));
	}
	return out;
}
function PisellFloorMapPaletteViewAllModal(props) {
	var _ref, _engine$props;
	const { open, categoryLabel, entries, onClose, onConfirm, renderEntryPreview, platform } = props;
	const { engine } = require_useEngineContext.default();
	const engineLocale = (_ref = engine === null || engine === void 0 || (_engine$props = engine.props) === null || _engine$props === void 0 ? void 0 : _engine$props.locale) !== null && _ref !== void 0 ? _ref : "zh-CN";
	/**
	* 切换引擎语言或弹窗打开时同步语言字典；首次 render 用的是模块顶层 init 的
	* 默认 locale，因此这里只负责追随后续语言变化，不在 render 期产生副作用。
	*/
	(0, react.useEffect)(() => {
		if (open) _pisell_utils.locales.init(_pisell_materials.floorMapLayoutLocales, engineLocale);
	}, [open, engineLocale]);
	/**
	* 给每个 record 打上一级分类标签：
	* - image：图元 kind 为 `floorMapImage`（如「Image (background)」）
	* - icon：其它装饰（多为 builtin shape 如 Toilet / Divider / Sofa…）
	*/
	const records = (0, react.useMemo)(() => entries.map((e) => ({
		id: e.key,
		title: e.label,
		__category: e.kind.value === IMAGE_KIND_VALUE ? CATEGORY_IMAGE : CATEGORY_ICON,
		__entry: e
	})), [entries]);
	/**
	* View All 顶部 Tabbar 固定两类：图片 / 图标。只有在 entries 中确有该类时才展示对应按钮。
	* Tabbar 自带内置 search + all，业务方无需自己注入。
	*/
	const tabbarDataSource = (0, react.useMemo)(() => {
		const hasImage = entries.some((e) => e.kind.value === IMAGE_KIND_VALUE);
		const hasIcon = entries.some((e) => e.kind.value !== IMAGE_KIND_VALUE);
		const out = [];
		if (hasImage) out.push({
			id: CATEGORY_IMAGE,
			name: _pisell_utils.locales.getText(`${I18N_VA}.view-all-modal-category-image`)
		});
		if (hasIcon) out.push({
			id: CATEGORY_ICON,
			name: _pisell_utils.locales.getText(`${I18N_VA}.view-all-modal-category-icon`)
		});
		return out;
	}, [entries, engineLocale]);
	const title = categoryLabel;
	return /* @__PURE__ */ react.default.createElement(require_PisellProResourcePickerModal.default, {
		open,
		title,
		onClose,
		platform,
		onConfirm: (selected) => {
			const quantities = {};
			for (const v of selected) {
				const key = v.value.trim();
				if (!key) continue;
				quantities[key] = Math.max(0, Math.floor(v.quantity));
			}
			onConfirm(expandEntriesByQuantity(entries, quantities));
		},
		records,
		mode: "multiple",
		value: [],
		selectorValueType: "object",
		selectorShowStepper: true,
		getOptionRuleConfig: () => ({ max: 99 }),
		getSearchText: (r) => {
			var _title;
			return String((_title = r.title) !== null && _title !== void 0 ? _title : "").toLowerCase();
		},
		categoryConfig: { levels: [{ field: "__category" }] },
		renderFilterBar: (ctx) => /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellTabbarTemplate1, {
			className: "pisell-pro-resource-picker-tabbar",
			style: {
				position: "sticky",
				top: 0,
				zIndex: 10,
				padding: 6,
				background: "rgb(250, 250, 250)"
			},
			dataSource: tabbarDataSource,
			value: Array.isArray(ctx.tabbarValue) && ctx.tabbarValue.length ? ctx.tabbarValue : ["0"],
			onChange: ctx.onTabbarValueChange,
			onSearch: ctx.onKeywordChange,
			maxLevel: 1
		}),
		getCardMeta: (r) => {
			var _title2;
			const e = r.__entry;
			return {
				title: String((_title2 = r.title) !== null && _title2 !== void 0 ? _title2 : ""),
				subtitles: [],
				image: e ? renderEntryPreview(e) : void 0
			};
		}
	});
}
//#endregion
exports.default = PisellFloorMapPaletteViewAllModal;
