var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/pisellReservation/floorMap/useReservationFloorPlan.ts
var useReservationFloorPlan_exports = {};
__export(useReservationFloorPlan_exports, {
  useReservationFloorPlan: () => useReservationFloorPlan
});
module.exports = __toCommonJS(useReservationFloorPlan_exports);
var import_react = require("react");
var import_materials = require("@pisell/materials");
var import_utils = require("@pisell/utils");
var import_floorPlanApi = require("./floorPlanApi");
var import_reservationMock = require("./reservationMock");
var import_reservationConstants = require("../reservationConstants");
function useReservationFloorPlan(options) {
  const {
    resolvedFloorPlanId,
    effectiveFloorPlanCode,
    gridDataSourceKey,
    floorMapConfigFromProp,
    engineLocale
  } = options;
  const [remoteFloorPlanView, setRemoteFloorPlanView] = (0, import_react.useState)(null);
  const [floorPlanRemoteId, setFloorPlanRemoteId] = (0, import_react.useState)(
    null
  );
  const subscriberIdRef = (0, import_react.useRef)((0, import_utils.getUniqueId)("floor-plan-code"));
  (0, import_react.useEffect)(() => {
    if (resolvedFloorPlanId == null) {
      setRemoteFloorPlanView(null);
      setFloorPlanRemoteId(null);
      return;
    }
    setRemoteFloorPlanView(null);
    setFloorPlanRemoteId(null);
  }, [resolvedFloorPlanId, effectiveFloorPlanCode, gridDataSourceKey]);
  const applyRemoteDetail = (0, import_react.useCallback)(
    (detail) => {
      if (typeof detail.id === "number" && Number.isFinite(detail.id)) {
        setFloorPlanRemoteId(Math.trunc(detail.id));
      }
      const fromLayoutField = (0, import_materials.parseLayoutFieldToViewConfigPatch)(detail.layout);
      if (fromLayoutField) {
        setRemoteFloorPlanView(fromLayoutField);
        return;
      }
      if (Array.isArray(detail.layout) && detail.layout.length > 0) {
        setRemoteFloorPlanView({
          sceneElements: (0, import_floorPlanApi.shopLayoutToSceneElements)(
            detail.layout,
            {
              canvasId: import_reservationConstants.RESERVATION_MAIN_CANVAS_ID,
              dataSourceKey: gridDataSourceKey
            }
          )
        });
      }
    },
    [gridDataSourceKey]
  );
  (0, import_materials.useShopFloorPlanSubscription)({
    enabled: resolvedFloorPlanId != null,
    code: effectiveFloorPlanCode,
    subscriberId: subscriberIdRef.current,
    ensureByCode: import_floorPlanApi.ensureShopFloorPlanByCode,
    unsubscribeQuery: import_floorPlanApi.unsubscribeShopFloorPlanCodeQuery,
    onDetail: applyRemoteDetail,
    onLoadError: import_floorPlanApi.toastFloorPlanLoadError
  });
  const baseFloorConfig = (0, import_react.useMemo)(() => {
    const defaults = (0, import_reservationMock.getReservationFloorMapShellConfig)();
    const parentCfg = floorMapConfigFromProp;
    let merged = parentCfg ? {
      ...defaults,
      ...parentCfg,
      canvases: parentCfg.canvases ?? defaults.canvases,
      canvasUi: { ...defaults.canvasUi, ...parentCfg.canvasUi },
      elementKinds: (0, import_reservationMock.mergeFloorMapElementKindsWithDefaults)(
        defaults.elementKinds ?? [],
        parentCfg.elementKinds
      ),
      elementKindCategories: (0, import_reservationMock.mergeFloorMapElementKindCategoriesWithDefaults)(
        defaults.elementKindCategories ?? [],
        parentCfg.elementKindCategories
      ),
      sceneElements: parentCfg.sceneElements ?? defaults.sceneElements,
      edges: parentCfg.edges ?? defaults.edges,
      datasourceComponents: parentCfg.datasourceComponents ?? defaults.datasourceComponents
    } : defaults;
    if (remoteFloorPlanView != null) {
      merged = (0, import_materials.mergeFloorPlanViewRemote)(merged, remoteFloorPlanView);
    }
    merged = {
      ...merged,
      elementKinds: (0, import_reservationMock.mergeFloorMapElementKindsWithDefaults)(
        defaults.elementKinds ?? [],
        merged.elementKinds
      ),
      elementKindCategories: (0, import_reservationMock.mergeFloorMapElementKindCategoriesWithDefaults)(
        defaults.elementKindCategories ?? [],
        merged.elementKindCategories
      )
    };
    return (0, import_reservationMock.applyReservationFloorMapLiveLabels)(merged);
  }, [floorMapConfigFromProp, remoteFloorPlanView, engineLocale]);
  return {
    baseFloorConfig,
    floorPlanRemoteId,
    setRemoteFloorPlanView
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useReservationFloorPlan
});
