require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_reservationMock = require("./reservationMock.js");
const require_floorPlanApi = require("./floorPlanApi.js");
const require_reservationConstants = require("../reservationConstants.js");
let react = require("react");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
//#region src/plus/pisellReservation/floorMap/useReservationFloorPlan.ts
/**
* 平面图方案：GET 远程 layout + 与预约默认 floorMapConfig 合并（含按 code 的店铺订阅推送）
*/
function useReservationFloorPlan(options) {
	const { resolvedFloorPlanId, effectiveFloorPlanCode, gridDataSourceKey, floorMapConfigFromProp, engineLocale } = options;
	const [remoteFloorPlanView, setRemoteFloorPlanView] = (0, react.useState)(null);
	const [floorPlanRemoteId, setFloorPlanRemoteId] = (0, react.useState)(null);
	const subscriberIdRef = (0, react.useRef)((0, _pisell_utils.getUniqueId)("floor-plan-code"));
	(0, react.useEffect)(() => {
		if (resolvedFloorPlanId == null) {
			setRemoteFloorPlanView(null);
			setFloorPlanRemoteId(null);
			return;
		}
		setRemoteFloorPlanView(null);
		setFloorPlanRemoteId(null);
	}, [
		resolvedFloorPlanId,
		effectiveFloorPlanCode,
		gridDataSourceKey
	]);
	const applyRemoteDetail = (0, react.useCallback)((detail) => {
		if (typeof detail.id === "number" && Number.isFinite(detail.id)) setFloorPlanRemoteId(Math.trunc(detail.id));
		const fromLayoutField = (0, _pisell_materials.parseLayoutFieldToViewConfigPatch)(detail.layout);
		if (fromLayoutField) {
			setRemoteFloorPlanView(fromLayoutField);
			return;
		}
		if (Array.isArray(detail.layout) && detail.layout.length > 0) setRemoteFloorPlanView({ sceneElements: require_floorPlanApi.shopLayoutToSceneElements(detail.layout, {
			canvasId: require_reservationConstants.RESERVATION_MAIN_CANVAS_ID,
			dataSourceKey: gridDataSourceKey
		}) });
	}, [gridDataSourceKey]);
	(0, _pisell_materials.useShopFloorPlanSubscription)({
		enabled: resolvedFloorPlanId != null,
		code: effectiveFloorPlanCode,
		subscriberId: subscriberIdRef.current,
		ensureByCode: require_floorPlanApi.ensureShopFloorPlanByCode,
		unsubscribeQuery: require_floorPlanApi.unsubscribeShopFloorPlanCodeQuery,
		onDetail: applyRemoteDetail,
		onLoadError: require_floorPlanApi.toastFloorPlanLoadError
	});
	return {
		baseFloorConfig: (0, react.useMemo)(() => {
			var _parentCfg$canvases, _defaults$elementKind, _defaults$elementKind2, _parentCfg$sceneEleme, _parentCfg$edges, _parentCfg$datasource, _defaults$elementKind3, _defaults$elementKind4;
			const defaults = require_reservationMock.getReservationFloorMapShellConfig();
			const parentCfg = floorMapConfigFromProp;
			let merged = parentCfg ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, defaults), parentCfg), {}, {
				canvases: (_parentCfg$canvases = parentCfg.canvases) !== null && _parentCfg$canvases !== void 0 ? _parentCfg$canvases : defaults.canvases,
				canvasUi: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, defaults.canvasUi), parentCfg.canvasUi),
				elementKinds: require_reservationMock.mergeFloorMapElementKindsWithDefaults((_defaults$elementKind = defaults.elementKinds) !== null && _defaults$elementKind !== void 0 ? _defaults$elementKind : [], parentCfg.elementKinds),
				elementKindCategories: require_reservationMock.mergeFloorMapElementKindCategoriesWithDefaults((_defaults$elementKind2 = defaults.elementKindCategories) !== null && _defaults$elementKind2 !== void 0 ? _defaults$elementKind2 : [], parentCfg.elementKindCategories),
				sceneElements: (_parentCfg$sceneEleme = parentCfg.sceneElements) !== null && _parentCfg$sceneEleme !== void 0 ? _parentCfg$sceneEleme : defaults.sceneElements,
				edges: (_parentCfg$edges = parentCfg.edges) !== null && _parentCfg$edges !== void 0 ? _parentCfg$edges : defaults.edges,
				datasourceComponents: (_parentCfg$datasource = parentCfg.datasourceComponents) !== null && _parentCfg$datasource !== void 0 ? _parentCfg$datasource : defaults.datasourceComponents
			}) : defaults;
			if (remoteFloorPlanView != null) merged = (0, _pisell_materials.mergeFloorPlanViewRemote)(merged, remoteFloorPlanView);
			merged = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, merged), {}, {
				elementKinds: require_reservationMock.mergeFloorMapElementKindsWithDefaults((_defaults$elementKind3 = defaults.elementKinds) !== null && _defaults$elementKind3 !== void 0 ? _defaults$elementKind3 : [], merged.elementKinds),
				elementKindCategories: require_reservationMock.mergeFloorMapElementKindCategoriesWithDefaults((_defaults$elementKind4 = defaults.elementKindCategories) !== null && _defaults$elementKind4 !== void 0 ? _defaults$elementKind4 : [], merged.elementKindCategories)
			});
			return require_reservationMock.applyReservationFloorMapLiveLabels(merged);
		}, [
			floorMapConfigFromProp,
			remoteFloorPlanView,
			engineLocale
		]),
		floorPlanRemoteId,
		setRemoteFloorPlanView
	};
}
//#endregion
exports.useReservationFloorPlan = useReservationFloorPlan;
