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 { IconFont } from '@zgfe/business-lib';
import { Dropdown, Menu } from 'antd';
import _ from 'lodash';
import React, { useEffect, useImperativeHandle, useState } from 'react';
import { ColumnCell } from './compareTable';
var classPrefix = 'flow-group-config';
var GroupSelector = function GroupSelector(_ref, ref) {
  var columnList = _ref.columnList,
    placehover = _ref.placehover,
    disabled = _ref.disabled,
    defaultValue = _ref.defaultValue,
    onChange = _ref.onChange;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    selected = _useState2[0],
    setSelected = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    inited = _useState4[0],
    setInited = _useState4[1];
  useImperativeHandle(ref, function () {
    return {
      unselect: function unselect(column) {
        setSelected(function (list) {
          var res = _.cloneDeep(list);
          list.forEach(function (name, i) {
            if (column.name === name) res.splice(i, 1);
          });
          return res;
        });
      }
    };
  });
  useEffect(function () {
    setInited(false);
    if (defaultValue) setSelected(defaultValue.map(function (column) {
      return column === null || column === void 0 ? void 0 : column.name;
    }));
    setInited(true);
  }, []);
  useEffect(function () {
    if (!onChange || !inited) return;
    var map = {};
    columnList.forEach(function (column) {
      return map[column.name] = column;
    });
    onChange(selected.map(function (name) {
      return map[name];
    }).filter(function (item) {
      return Boolean(item);
    }));
  }, [selected]);
  function onSelect(item) {
    setSelected(function (list) {
      var res = _.cloneDeep(list);
      if (res.includes(item.key)) {
        res.splice(res.findIndex(function (name) {
          return item.key === name;
        }), 1);
      } else {
        res.push(item.key);
      }
      return res;
    });
  }
  return /*#__PURE__*/React.createElement(Dropdown, {
    disabled: disabled,
    overlay: /*#__PURE__*/React.createElement(Menu, {
      className: "".concat(classPrefix, "-menu"),
      selectedKeys: selected,
      items: columnList.map(function (column) {
        return {
          key: column.name,
          label: /*#__PURE__*/React.createElement(ColumnCell, {
            column: column
          })
        };
      }),
      onClick: onSelect
    }),
    trigger: ['click']
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-list-item btn-add ").concat(disabled ? 'disabled' : '')
  }, placehover || '添加分组', /*#__PURE__*/React.createElement(IconFont, {
    type: "tianjia1"
  })));
};
export default /*#__PURE__*/React.forwardRef(GroupSelector);