import { useEffect, useRef } from 'react';
import { pickFloorPlanDetail } from "../shopFloorPlan/shopFloorPlanPure";
/**
 * 按 code 订阅店铺平面图：首包 Promise + WebSocket callback 均归一为 `onDetail`。
 * 不包含与业务默认 {@link FloorMapViewConfig} 的合并（由调用方在 `onDetail` 或后续 useMemo 完成）。
 */
export function useShopFloorPlanSubscription(params) {
  var enabled = params.enabled,
    code = params.code,
    subscriberId = params.subscriberId,
    ensureByCode = params.ensureByCode,
    unsubscribeQuery = params.unsubscribeQuery,
    onDetail = params.onDetail,
    onLoadError = params.onLoadError,
    _params$osServer = params.osServer,
    osServer = _params$osServer === void 0 ? true : _params$osServer;
  var onDetailRef = useRef(onDetail);
  onDetailRef.current = onDetail;
  var onLoadErrorRef = useRef(onLoadError);
  onLoadErrorRef.current = onLoadError;
  useEffect(function () {
    if (!enabled) return;
    var cancelled = false;
    var subId = subscriberId;
    void ensureByCode(code, {
      osServer: osServer,
      subscriberId: subId,
      callback: function callback(res) {
        if (cancelled) return;
        var detail = pickFloorPlanDetail(res);
        if (!detail) return;
        onDetailRef.current(detail);
      }
    }).then(function (detail) {
      if (cancelled || !detail) return;
      onDetailRef.current(detail);
    }).catch(function (err) {
      if (!cancelled) {
        var _onLoadErrorRef$curre;
        (_onLoadErrorRef$curre = onLoadErrorRef.current) === null || _onLoadErrorRef$curre === void 0 || _onLoadErrorRef$curre.call(onLoadErrorRef, err);
      }
    });
    return function () {
      cancelled = true;
      void unsubscribeQuery(subId);
    };
  }, [enabled, code, subscriberId, ensureByCode, unsubscribeQuery, osServer]);
}