//#region src/components/pisellFloorMapLayout/shopFloorPlan/shopFloorPlanMapping.ts
function parseResourceId(recordId) {
	if (recordId == null || recordId === "") return null;
	const n = Number(recordId);
	return Number.isFinite(n) && n > 0 ? Math.trunc(n) : null;
}
function isCircleShape(el) {
	return el.shape === "circle";
}
/**
* 将场景图元转为后端 layout 项（多画布时通常摊平到同一 layout）。
*/
function sceneElementsToShopLayout(sceneElements) {
	return sceneElements.map((el) => {
		var _el$width, _el$height, _el$extensions, _el$extensions2, _el$name, _el$fillColor, _el$strokeColor, _el$dataBinding;
		const w = (_el$width = el.width) !== null && _el$width !== void 0 ? _el$width : 100;
		const h = (_el$height = el.height) !== null && _el$height !== void 0 ? _el$height : 100;
		const circle = isCircleShape(el);
		const rotation = typeof ((_el$extensions = el.extensions) === null || _el$extensions === void 0 ? void 0 : _el$extensions.rotation) === "number" ? el.extensions.rotation : Number((_el$extensions2 = el.extensions) === null || _el$extensions2 === void 0 ? void 0 : _el$extensions2.rotation) || 0;
		const base = {
			id: el.instanceId,
			type: circle ? "circle" : "rect",
			label: ((_el$name = el.name) === null || _el$name === void 0 ? void 0 : _el$name.trim()) || "",
			x: el.x,
			y: el.y,
			rotation,
			color: (_el$fillColor = el.fillColor) !== null && _el$fillColor !== void 0 ? _el$fillColor : "#1a2332",
			border_color: (_el$strokeColor = el.strokeColor) !== null && _el$strokeColor !== void 0 ? _el$strokeColor : "#ffffff",
			resource_id: parseResourceId((_el$dataBinding = el.dataBinding) === null || _el$dataBinding === void 0 ? void 0 : _el$dataBinding.recordId)
		};
		if (circle) base.radius = Math.max(1, Math.round(Math.min(w, h) / 2));
		else {
			base.width = w;
			base.height = h;
		}
		return base;
	});
}
/**
* 将扁平 layout 数组转为 `sceneElements`；图元种类由调用方传入（预约场景可传 boundTable / boundRoundTable 等）。
*/
function shopLayoutToSceneElements(layout, options) {
	const { canvasId, dataSourceKey, rectElementKind, circleElementKind } = options;
	return layout.map((item) => {
		var _item$radius, _item$width, _item$height, _item$label;
		const circle = item.type === "circle";
		const r = Math.max(1, Math.round((_item$radius = item.radius) !== null && _item$radius !== void 0 ? _item$radius : 40));
		const w = circle ? r * 2 : (_item$width = item.width) !== null && _item$width !== void 0 ? _item$width : 100;
		const h = circle ? r * 2 : (_item$height = item.height) !== null && _item$height !== void 0 ? _item$height : 100;
		const rotation = typeof item.rotation === "number" ? item.rotation : 0;
		const el = {
			instanceId: String(item.id),
			canvasId,
			x: item.x,
			y: item.y,
			width: w,
			height: h,
			name: ((_item$label = item.label) === null || _item$label === void 0 ? void 0 : _item$label.trim()) || void 0,
			zIndex: 0,
			elementKind: circle ? circleElementKind : rectElementKind,
			shape: circle ? "circle" : void 0,
			fillColor: item.color,
			strokeColor: item.border_color,
			extensions: rotation !== 0 ? { rotation } : void 0
		};
		if (item.resource_id != null && Number.isFinite(Number(item.resource_id))) el.dataBinding = {
			dataSourceKey,
			recordId: String(Math.trunc(Number(item.resource_id)))
		};
		return el;
	});
}
//#endregion
exports.sceneElementsToShopLayout = sceneElementsToShopLayout;
exports.shopLayoutToSceneElements = shopLayoutToSceneElements;
