var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/plus/pisellGlobalSearch/providers/salesProvider.tsx
var salesProvider_exports = {};
__export(salesProvider_exports, {
  default: () => salesProvider_default,
  salesProvider: () => salesProvider
});
module.exports = __toCommonJS(salesProvider_exports);
var import_react = __toESM(require("react"));
var import_searchService = require("../api/searchService");
var import_salesRealtime = require("../utils/salesRealtime");
var import_orderResultViews = require("./orderResultViews");
var salesProvider = {
  key: "order",
  label: {
    en: "Sales",
    "zh-CN": "订单",
    "zh-HK": "訂單",
    ja: "売上",
    pt: "Vendas"
  },
  order: 20,
  search: async ({
    keyword,
    signal,
    mode,
    pageSize,
    scope,
    onRealtimePatch,
    realtimeSubscriberId,
    onRealtimeCleanup
  }) => {
    const kw = (keyword ?? "").trim();
    const searchMode = (0, import_searchService.searchModeFromMode)(mode);
    if (!kw) {
      return { list: [], total: 0 };
    }
    const runSearch = scope === "all" ? import_searchService.searchAggregateSales : import_searchService.searchSalesModule;
    if (mode === "business") {
      if (scope === "single") {
        return {
          list: [],
          total: 0
        };
      }
      const resolvedRealtimeSubscriberId = paramsRealtimeSubscriberId({
        scope,
        mode,
        id: realtimeSubscriberId
      });
      if (resolvedRealtimeSubscriberId) {
        onRealtimeCleanup == null ? void 0 : onRealtimeCleanup(() => {
          void (0, import_searchService.unsubscribeAggregateSalesSearch)(resolvedRealtimeSubscriberId);
        });
      }
      const res = await runSearch({
        keyword: kw,
        searchMode,
        perPage: pageSize ?? 20,
        signal,
        subscriberId: resolvedRealtimeSubscriberId,
        resetVisibleOrderIds: true,
        callback: resolvedRealtimeSubscriberId ? (realtimeRes) => {
          const deltaItems = (0, import_salesRealtime.normalizeSalesDeltaItems)(realtimeRes);
          console.log(
            "[GlobalSearch][Realtime] 收到实时数据(business/all)",
            {
              count: deltaItems.length,
              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: realtimeRes
            }
          );
          if (deltaItems.length > 0) {
            onRealtimePatch == null ? void 0 : onRealtimePatch(deltaItems);
          }
        } : void 0
      });
      return {
        list: res.items,
        total: res.total
      };
    }
    return {
      list: [],
      total: 0
    };
  },
  // renderItem 仅作为兜底（renderResults 出错时被使用），实际不会走到
  renderItem: (item) => /* @__PURE__ */ import_react.default.createElement("div", { style: { padding: 6 } }, /* @__PURE__ */ import_react.default.createElement("div", { style: { fontWeight: 600 } }, item.shop_order_number || item.order_number || item.order_id), item.customer_name && /* @__PURE__ */ import_react.default.createElement("div", { style: { fontSize: 12, color: "#6b7280" } }, item.customer_name)),
  renderResults: ({ state, keyword, mode, layout }, ctx) => {
    if (mode === "business") {
      if (layout === "single") {
        return /* @__PURE__ */ import_react.default.createElement(import_orderResultViews.OrderBusinessList, { keyword });
      }
      return /* @__PURE__ */ import_react.default.createElement(
        import_orderResultViews.OrderBusinessDetails,
        {
          items: state.list,
          loading: state.loading,
          keyword,
          detailRefreshVersions: ctx.detailRefreshVersions
        }
      );
    }
    return /* @__PURE__ */ import_react.default.createElement(
      import_orderResultViews.OrderAdminGrid,
      {
        keyword,
        providerKey: "order",
        onOpenDetail: ctx.onOpenDetail,
        onRealtimePatchItems: ctx.onRealtimePatchItems,
        onReportTotal: ctx.onReportTotal,
        previewLimit: layout === "grouped" ? 5 : void 0
      }
    );
  }
};
function paramsRealtimeSubscriberId(params) {
  if (params.scope !== "all" || params.mode !== "business") return void 0;
  return params.id;
}
var salesProvider_default = salesProvider;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  salesProvider
});
