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/pisellGlobalSearch/hooks/useEsSalesGridData.ts
var useEsSalesGridData_exports = {};
__export(useEsSalesGridData_exports, {
  useEsSalesGridData: () => useEsSalesGridData
});
module.exports = __toCommonJS(useEsSalesGridData_exports);
var import_react = require("react");
var import_utils = require("@pisell/utils");
var import_searchService = require("../api/searchService");
var import_salesRealtime = require("../utils/salesRealtime");
function useEsSalesGridData(params) {
  const { keyword, searchMode, perPage, filters, sort, subscriberPrefix, onDelta } = params;
  const [items, setItems] = (0, import_react.useState)([]);
  const [total, setTotal] = (0, import_react.useState)(0);
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [error, setError] = (0, import_react.useState)(null);
  const [page, setPage] = (0, import_react.useState)(1);
  const reqIdRef = (0, import_react.useRef)(0);
  const subscriberIdRef = (0, import_react.useRef)((0, import_utils.getUniqueId)(subscriberPrefix));
  const onDeltaRef = (0, import_react.useRef)(onDelta);
  onDeltaRef.current = onDelta;
  const applySalesDelta = (0, import_react.useCallback)((res) => {
    var _a;
    const deltaItems = (0, import_salesRealtime.normalizeSalesDeltaItems)(res);
    if (deltaItems.length === 0) return;
    console.log("[GlobalSearch][Realtime] 收到实时数据", {
      count: deltaItems.length,
      subscriberPrefix,
      orderIds: deltaItems.map((it) => (it == null ? void 0 : it.order_id) ?? (it == null ? void 0 : it.id) ?? (it == null ? void 0 : it.key)),
      items: deltaItems,
      raw: res
    });
    setItems((prev) => (0, import_salesRealtime.patchLoadedSalesItems)(prev, deltaItems));
    (_a = onDeltaRef.current) == null ? void 0 : _a.call(onDeltaRef, deltaItems);
  }, [subscriberPrefix]);
  const fetchPage = (0, import_react.useCallback)(
    async (targetPage, append) => {
      const kw = (keyword ?? "").trim();
      if (!kw) {
        setItems([]);
        setTotal(0);
        setError(null);
        setPage(1);
        void (0, import_searchService.unsubscribeSalesSearch)(subscriberIdRef.current);
        return;
      }
      const reqId = ++reqIdRef.current;
      setLoading(true);
      setError(null);
      try {
        const res = await (0, import_searchService.searchSalesModule)({
          keyword: kw,
          searchMode,
          filters,
          sort,
          page: targetPage,
          perPage,
          subscriberId: subscriberIdRef.current,
          callback: applySalesDelta,
          resetVisibleOrderIds: !append
        });
        if (reqId !== reqIdRef.current) return;
        setTotal(res.total);
        setItems((prev) => append ? [...prev, ...res.items] : res.items);
        setPage(targetPage);
      } catch (e) {
        if (reqId !== reqIdRef.current) return;
        setError(e);
        if (!append) {
          setItems([]);
          setTotal(0);
        }
      } finally {
        if (reqId === reqIdRef.current) setLoading(false);
      }
    },
    [keyword, searchMode, perPage, filters, sort, applySalesDelta]
  );
  (0, import_react.useEffect)(() => {
    const subscriberId = subscriberIdRef.current;
    return () => {
      void (0, import_searchService.unsubscribeSalesSearch)(subscriberId);
    };
  }, []);
  (0, import_react.useEffect)(() => {
    setPage(1);
    fetchPage(1, false);
  }, [fetchPage]);
  const hasMore = items.length < total;
  return {
    items,
    total,
    loading,
    error,
    page,
    hasMore,
    fetchPage,
    subscriberId: subscriberIdRef.current
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useEsSalesGridData
});
