import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import { getReservationDataSourceFormColumns } from "./reservationMock.js";
import { saveViewConfigToShopFloorPlan, toastFloorPlanSaveError } from "./floorPlanApi.js";
import { ReservationFloorMapCanvasTabsTrailing } from "../components/PisellReservationSubcomponents.js";
import React, { useMemo } from "react";
import { locales } from "@pisell/utils";
import { wrapFloorMapOnSaveWithRemotePersist } from "@pisell/materials";
//#region src/plus/pisellReservation/floorMap/useReservationFloorMapMerged.tsx
/**
* 合并 RecordBoard.floorMap 与预约默认：持久化 onSave、画布 Tab 尾区、缩放/平移等
*/
const _excluded = ["getMergedItemWrapperStyle"];
const I18N_FLOOR_MAP_DATA_SOURCE_LABEL = "pisell-reservation.floor-map.data-source-label";
function useReservationFloorMapMerged(params) {
	const { floorMapProp, baseFloorConfig, dataSources, renderItemByKind, gridDataSourceKey, effectiveMapMode, resolvedFloorPlanId, floorPlanRemoteId, effectiveFloorPlanCode, floorPlanCanvasWidth, floorPlanCanvasHeight, floorPlanName, floorPlanSort, floorPlanStatus, onFloorPlanPersisted, putFloorPlan, isFloorModeControlled, mapEditInternal, setMapEditInternal, floorMapLayoutRef, reservationFloorMapViewportOverlay, setRemoteFloorPlanView, floorMapItemClickBefore, engineLocale, floorMapHudTableDrawer = false } = params;
	return useMemo(() => {
		var _floorMapProp$dataSou, _floorRest$cellSize, _floorRest$itemUnit, _floorRest$floorMapCo, _floorMapProp$renderI, _floorRest$toolbarSho, _floorRest$showDataSo, _floorRest$zoom, _floorRest$floorMapVi, _floorRest$viewportDo;
		const labels = { [gridDataSourceKey]: locales.getText(I18N_FLOOR_MAP_DATA_SOURCE_LABEL) };
		const forms = { [gridDataSourceKey]: getReservationDataSourceFormColumns() };
		const _ref = floorMapProp !== null && floorMapProp !== void 0 ? floorMapProp : {}, { getMergedItemWrapperStyle } = _ref, floorRest = _objectWithoutProperties(_ref, _excluded);
		const prevOnSave = floorRest.onSave;
		const persistTargetId = floorPlanRemoteId !== null && floorPlanRemoteId !== void 0 ? floorPlanRemoteId : resolvedFloorPlanId;
		const onSaveWithApi = wrapFloorMapOnSaveWithRemotePersist({
			canPersist: persistTargetId != null && Number.isFinite(persistTargetId) && Math.floor(persistTargetId) === persistTargetId && persistTargetId > 0,
			persistTargetId: persistTargetId !== null && persistTargetId !== void 0 ? persistTargetId : 0,
			persist: (id, config) => saveViewConfigToShopFloorPlan(id, config, {
				canvasWidth: floorPlanCanvasWidth,
				canvasHeight: floorPlanCanvasHeight,
				code: effectiveFloorPlanCode,
				name: floorPlanName,
				sort: floorPlanSort,
				status: floorPlanStatus,
				putFloorPlan: putFloorPlan != null ? (planId, body) => putFloorPlan(planId, body) : void 0
			}),
			setRemoteFloorPlanView,
			onFloorPlanPersisted,
			prevOnSave,
			onPersistError: toastFloorPlanSaveError
		});
		const mergedDataSources = (_floorMapProp$dataSou = floorMapProp === null || floorMapProp === void 0 ? void 0 : floorMapProp.dataSources) !== null && _floorMapProp$dataSou !== void 0 ? _floorMapProp$dataSou : dataSources;
		return _objectSpread2(_objectSpread2({
			mapLayer: _objectSpread2({
				showGrid: true,
				backgroundColor: "#e8e8e8",
				snapStep: 1
			}, floorRest.mapLayer),
			cellSize: (_floorRest$cellSize = floorRest.cellSize) !== null && _floorRest$cellSize !== void 0 ? _floorRest$cellSize : 10,
			itemUnit: (_floorRest$itemUnit = floorRest.itemUnit) !== null && _floorRest$itemUnit !== void 0 ? _floorRest$itemUnit : "pixel",
			dataSourceLabels: labels,
			dataSourceFormColumns: forms
		}, floorRest), {}, {
			floorMapConfig: (_floorRest$floorMapCo = floorRest.floorMapConfig) !== null && _floorRest$floorMapCo !== void 0 ? _floorRest$floorMapCo : baseFloorConfig,
			dataSources: mergedDataSources,
			renderItemByKind: (_floorMapProp$renderI = floorMapProp === null || floorMapProp === void 0 ? void 0 : floorMapProp.renderItemByKind) !== null && _floorMapProp$renderI !== void 0 ? _floorMapProp$renderI : renderItemByKind,
			mode: effectiveMapMode,
			toolbarShowSave: (_floorRest$toolbarSho = floorRest.toolbarShowSave) !== null && _floorRest$toolbarSho !== void 0 ? _floorRest$toolbarSho : false,
			showDataSourceRecordFormBlock: (_floorRest$showDataSo = floorRest.showDataSourceRecordFormBlock) !== null && _floorRest$showDataSo !== void 0 ? _floorRest$showDataSo : false,
			zoom: _objectSpread2(_objectSpread2({
				defaultScale: 1,
				min: .4,
				max: 2.2,
				step: .1
			}, floorRest.zoom), {}, { autoFitOnMount: ((_floorRest$zoom = floorRest.zoom) === null || _floorRest$zoom === void 0 ? void 0 : _floorRest$zoom.autoFitOnMount) !== false }),
			controls: _objectSpread2({
				showZoom: true,
				showReset: true,
				showFitBounds: true,
				showGridToggle: true,
				horizontalAlign: "end"
			}, floorRest.controls),
			pan: _objectSpread2({
				enabled: true,
				boundary: "none"
			}, floorRest.pan),
			onItemClick: (item, index, event) => {
				var _floorMapProp$onItemC;
				floorMapItemClickBefore === null || floorMapItemClickBefore === void 0 || floorMapItemClickBefore({
					item,
					index,
					event,
					mode: effectiveMapMode,
					dataSources: mergedDataSources
				});
				floorMapProp === null || floorMapProp === void 0 || (_floorMapProp$onItemC = floorMapProp.onItemClick) === null || _floorMapProp$onItemC === void 0 || _floorMapProp$onItemC.call(floorMapProp, item, index, event);
			},
			getMergedItemWrapperStyle,
			onSave: onSaveWithApi,
			floorMapViewportOverlay: (_floorRest$floorMapVi = floorRest.floorMapViewportOverlay) !== null && _floorRest$floorMapVi !== void 0 ? _floorRest$floorMapVi : reservationFloorMapViewportOverlay,
			viewportOverlayPlacement: floorMapHudTableDrawer ? "bottom-start" : floorRest.viewportOverlayPlacement,
			viewportOverlayClassName: floorRest.viewportOverlayClassName,
			canvasTabsBarTrailing: floorRest.canvasTabsBarTrailing,
			viewportDockTrailing: floorRest.viewportDockTrailing === false ? /* @__PURE__ */ React.createElement(React.Fragment, null) : (_floorRest$viewportDo = floorRest.viewportDockTrailing) !== null && _floorRest$viewportDo !== void 0 ? _floorRest$viewportDo : /* @__PURE__ */ React.createElement(ReservationFloorMapCanvasTabsTrailing, {
				isFloorModeControlled,
				mapEditInternal,
				setMapEditInternal,
				floorMapLayoutRef
			})
		});
	}, [
		baseFloorConfig,
		dataSources,
		floorMapProp,
		floorMapHudTableDrawer,
		reservationFloorMapViewportOverlay,
		renderItemByKind,
		gridDataSourceKey,
		effectiveMapMode,
		resolvedFloorPlanId,
		floorPlanRemoteId,
		effectiveFloorPlanCode,
		floorPlanCanvasWidth,
		floorPlanCanvasHeight,
		floorPlanName,
		floorPlanSort,
		floorPlanStatus,
		onFloorPlanPersisted,
		putFloorPlan,
		isFloorModeControlled,
		mapEditInternal,
		floorMapLayoutRef,
		setRemoteFloorPlanView,
		floorMapItemClickBefore,
		engineLocale
	]);
}
//#endregion
export { useReservationFloorMapMerged };
