import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { applyReservationFloorMapLiveLabels, getReservationFloorMapShellConfig, mergeFloorMapElementKindCategoriesWithDefaults, mergeFloorMapElementKindsWithDefaults } from "./reservationMock.js";
import { ensureShopFloorPlanByCode, shopLayoutToSceneElements as shopLayoutToSceneElements$1, toastFloorPlanLoadError, unsubscribeShopFloorPlanCodeQuery } from "./floorPlanApi.js";
import { RESERVATION_MAIN_CANVAS_ID } from "../reservationConstants.js";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { getUniqueId } from "@pisell/utils";
import { mergeFloorPlanViewRemote, parseLayoutFieldToViewConfigPatch, useShopFloorPlanSubscription } from "@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] = useState(null);
	const [floorPlanRemoteId, setFloorPlanRemoteId] = useState(null);
	const subscriberIdRef = useRef(getUniqueId("floor-plan-code"));
	useEffect(() => {
		if (resolvedFloorPlanId == null) {
			setRemoteFloorPlanView(null);
			setFloorPlanRemoteId(null);
			return;
		}
		setRemoteFloorPlanView(null);
		setFloorPlanRemoteId(null);
	}, [
		resolvedFloorPlanId,
		effectiveFloorPlanCode,
		gridDataSourceKey
	]);
	const applyRemoteDetail = useCallback((detail) => {
		if (typeof detail.id === "number" && Number.isFinite(detail.id)) setFloorPlanRemoteId(Math.trunc(detail.id));
		const fromLayoutField = parseLayoutFieldToViewConfigPatch(detail.layout);
		if (fromLayoutField) {
			setRemoteFloorPlanView(fromLayoutField);
			return;
		}
		if (Array.isArray(detail.layout) && detail.layout.length > 0) setRemoteFloorPlanView({ sceneElements: shopLayoutToSceneElements$1(detail.layout, {
			canvasId: RESERVATION_MAIN_CANVAS_ID,
			dataSourceKey: gridDataSourceKey
		}) });
	}, [gridDataSourceKey]);
	useShopFloorPlanSubscription({
		enabled: resolvedFloorPlanId != null,
		code: effectiveFloorPlanCode,
		subscriberId: subscriberIdRef.current,
		ensureByCode: ensureShopFloorPlanByCode,
		unsubscribeQuery: unsubscribeShopFloorPlanCodeQuery,
		onDetail: applyRemoteDetail,
		onLoadError: toastFloorPlanLoadError
	});
	return {
		baseFloorConfig: useMemo(() => {
			var _parentCfg$canvases, _defaults$elementKind, _defaults$elementKind2, _parentCfg$sceneEleme, _parentCfg$edges, _parentCfg$datasource, _defaults$elementKind3, _defaults$elementKind4;
			const defaults = getReservationFloorMapShellConfig();
			const parentCfg = floorMapConfigFromProp;
			let merged = parentCfg ? _objectSpread2(_objectSpread2(_objectSpread2({}, defaults), parentCfg), {}, {
				canvases: (_parentCfg$canvases = parentCfg.canvases) !== null && _parentCfg$canvases !== void 0 ? _parentCfg$canvases : defaults.canvases,
				canvasUi: _objectSpread2(_objectSpread2({}, defaults.canvasUi), parentCfg.canvasUi),
				elementKinds: mergeFloorMapElementKindsWithDefaults((_defaults$elementKind = defaults.elementKinds) !== null && _defaults$elementKind !== void 0 ? _defaults$elementKind : [], parentCfg.elementKinds),
				elementKindCategories: 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 = mergeFloorPlanViewRemote(merged, remoteFloorPlanView);
			merged = _objectSpread2(_objectSpread2({}, merged), {}, {
				elementKinds: mergeFloorMapElementKindsWithDefaults((_defaults$elementKind3 = defaults.elementKinds) !== null && _defaults$elementKind3 !== void 0 ? _defaults$elementKind3 : [], merged.elementKinds),
				elementKindCategories: mergeFloorMapElementKindCategoriesWithDefaults((_defaults$elementKind4 = defaults.elementKindCategories) !== null && _defaults$elementKind4 !== void 0 ? _defaults$elementKind4 : [], merged.elementKindCategories)
			});
			return applyReservationFloorMapLiveLabels(merged);
		}, [
			floorMapConfigFromProp,
			remoteFloorPlanView,
			engineLocale
		]),
		floorPlanRemoteId,
		setRemoteFloorPlanView
	};
}
//#endregion
export { useReservationFloorPlan };
