var _excluded = ["onRowClick", "resultProps", "onAddClient", "hasMore", "onLoadMore", "state", "resultLoading"];
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 _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, useRef, useState } from 'react';
// @ts-ignore
import { locales } from '@pisell/utils';
import ComprehensiveSearch from "../../pro/comprehensiveSearch";
import AddClient from "./Add";
import ResultList from "./ResultList";
import localeTexts from "./locales";
import useEngineContext from "../../hooks/useEngineContext";
import useTranslationOriginal from "../../hooks/useTranslationOriginal";
import "./index.less";
var ClientSearch = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _context$appHelper, _state$bookingConfig, _context$appHelper2;
  var onRowClick = _ref.onRowClick,
    resultProps = _ref.resultProps,
    onAddClient = _ref.onAddClient,
    hasMore = _ref.hasMore,
    onLoadMore = _ref.onLoadMore,
    state = _ref.state,
    resultLoading = _ref.resultLoading,
    props = _objectWithoutProperties(_ref, _excluded);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    modalVisible = _useState2[0],
    setModalVisible = _useState2[1];
  var comprehensiveSearchRef = useRef(null);
  var addClientRef = useRef(null);
  var resultListRef = useRef(null);
  var translationOriginal = useTranslationOriginal();
  var context = useEngineContext();
  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 appointmentSubject = (state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.basic) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.appointment_subject) || {};
  var translation = context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.translationShop;
  var handleAdd = function handleAdd() {
    setModalVisible(true);
  };
  var handleModalCancel = function handleModalCancel() {
    var _addClientRef$current;
    setModalVisible(false);
    (_addClientRef$current = addClientRef.current) === null || _addClientRef$current === void 0 || _addClientRef$current.reset();
  };

  // 自定义结果展示组件
  var renderResults = function renderResults(searchResults) {
    return /*#__PURE__*/React.createElement(ResultList, {
      ref: resultListRef,
      dataSource: searchResults,
      hasMore: hasMore,
      loading: resultLoading,
      holderTitle: (translation === null || translation === void 0 ? void 0 : translation(appointmentSubject === null || appointmentSubject === void 0 ? void 0 : appointmentSubject.form_name)) || locales.getText('pisell2.text.client-search.holder'),
      onLoadMore: onLoadMore,
      onRowClick: onRowClick,
      translateText: translationOriginal
    });
  };
  var handleAddClient = function handleAddClient(record, isChecked) {
    onAddClient === null || onAddClient === void 0 || onAddClient(record, isChecked);
    handleModalCancel();
  };
  useImperativeHandle(ref, function () {
    return {
      resetScrollPosition: function resetScrollPosition() {
        var _resultListRef$curren;
        (_resultListRef$curren = resultListRef.current) === null || _resultListRef$curren === void 0 || _resultListRef$curren.resetScrollPosition();
      }
    };
  });
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ComprehensiveSearch, _extends({}, props, {
    ref: comprehensiveSearchRef,
    renderType: "modal",
    onAdd: handleAdd,
    resultDisplay: renderResults
  })), /*#__PURE__*/React.createElement(AddClient, {
    containerProps: {
      className: 'pisell-client-search__new-client-modal',
      title: locales.getText('pisell2.text.create-new-client'),
      open: modalVisible,
      onCancel: handleModalCancel,
      width: 680
    },
    onChange: handleAddClient,
    ref: addClientRef
  }));
});
export default ClientSearch;