"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _react = _interopRequireWildcard(require("react"));

var _icon = _interopRequireDefault(require("../../../../lib/icon"));

var _textField = _interopRequireDefault(require("../../text-field"));

var _checkBox = _interopRequireDefault(require("../../check-box"));

var _localeContext = require("../../locale-context");

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

  var _useState = (0, _react.useState)(''),
      _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
      searchText = _useState2[0],
      setSearchText = _useState2[1];

  var labelCodes = (0, _react.useMemo)(function () {
    return groups.reduce(function (res, current) {
      return [].concat((0, _toConsumableArray2["default"])(res), (0, _toConsumableArray2["default"])(current.fields.map(function (o) {
        return [o.get('name'), o.get('label')];
      })));
    }, []);
  }, [groups]);
  var isChecked = (0, _react.useCallback)(function (code) {
    return value.includes(code);
  }, [value]);
  var handleChange = (0, _react.useCallback)(function (code, select) {
    if (select) {
      onSelect(code);
    } else {
      onUnSelect(code);
    }
  }, [onSelect, onUnSelect]);
  var renderGroup = (0, _react.useCallback)(function (group) {
    return group.fields.length > 0 && /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefixCls, "-section"),
      key: group.title
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(prefixCls, "-list")
    }, group.fields.map(function (field) {
      var code = field.get('name');
      var label = field.get('label');
      var checked = isChecked(code);

      if (label && label.includes(searchText || '')) {
        return /*#__PURE__*/_react["default"].createElement("div", {
          className: "".concat(prefixCls, "-item"),
          key: code
        }, /*#__PURE__*/_react["default"].createElement(_checkBox["default"], {
          value: code,
          disabled: field.get('disabled'),
          checked: checked,
          onChange: function onChange() {
            return handleChange(code, !checked);
          }
        }, label));
      }

      return null;
    })));
  }, [handleChange, isChecked, searchText]);

  var chooseRe = function chooseRe() {
    var allValue = labelCodes.map(function (lc) {
      if (lc[1] && lc[1].includes(searchText || '')) {
        return lc[0];
      }

      return undefined;
    }).filter(function (v) {
      return !!v;
    });
    var selected = value.slice(fieldsLimit);
    var reSelectedOptions = allValue.filter(function (i) {
      var optionIsSelect = selected.some(function (v) {
        return v === i;
      });
      return !optionIsSelect;
    });
    handleChange(selected, false);
    handleChange(reSelectedOptions, true);
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: prefixCls
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefixCls, "-search")
  }, /*#__PURE__*/_react["default"].createElement(_textField["default"], {
    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["default"].createElement(_icon["default"], {
      type: "search"
    }),
    placeholder: (0, _localeContext.$l)('Table', 'enter_search_filter'),
    clearButton: true
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(prefixCls, "-header")
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefixCls, "-search-action"),
    onClick: function onClick() {
      var values = labelCodes.map(function (lc) {
        if (lc[1] && lc[1].includes(searchText || '')) {
          return lc[0];
        }

        return undefined;
      }).filter(function (v) {
        return !!v;
      });
      handleChange(values, true);
    }
  }, (0, _localeContext.$l)('Select', 'select_all')), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefixCls, "-search-action"),
    onClick: chooseRe
  }, (0, _localeContext.$l)('Select', 'select_re')), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefixCls, "-search-action"),
    onClick: function onClick() {
      var values = labelCodes.map(function (lc) {
        if (lc[1] && lc[1].includes(searchText || '')) {
          return lc[0];
        }

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

ComboFieldList.displayName = 'ComboFieldList';

var _default = /*#__PURE__*/(0, _react.memo)(ComboFieldList);

exports["default"] = _default;
//# sourceMappingURL=ComboFieldList.js.map
