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; }
import { Select, Space } from 'antd';
import Icon from "../Icon";
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import CombineContent from "./components/Content";
import CombineTags from "./components/Tags";
import { useCombineBoxStyles } from "./style";
import { debounce } from 'lodash';
var CombineSearch = function CombineSearch(_ref) {
  var _ref$fieldNames = _ref.fieldNames,
    fieldNames = _ref$fieldNames === void 0 ? {
      label: 'title',
      value: 'key'
    } : _ref$fieldNames,
    options = _ref.options,
    _ref$search = _ref.search,
    search = _ref$search === void 0 ? true : _ref$search,
    _ref$placeholder = _ref.placeholder,
    placeholder = _ref$placeholder === void 0 ? '请选择筛选条件' : _ref$placeholder,
    prefixIcon = _ref.prefixIcon,
    border = _ref.border,
    addonBefore = _ref.addonBefore,
    addonAfter = _ref.addonAfter,
    request = _ref.request,
    values = _ref.values,
    setValues = _ref.setValues,
    defaultSelectKey = _ref.defaultSelectKey,
    _ref$defaultAfterChan = _ref.defaultAfterChange,
    defaultAfterChange = _ref$defaultAfterChan === void 0 ? false : _ref$defaultAfterChan;
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    current = _useState2[0],
    setCurrent = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    focus = _useState4[0],
    setFocus = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    canSelectOptions = _useState6[0],
    setCanSelectOptions = _useState6[1];
  var _useCombineBoxStyles = useCombineBoxStyles({
      focus: focus,
      border: !!border
    }),
    styles = _useCombineBoxStyles.styles;
  var _useState7 = useState(true),
    _useState8 = _slicedToArray(_useState7, 2),
    initFlag = _useState8[0],
    setInitFlag = _useState8[1];
  var currentOption = useMemo(function () {
    var findOption = canSelectOptions.find(function (i) {
      return i[fieldNames.value] === current;
    });
    return findOption;
  }, [current, canSelectOptions]);
  useEffect(function () {
    var resetOptions = options.filter(function (i) {
      return !!i.componentType;
    });
    if (defaultSelectKey) {
      var hasDefault = resetOptions.find(function (i) {
        return i[fieldNames.value] === defaultSelectKey;
      });
      if (hasDefault) {
        setCurrent(defaultSelectKey);
      }
    }
    setCanSelectOptions(resetOptions);
  }, [options, defaultSelectKey]);
  var getInitCallBack = useCallback(debounce(function (values) {
    var length = !!Object.keys(values).length;
    if (initFlag && request) {
      setInitFlag(false);
      request(values, current, length ? '$initValue' : '$init');
    }
  }, 200), [JSON.stringify(values), setValues]);
  var getCallBack = useCallback(debounce(function (value, type) {
    if (request && !initFlag) {
      request(values, current, '$value');
    }
  }, 200), [initFlag, current, JSON.stringify(values)]);
  useEffect(function () {
    getCallBack(values, '$value');
  }, [JSON.stringify(values)]);
  useEffect(function () {
    getInitCallBack(values);
  }, [JSON.stringify(values)]);
  var onChangeCallBack = useCallback(function (cb) {
    if (cb) {
      cb();
    }
    setTimeout(function () {
      if (defaultAfterChange) {
        return;
      }
      setCurrent('');
    }, 100);
  }, [defaultAfterChange]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: styles.box
  }, /*#__PURE__*/React.createElement("div", {
    className: styles.left
  }, addonBefore, search ? /*#__PURE__*/React.createElement(Space.Compact, {
    className: styles.card,
    onBlur: function onBlur() {
      setFocus(false);
    },
    onFocus: function onFocus() {
      setFocus(true);
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "icon-span"
  }, prefixIcon || /*#__PURE__*/React.createElement(Icon, {
    name: "v35_Search"
  })), /*#__PURE__*/React.createElement(Select, {
    placeholder: placeholder,
    filterOption: function filterOption(input, option) {
      var _option$fieldNames$la;
      return ((_option$fieldNames$la = option === null || option === void 0 ? void 0 : option[fieldNames.label]) !== null && _option$fieldNames$la !== void 0 ? _option$fieldNames$la : '').toLowerCase().includes(input.toLowerCase());
    },
    popupMatchSelectWidth: 150,
    options: canSelectOptions,
    labelRender: function labelRender(props) {
      return /*#__PURE__*/React.createElement(Space, {
        align: "center"
      }, props.label + '  :');
    },
    className: "select-no-padding",
    fieldNames: fieldNames,
    value: current || null,
    onSelect: function onSelect(value) {
      setCurrent(value);
    },
    style: {
      width: current ? '' : '100%',
      zIndex: 100
    },
    suffixIcon: null,
    showSearch: !current,
    variant: 'borderless'
  }), current && /*#__PURE__*/React.createElement(CombineContent, {
    setCombineValues: setValues,
    fieldNames: fieldNames,
    combineValues: values,
    onChangeState: onChangeCallBack,
    current: currentOption
  })) : null), /*#__PURE__*/React.createElement("div", {
    className: styles.right
  }, addonAfter)), search ? /*#__PURE__*/React.createElement(CombineTags, {
    setCurrent: setCurrent,
    current: current,
    options: canSelectOptions,
    fieldNames: fieldNames,
    combineValues: values,
    setCombineValues: setValues
  }) : /*#__PURE__*/React.createElement("div", {
    style: {
      margin: '8px 0'
    }
  }));
};
export default CombineSearch;