function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; }
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);