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 React from 'react';
import { Spin } from 'antd';
// @ts-ignore - 与仓库其它私有物料保持一致的忽略写法
import { locales } from '@pisell/utils';
import { getLocaleLabel } from "../utils/getLocaleLabel";
/**
 * All 模式下按 Provider 分组的结果列表。
 *
 * - 每个组独立 loading / empty / error，先到先显示；
 * - 超过展示上限时显示 "View all N"，点击切到对应 Tab。
 */
var GroupedResults = function GroupedResults(_ref) {
  var providers = _ref.providers,
    stateMap = _ref.stateMap,
    keyword = _ref.keyword,
    mode = _ref.mode,
    locale = _ref.locale,
    onJumpTab = _ref.onJumpTab,
    onClose = _ref.onClose,
    navigate = _ref.navigate,
    onOpenDetail = _ref.onOpenDetail,
    onRealtimePatchItems = _ref.onRealtimePatchItems,
    detailRefreshVersions = _ref.detailRefreshVersions;
  var ctxBase = {
    close: onClose,
    navigate: navigate,
    onOpenDetail: onOpenDetail,
    onRealtimePatchItems: onRealtimePatchItems,
    detailRefreshVersions: detailRefreshVersions
  };

  // 如果所有 group 都为空且都不在 loading，整体显示空态；
  // 注意：拥有 renderResults 的 group 视为"始终非空"——它自管渲染。
  var allEmpty = providers.every(function (p) {
    var _s$list$length, _s$list;
    if (p.renderResults) return false;
    var s = stateMap[p.key];
    return !(s !== null && s !== void 0 && s.loading) && ((_s$list$length = s === null || s === void 0 || (_s$list = s.list) === null || _s$list === void 0 ? void 0 : _s$list.length) !== null && _s$list$length !== void 0 ? _s$list$length : 0) === 0 && !(s !== null && s !== void 0 && s.error);
  });
  if (allEmpty) {
    return /*#__PURE__*/React.createElement("div", {
      className: "pisell-global-search__empty"
    }, keyword ? locales.getText('pisell-global-search.empty') : locales.getText('pisell-global-search.empty.idle'));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-global-search__groups"
  }, providers.map(function (provider) {
    var state = stateMap[provider.key];
    if (!state) return null;
    var loading = state.loading,
      list = state.list,
      total = state.total,
      error = state.error;
    var showGroupHeader = mode !== 'business';

    // 完全空 + 未加载 + 无错误，跳过该组，避免一堆空标题
    // 但拥有 renderResults 的 Provider 由它自管展示，永远不跳过
    if (!provider.renderResults && !loading && list.length === 0 && !error) {
      return null;
    }
    return /*#__PURE__*/React.createElement("section", {
      key: provider.key,
      className: "pisell-global-search__group"
    }, showGroupHeader && /*#__PURE__*/React.createElement("header", {
      className: "pisell-global-search__group-header"
    }, /*#__PURE__*/React.createElement("span", {
      className: "pisell-global-search__group-title"
    }, provider.icon, getLocaleLabel(provider.label, locale), total > 0 && /*#__PURE__*/React.createElement("span", null, "(", total, ")")), total > list.length && /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: "pisell-global-search__view-all",
      onClick: function onClick() {
        return onJumpTab(provider.key);
      }
    }, locales.getText('pisell-global-search.view-all'))), /*#__PURE__*/React.createElement("div", {
      className: "pisell-global-search__group-body".concat(provider.renderResults ? ' pisell-global-search__group-body--bare' : '')
    }, provider.renderResults ?
    // Provider 提供 renderResults → 接管 group body 渲染
    provider.renderResults({
      state: state,
      keyword: keyword,
      mode: mode,
      layout: 'grouped'
    }, _objectSpread({
      keyword: keyword
    }, ctxBase)) : loading && list.length === 0 ? /*#__PURE__*/React.createElement("div", {
      className: "pisell-global-search__group-loading"
    }, /*#__PURE__*/React.createElement(Spin, {
      size: "small"
    })) : error ? /*#__PURE__*/React.createElement("div", {
      className: "pisell-global-search__group-error"
    }, locales.getText('pisell-global-search.error')) : /*#__PURE__*/React.createElement("ul", {
      className: "pisell-global-search__group-list"
    }, list.map(function (item, index) {
      return /*#__PURE__*/React.createElement("li", {
        key: getItemKey(item, index),
        className: "pisell-global-search__group-item"
      }, provider.renderItem(item, _objectSpread({
        keyword: keyword
      }, ctxBase)));
    }))));
  }));
};

/**
 * 兼容业务 item 没有 id 的场景；优先用 id，其次用 index。
 */
function getItemKey(item, index) {
  if (item && _typeof(item) === 'object') {
    var obj = item;
    if (typeof obj.id === 'string' || typeof obj.id === 'number') {
      return obj.id;
    }
    if (typeof obj.key === 'string' || typeof obj.key === 'number') {
      return obj.key;
    }
  }
  return index;
}
export default GroupedResults;