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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useCallback, useMemo, useRef, forwardRef, useState } from 'react';
import { Checkbox, Divider, Select as OriginalSelect, Input } from 'antd';
import { useControllableValue, useMemoizedFn } from 'ahooks';
import classNames from 'classnames';
import Iconfont from "../iconfont";
import { getText } from "../../locales";
import { withWrap } from "../../utils/hoc";
import BrowserSelect from "../browserSelect";
import usePisellConfig from "../pisell-config-provider/hooks/usePisellConfig";
import "./index.less";
var prefix = 'pisell-lowcode-';
var SelectBase = /*#__PURE__*/forwardRef(function (props, ref) {
  var isShowDropdown = props.isShowDropdown,
    selectAll = props.selectAll,
    mode = props.mode,
    options = props.options,
    propsDropdownRender = props.dropdownRender,
    showSearch = props.showSearch,
    styleType = props.styleType,
    _props$optionFilterPr = props.optionFilterProp,
    optionFilterProp = _props$optionFilterPr === void 0 ? 'value' : _props$optionFilterPr,
    popupClassName = props.popupClassName,
    onDropdownVisibleChange = props.onDropdownVisibleChange;
  var _usePisellConfig = usePisellConfig(),
    platform = _usePisellConfig.platform;
  var searchInputRef = useRef(null);
  var _useControllableValue = useControllableValue(props),
    _useControllableValue2 = _slicedToArray(_useControllableValue, 2),
    state = _useControllableValue2[0],
    setState = _useControllableValue2[1];
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    searchValue = _useState2[0],
    setSearchValue = _useState2[1];
  var filteredOptions = useMemo(function () {
    if (!searchValue) return options;
    return (options || []).filter(function (option) {
      var _option$optionFilterP;
      var optionValue = ((_option$optionFilterP = option[optionFilterProp]) === null || _option$optionFilterP === void 0 ? void 0 : _option$optionFilterP.toString().toLowerCase()) || '';
      return optionValue.includes(searchValue.toLowerCase());
    });
  }, [options, searchValue, optionFilterProp]);
  var onSelectAllChange = useMemoizedFn(function (e) {
    if (e.target.checked) {
      setState(filteredOptions.map(function (item) {
        return item.value;
      }), filteredOptions);
    } else {
      setState([], []);
    }
  });
  var handleDropdownVisibleChange = useMemoizedFn(function (visible) {
    if (!visible) {
      setSearchValue('');
    } else {
      if (platform === 'pc') {
        setTimeout(function () {
          var _searchInputRef$curre;
          (_searchInputRef$curre = searchInputRef.current) === null || _searchInputRef$curre === void 0 ? void 0 : _searchInputRef$curre.focus();
        }, 200);
      }
    }
    onDropdownVisibleChange === null || onDropdownVisibleChange === void 0 ? void 0 : onDropdownVisibleChange(visible);
  });
  var isShowSelectAll = useMemo(function () {
    return mode === 'multiple' && isShowDropdown && selectAll;
  }, [mode, selectAll, isShowDropdown]);
  var checkboxChecked = useMemo(function () {
    var _state$sort, _filteredOptions$map, _filteredOptions$map$;
    if (mode !== 'multiple') return false;
    return (state === null || state === void 0 ? void 0 : state.length) === (filteredOptions === null || filteredOptions === void 0 ? void 0 : filteredOptions.length) && (state === null || state === void 0 ? void 0 : (_state$sort = state.sort()) === null || _state$sort === void 0 ? void 0 : _state$sort.join('')) === (filteredOptions === null || filteredOptions === void 0 ? void 0 : (_filteredOptions$map = filteredOptions.map(function (item) {
      return item.value;
    })) === null || _filteredOptions$map === void 0 ? void 0 : (_filteredOptions$map$ = _filteredOptions$map.sort()) === null || _filteredOptions$map$ === void 0 ? void 0 : _filteredOptions$map$.join(''));
  }, [state, filteredOptions]);
  var dropdownRender = useCallback(propsDropdownRender ? propsDropdownRender : function (originNode) {
    return /*#__PURE__*/React.createElement(React.Fragment, null, showSearch && /*#__PURE__*/React.createElement(Input, {
      ref: searchInputRef,
      className: "pisell-select-search-input",
      size: "middle",
      placeholder: getText('select-dropdown-render-search'),
      value: searchValue,
      onChange: function onChange(e) {
        return setSearchValue(e.target.value);
      },
      bordered: false,
      allowClear: true,
      prefix: /*#__PURE__*/React.createElement(Iconfont, {
        style: {
          color: '#98A2B3'
        },
        type: "pisell2-search-lg"
      })
    }), originNode, isShowDropdown && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Divider, {
      className: "".concat(prefix, "select-dropdown-render-divider")
    }), /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefix, "select-dropdown-render-options-wrap")
    }, isShowSelectAll && /*#__PURE__*/React.createElement(Checkbox, {
      onChange: onSelectAllChange,
      checked: checkboxChecked
    }, getText('select-dropdown-render-select-all')))));
  }, [isShowDropdown, isShowSelectAll, checkboxChecked, propsDropdownRender, searchValue, showSearch]);
  if (styleType === 'system') {
    return /*#__PURE__*/React.createElement(BrowserSelect, props);
  }
  return /*#__PURE__*/React.createElement(OriginalSelect, _extends({}, props, {
    defaultActiveFirstOption: false,
    onDropdownVisibleChange: handleDropdownVisibleChange,
    showSearch: false,
    ref: ref,
    value: state,
    onChange: function onChange(value, option) {
      return setState(value, option);
    },
    dropdownRender: dropdownRender,
    options: filteredOptions,
    popupClassName: classNames('pisell-base-select-dropdown', popupClassName)
  }));
});
var Select = withWrap(SelectBase);
Select.Option = OriginalSelect.Option;
export default Select;