import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { FLOOR_MAP_SELECTION_ZONE_ELEMENT_KIND } from "../types.js";
import { getFloorMapBuiltinShapeDef } from "./floorMapBuiltinShapes.js";
import { getElementKindConfig } from "./floorMapElementKindUtils.js";
//#region src/components/pisellFloorMapLayout/utils/floorMapConfigMerge.ts
const DEFAULT_SIZE = 48;
const IMAGE_KIND_DEFAULT_SIZE = {
	width: 400,
	height: 240
};
const BUILTIN_SHAPE_FALLBACK_SIZE = {
	width: 96,
	height: 96
};
const SELECTION_ZONE_KIND_DEFAULT_SIZE = {
	width: 320,
	height: 240
};
/** 选区默认置于较低层，保证框内图元仍可被单独点选/拖动 */
const SELECTION_ZONE_DEFAULT_Z_INDEX = -50;
function getDefaultSize(elementKind, elementKinds, element) {
	if (!elementKind) return {
		width: DEFAULT_SIZE,
		height: DEFAULT_SIZE
	};
	if (elementKind === "floorMapImage") return _objectSpread2({}, IMAGE_KIND_DEFAULT_SIZE);
	if (elementKind === "floorMapBuiltinShape") {
		const def = getFloorMapBuiltinShapeDef(element === null || element === void 0 ? void 0 : element.builtinShapeKey);
		return def ? _objectSpread2({}, def.defaultSize) : _objectSpread2({}, BUILTIN_SHAPE_FALLBACK_SIZE);
	}
	if (elementKind === "floorMapSelectionZone") return _objectSpread2({}, SELECTION_ZONE_KIND_DEFAULT_SIZE);
	if (!elementKinds.find((k) => k.value === elementKind)) return {
		width: DEFAULT_SIZE,
		height: DEFAULT_SIZE
	};
	return {
		width: DEFAULT_SIZE,
		height: DEFAULT_SIZE
	};
}
/**
* 合并指定画布下的 sceneElements 为一份 items（用于 ItemLayer / EditableItemLayer）
*/
function mergeCanvasItems(canvasId, config, defaultCellSize = DEFAULT_SIZE) {
	const { sceneElements = [], elementKinds = [] } = config;
	const items = [];
	const onCanvas = sceneElements.filter((el) => el.canvasId === canvasId);
	for (const el of onCanvas) {
		var _ref, _el$width, _ref2, _el$height, _el$shape;
		const size = getDefaultSize(el.elementKind, elementKinds, el);
		const w = (_ref = (_el$width = el.width) !== null && _el$width !== void 0 ? _el$width : size.width) !== null && _ref !== void 0 ? _ref : defaultCellSize;
		const h = (_ref2 = (_el$height = el.height) !== null && _el$height !== void 0 ? _el$height : size.height) !== null && _ref2 !== void 0 ? _ref2 : defaultCellSize;
		const kindConfig = getElementKindConfig({ elementKinds }, el.elementKind);
		const isSelectionZone = el.elementKind === FLOOR_MAP_SELECTION_ZONE_ELEMENT_KIND;
		items.push({
			id: el.instanceId,
			instanceId: el.instanceId,
			x: el.x,
			y: el.y,
			width: w,
			height: h,
			name: el.name,
			zIndex: isSelectionZone && el.zIndex == null ? SELECTION_ZONE_DEFAULT_Z_INDEX : el.zIndex,
			shape: (_el$shape = el.shape) !== null && _el$shape !== void 0 ? _el$shape : kindConfig === null || kindConfig === void 0 ? void 0 : kindConfig.defaultShape,
			elementKind: el.elementKind,
			_sceneElement: el
		});
	}
	return items;
}
//#endregion
export { mergeCanvasItems };
