var _excluded = ["dataSource", "isShowScanner", "isShowCamera", "isShowNFC", "isShowNewButton", "newButtonAlign", "newButtonText", "renderType", "containerProps", "isShowSearch", "isShowSearchCamera", "searchIcon", "placeholder", "isShowSearchButton", "resultDisplay", "resultSort", "style", "loading", "keyword", "onKeywordChange", "onSearch", "onAdd", "onRowClick", "variant"];
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); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { forwardRef, useImperativeHandle } from 'react';
import { Button, Empty, Spin } from 'antd';
import { PlusCircle } from '@pisell/icon';
import { locales } from '@pisell/utils';
import useEngineContext from "../../hooks/useEngineContext";
import localeTexts from "./locales";
import "./index.less";
import DeviceIconGroup from "./components/deviceIconGroup";
import SearchSection from "./components/searchSection";
import CompensatedPisellContainer from "../compensatedPisellContainer";
import { withResponsive } from "../../responsive";
var ComprehensiveSearch = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _context$appHelper, _containerProps$open;
  var _ref$dataSource = _ref.dataSource,
    dataSource = _ref$dataSource === void 0 ? [] : _ref$dataSource,
    _ref$isShowScanner = _ref.isShowScanner,
    isShowScanner = _ref$isShowScanner === void 0 ? true : _ref$isShowScanner,
    _ref$isShowCamera = _ref.isShowCamera,
    isShowCamera = _ref$isShowCamera === void 0 ? true : _ref$isShowCamera,
    _ref$isShowNFC = _ref.isShowNFC,
    isShowNFC = _ref$isShowNFC === void 0 ? true : _ref$isShowNFC,
    _ref$isShowNewButton = _ref.isShowNewButton,
    isShowNewButton = _ref$isShowNewButton === void 0 ? true : _ref$isShowNewButton,
    _ref$newButtonAlign = _ref.newButtonAlign,
    newButtonAlign = _ref$newButtonAlign === void 0 ? 'left' : _ref$newButtonAlign,
    newButtonText = _ref.newButtonText,
    renderType = _ref.renderType,
    _ref$containerProps = _ref.containerProps,
    containerProps = _ref$containerProps === void 0 ? {} : _ref$containerProps,
    _ref$isShowSearch = _ref.isShowSearch,
    isShowSearch = _ref$isShowSearch === void 0 ? true : _ref$isShowSearch,
    _ref$isShowSearchCame = _ref.isShowSearchCamera,
    isShowSearchCamera = _ref$isShowSearchCame === void 0 ? true : _ref$isShowSearchCame,
    _ref$searchIcon = _ref.searchIcon,
    searchIcon = _ref$searchIcon === void 0 ? 'pisell2-search-lg' : _ref$searchIcon,
    placeholder = _ref.placeholder,
    _ref$isShowSearchButt = _ref.isShowSearchButton,
    isShowSearchButton = _ref$isShowSearchButt === void 0 ? true : _ref$isShowSearchButt,
    _ref$resultDisplay = _ref.resultDisplay,
    resultDisplay = _ref$resultDisplay === void 0 ? 'list' : _ref$resultDisplay,
    _ref$resultSort = _ref.resultSort,
    resultSort = _ref$resultSort === void 0 ? 'asc' : _ref$resultSort,
    style = _ref.style,
    loading = _ref.loading,
    keyword = _ref.keyword,
    onKeywordChange = _ref.onKeywordChange,
    onSearch = _ref.onSearch,
    onAdd = _ref.onAdd,
    onRowClick = _ref.onRowClick,
    variant = _ref.variant,
    props = _objectWithoutProperties(_ref, _excluded);
  var context = useEngineContext();
  var isPhoneVariant = variant === 'phone';
  var finalRenderType = isPhoneVariant && (renderType === 'modal' || renderType === 'drawer') ? 'drawer' : renderType;
  locales.init(localeTexts, ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.storage) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.get('umi_locale')) || 'en');
  var handleAdd = function handleAdd() {
    onAdd === null || onAdd === void 0 || onAdd();
  };
  var renderSearchSection = function renderSearchSection() {
    if (!isShowSearch) return null;
    return /*#__PURE__*/React.createElement(SearchSection, {
      value: keyword,
      onChange: onKeywordChange,
      onSearch: onSearch,
      loading: loading,
      placeholder: placeholder
    });
  };
  useImperativeHandle(ref, function () {
    return {};
  });
  var renderResults = function renderResults() {
    if ( /*#__PURE__*/React.isValidElement(resultDisplay)) {
      return resultDisplay;
    }

    // 如果是自定义组件或渲染函数
    if (typeof resultDisplay === 'function') {
      var result = resultDisplay(dataSource);
      if ( /*#__PURE__*/React.isValidElement(result)) {
        return result;
      }
      return null;
    }
    if (loading) {
      return /*#__PURE__*/React.createElement(Spin, null, /*#__PURE__*/React.createElement("div", {
        className: "comprehensive-search__loading"
      }));
    }

    //@ts-ignore
    if (dataSource.length === 0 && props.__designMode !== 'design') {
      return /*#__PURE__*/React.createElement(Empty, {
        description: locales.getText('pisell2.comprehensive-search.empty'),
        className: "comprehensive-search__empty"
      });
    }
    var resultClass = resultDisplay === 'list' ? 'comprehensive-search__results--list' : 'comprehensive-search__results--grid';

    // 渲染所有字段值
    var renderAllFields = function renderAllFields(result) {
      return Object.entries(result).map(function (_ref2) {
        var _ref3 = _slicedToArray(_ref2, 2),
          key = _ref3[0],
          value = _ref3[1];
        if (value == null) return null;
        var displayValue;
        if (Array.isArray(value)) {
          displayValue = value.join(', ');
        } else if (_typeof(value) === 'object') {
          displayValue = JSON.stringify(value);
        } else {
          displayValue = String(value);
        }
        return /*#__PURE__*/React.createElement("div", {
          key: key,
          className: "comprehensive-search__result-field"
        }, /*#__PURE__*/React.createElement("span", {
          className: "comprehensive-search__result-field-value"
        }, displayValue));
      });
    };
    return /*#__PURE__*/React.createElement("div", {
      className: "comprehensive-search__results ".concat(resultClass)
    }, dataSource.map(function (result, index) {
      return /*#__PURE__*/React.createElement("div", {
        key: result.id,
        className: "comprehensive-search__result-item"
      }, /*#__PURE__*/React.createElement("div", {
        className: "comprehensive-search__result-fields",
        onClick: function onClick() {
          return onRowClick === null || onRowClick === void 0 ? void 0 : onRowClick(result, index);
        }
      }, renderAllFields(result)));
    }));
  };

  // 根据按钮对齐方式决定布局
  var getTopRowLayout = function getTopRowLayout() {
    var newButton = /*#__PURE__*/React.createElement(Button, {
      className: "comprehensive-search__new-button",
      onClick: handleAdd,
      type: "primary"
    }, newButtonText || locales.getText('pisell2.text.new-client'), /*#__PURE__*/React.createElement(PlusCircle, {
      style: {
        fontSize: 20
      }
    }));
    var innerProps = {
      isShowCamera: isShowCamera,
      isShowNFC: isShowNFC,
      isShowScanner: isShowScanner
    };
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
      className: "comprehensive-search__third"
    }, newButtonAlign === 'left' ? newButton : newButtonAlign === 'right' ? /*#__PURE__*/React.createElement(DeviceIconGroup, innerProps) : null), /*#__PURE__*/React.createElement("div", {
      className: "comprehensive-search__third"
    }, newButtonAlign === 'center' ? newButton : null), /*#__PURE__*/React.createElement("div", {
      className: "comprehensive-search__third"
    }, newButtonAlign === 'left' ? /*#__PURE__*/React.createElement(DeviceIconGroup, innerProps) : newButtonAlign === 'right' ? newButton : /*#__PURE__*/React.createElement(DeviceIconGroup, innerProps)));
  };
  var renderContent = function renderContent() {
    return /*#__PURE__*/React.createElement("div", _extends({
      className: "comprehensive-search",
      style: style
    }, props), /*#__PURE__*/React.createElement("div", {
      className: "comprehensive-search__top-section"
    }, /*#__PURE__*/React.createElement("div", {
      className: "comprehensive-search__top-row"
    }, getTopRowLayout())), renderSearchSection(), /*#__PURE__*/React.createElement("div", {
      className: "comprehensive-search__results-container"
    }, renderResults()));
  };
  return /*#__PURE__*/React.createElement(CompensatedPisellContainer, {
    renderType: finalRenderType,
    showHandlerBar: isPhoneVariant && finalRenderType === 'drawer',
    onHandlerBarClick: containerProps.onClose || containerProps.onCancel || function () {},
    props: isPhoneVariant && finalRenderType === 'drawer' ? _objectSpread(_objectSpread({}, containerProps), {}, {
      open: (_containerProps$open = containerProps.open) !== null && _containerProps$open !== void 0 ? _containerProps$open : true,
      placement: 'bottom',
      height: containerProps.height || '95vh',
      width: '100%',
      closable: false,
      footer: null,
      title: null,
      className: containerProps.className || 'comprehensive-search-modal',
      bodyStyle: _objectSpread({
        padding: '12px 12px calc(12px + env(safe-area-inset-bottom))',
        overflow: 'hidden',
        display: 'flex',
        flexDirection: 'column'
      }, containerProps.bodyStyle || {}),
      onClose: containerProps.onClose || containerProps.onCancel || function () {}
    }) : _objectSpread({
      platform: 'pc',
      open: true,
      cancelVisible: false,
      bodyStyle: {
        padding: '0px 24px 24px',
        overflow: 'hidden',
        display: 'flex',
        flexDirection: 'column'
      },
      width: 680,
      onCancel: function onCancel() {},
      okButtonProps: {},
      onOk: function onOk() {},
      footer: null,
      headerDivider: false,
      className: 'comprehensive-search-modal'
    }, containerProps)
  }, renderContent());
});
ComprehensiveSearch.displayName = 'ComprehensiveSearch';
export default withResponsive(ComprehensiveSearch);