function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { getSalesOrderId } from "../api/searchService";
/**
 * 订单搜索实时（OS Server delta）相关纯函数。
 *
 * 抽到独立 util 是为了让「数据 hook」（useEsSalesGridData）与「视图」（orderResultViews）
 * 都能复用，且避免 hook ↔ 视图之间的循环 import。
 */

/**
 * 从 OS Server 搜索 delta 回调中提取变更订单列表。
 *
 * 兼容两种响应形态：单模块 `{ data: { list } }` 与聚合 `{ data: { results: [{ module, list }] } }`。
 *
 * @example
 * const deltaItems = normalizeSalesDeltaItems({ data: { list: [{ order_id: 1 }] } });
 */
export function normalizeSalesDeltaItems(res) {
  var _res$data;
  var payload = (_res$data = res === null || res === void 0 ? void 0 : res.data) !== null && _res$data !== void 0 ? _res$data : res;
  if (Array.isArray(payload === null || payload === void 0 ? void 0 : payload.list)) return payload.list;
  var results = Array.isArray(payload === null || payload === void 0 ? void 0 : payload.results) ? payload.results : [];
  var salesGroup = results.find(function (item) {
    return (item === null || item === void 0 ? void 0 : item.module) === 'sales';
  });
  return Array.isArray(salesGroup === null || salesGroup === void 0 ? void 0 : salesGroup.list) ? salesGroup.list : [];
}

/**
 * 只替换已加载列表中存在的订单，不插入新的订单。
 *
 * 实时推送可能只带「变更字段」的精简对象，整体替换会清空其它字段导致行内容闪烁；
 * 用浅合并仅覆盖推送带来的字段，保留已加载的完整字段。
 *
 * @example
 * const nextItems = patchLoadedSalesItems(items, [{ order_id: 1, status: 'paid' }]);
 */
export function patchLoadedSalesItems(currentItems, deltaItems) {
  if (!deltaItems.length) return currentItems;
  var deltaMap = new Map();
  deltaItems.forEach(function (item) {
    var orderId = getSalesOrderId(item);
    if (orderId !== undefined) deltaMap.set(String(orderId), item);
  });
  if (deltaMap.size === 0) return currentItems;
  var changed = false;
  var nextItems = currentItems.map(function (item) {
    var orderId = getSalesOrderId(item);
    if (orderId === undefined) return item;
    var next = deltaMap.get(String(orderId));
    if (!next) return item;
    changed = true;
    return _objectSpread(_objectSpread({}, item), next);
  });
  return changed ? nextItems : currentItems;
}