import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import React, { useCallback, useMemo, useState, memo } from 'react';
import Icon from '../../../../es/icon';
import TextField from '../../text-field';
import CheckBox from '../../check-box';
import { $l } from '../../locale-context';

var FieldList = function FieldList(_ref) {
  var value = _ref.value,
      onSelect = _ref.onSelect,
      onUnSelect = _ref.onUnSelect,
      groups = _ref.groups,
      prefixCls = _ref.prefixCls;

  var _useState = useState(''),
      _useState2 = _slicedToArray(_useState, 2),
      searchText = _useState2[0],
      setSearchText = _useState2[1];

  var codes = useMemo(function () {
    return groups.reduce(function (res, current) {
      return [].concat(_toConsumableArray(res), _toConsumableArray(current.fields.map(function (o) {
        var hasBindProps = function hasBindProps(propsName) {
          return o && o.get(propsName) && o.get(propsName).bind;
        };

        if (!o.get('bind') && !hasBindProps('computedProps') && !hasBindProps('dynamicProps')) {
          return o.get('name');
        }

        return undefined;
      })));
    }, []);
  }, [groups]);
  var labelCodes = useMemo(function () {
    return groups.reduce(function (res, current) {
      return [].concat(_toConsumableArray(res), _toConsumableArray(current.fields.map(function (o) {
        var hasBindProps = function hasBindProps(propsName) {
          return o && o.get(propsName) && o.get(propsName).bind;
        };

        if (!o.get('bind') && !hasBindProps('computedProps') && !hasBindProps('dynamicProps')) {
          return [o.get('name'), o.get('label')];
        }

        return undefined;
      })));
    }, []);
  }, [groups]);
  var hasSelect = useMemo(function () {
    var isSelect = labelCodes.some(function (lc) {
      if (lc && (lc[1] && lc[1].includes(searchText || '') || lc[1] === undefined)) {
        return value.includes(lc[0]);
      }

      return false;
    });
    return isSelect;
  }, [value.length, searchText]);
  var hasSelectAll = useMemo(function () {
    var isAll = labelCodes.some(function (lc) {
      if (lc && (lc[1] && lc[1].includes(searchText || '') || lc[1] === undefined)) {
        return !value.includes(lc[0]);
      }

      return false;
    });
    return !isAll;
  }, [searchText, value.length]);
  var isChecked = useCallback(function (code) {
    return value.includes(code);
  }, [value]);
  var handleChange = useCallback(function (code, select) {
    if (select) {
      onSelect(code);
    } else {
      onUnSelect(code);
    }
  }, [onSelect, onUnSelect]);
  var renderGroup = useCallback(function (group) {
    return group.fields.length > 0 && /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-section"),
      key: group.title
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-list")
    }, group.fields.map(function (field) {
      var code = field.get('name');
      var label = field.get('label') || code;
      var checked = isChecked(code);

      var hasBindProps = function hasBindProps(propsName) {
        return field && field.get(propsName) && field.get(propsName).bind;
      };

      if (label && label.includes(searchText || '') && !field.get('bind') && !hasBindProps('computedProps') && !hasBindProps('dynamicProps')) {
        return /*#__PURE__*/React.createElement("div", {
          className: "".concat(prefixCls, "-item"),
          key: code
        }, /*#__PURE__*/React.createElement(CheckBox, {
          value: code,
          checked: checked,
          onChange: function onChange() {
            return handleChange(code, !checked);
          }
        }, label));
      }

      return null;
    })));
  }, [handleChange, isChecked, searchText]);
  var selectItems = value.filter(function (v) {
    return codes.includes(v);
  });
  return /*#__PURE__*/React.createElement("div", {
    className: prefixCls
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-search")
  }, /*#__PURE__*/React.createElement(TextField, {
    style: {
      flex: 1
    },
    value: searchText,
    onClick: function onClick(e) {
      e.preventDefault();
      e.currentTarget.focus();
    },
    onChange: function onChange(v) {
      setSearchText(v);
    },
    onInput: function onInput(e) {
      setSearchText(e.target.value);
    },
    prefix: /*#__PURE__*/React.createElement(Icon, {
      type: "search"
    }),
    placeholder: $l('Table', 'enter_text_filter'),
    clearButton: true
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-header")
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-search-selected")
  }, $l('Screening', 'selected'), /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-search-items")
  }, selectItems.length), $l('Transfer', 'items')), /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-search-divide")
  }), /*#__PURE__*/React.createElement("span", {
    style: {
      display: !hasSelectAll ? 'inline-block' : 'none'
    },
    className: "".concat(prefixCls, "-search-action"),
    onClick: function onClick() {
      var values = labelCodes.map(function (lc) {
        if (lc && (lc[1] && lc[1].includes(searchText || '') || lc[1] === undefined)) {
          return lc[0];
        }

        return undefined;
      }).filter(function (v) {
        return !!v;
      });
      handleChange(values, true);
    }
  }, $l('Select', 'select_all')), /*#__PURE__*/React.createElement("span", {
    style: {
      display: hasSelect ? 'inline-block' : 'none'
    },
    className: "".concat(prefixCls, "-search-action"),
    onClick: function onClick() {
      var values = labelCodes.map(function (lc) {
        if (lc && (lc[1] && lc[1].includes(searchText || '') || lc[1] === undefined)) {
          return lc[0];
        }

        return undefined;
      }).filter(function (v) {
        return !!v;
      });
      handleChange(values, false);
    }
  }, $l('Table', 'clear_filter'))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content")
  }, groups.map(function (group) {
    return renderGroup(group);
  })));
};

FieldList.displayName = 'FieldList';
export default /*#__PURE__*/memo(FieldList);
//# sourceMappingURL=FieldList.js.map
