var _excluded = ["disabled", "children", "onClick", "style"],
  _excluded2 = ["onClick", "disabled", "children"],
  _excluded3 = ["onClick", "disabled", "style", "children"],
  _excluded4 = ["onClick", "disabled", "children"];
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(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(arr) { if (Array.isArray(arr)) return arr; }
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import { MoreOutlined } from '@ant-design/icons';
import { Divider, Dropdown, Space } from 'antd';
import React, { useEffect, useState } from 'react';
import "./index.less";
var LinkButton = function LinkButton(_ref) {
  var _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    linkChildren = _ref.children,
    onClick = _ref.onClick,
    style = _ref.style,
    args = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/React.createElement("span", _extends({
    style: style,
    className: disabled ? 'displayed-name' : 'name-link',
    onClick: disabled ? undefined : onClick
  }, args), linkChildren);
};
var Actions = function Actions(_ref2) {
  var children = _ref2.children,
    _ref2$threshold = _ref2.threshold,
    threshold = _ref2$threshold === void 0 ? 3 : _ref2$threshold;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    list = _useState2[0],
    setList = _useState2[1];
  useEffect(function () {
    if (!children) {
      setList([]);
      return;
    }
    if (Array.isArray(children)) {
      var lt = children.filter(function (item) {
        return item !== null && typeof item !== 'boolean' && /*#__PURE__*/React.isValidElement(item);
      });
      setList(lt);
    } else if ( /*#__PURE__*/React.isValidElement(children) && children.props.children) {
      setList([children]);
    } else {
      setList([]);
    }
  }, [children]);
  if (list.length <= threshold) {
    return /*#__PURE__*/React.createElement(Space, {
      align: "center",
      style: {
        gap: 0
      },
      split: /*#__PURE__*/React.createElement(Divider, {
        type: "vertical"
      })
    }, list.map(function (item, index) {
      var _item$props = item.props,
        onClick = _item$props.onClick,
        _item$props$disabled = _item$props.disabled,
        disabled = _item$props$disabled === void 0 ? false : _item$props$disabled,
        linkChildren = _item$props.children,
        rest = _objectWithoutProperties(_item$props, _excluded2);
      return /*#__PURE__*/React.createElement(LinkButton, _extends({
        key: index,
        onClick: onClick,
        disabled: disabled
      }, rest), linkChildren);
    }));
  }
  var l1 = list.slice(0, threshold);
  var l2 = list.slice(threshold);
  var items = l2.map(function (item, index) {
    var _item$props2 = item.props,
      onClick = _item$props2.onClick,
      _item$props2$disabled = _item$props2.disabled,
      disabled = _item$props2$disabled === void 0 ? false : _item$props2$disabled,
      style = _item$props2.style,
      labelChildren = _item$props2.children,
      rest = _objectWithoutProperties(_item$props2, _excluded3);
    void onClick;
    return {
      key: String(index),
      disabled: disabled,
      label: /*#__PURE__*/React.createElement("span", _extends({
        className: disabled ? 'displayed-name' : '',
        style: style
      }, rest), labelChildren)
    };
  });
  var onMenuClick = function onMenuClick(_ref3) {
    var key = _ref3.key,
      domEvent = _ref3.domEvent;
    domEvent === null || domEvent === void 0 || domEvent.stopPropagation();
    var idx = Number(key);
    var item = l2[idx];
    if (item && !item.props.disabled) {
      var _item$props$onClick, _item$props3;
      (_item$props$onClick = (_item$props3 = item.props).onClick) === null || _item$props$onClick === void 0 || _item$props$onClick.call(_item$props3);
    }
  };
  return /*#__PURE__*/React.createElement(Space, {
    align: "center",
    style: {
      gap: 0
    },
    split: /*#__PURE__*/React.createElement(Divider, {
      type: "vertical"
    })
  }, l1.map(function (item, index) {
    var _item$props4 = item.props,
      onClick = _item$props4.onClick,
      _item$props4$disabled = _item$props4.disabled,
      disabled = _item$props4$disabled === void 0 ? false : _item$props4$disabled,
      linkChildren = _item$props4.children,
      rest = _objectWithoutProperties(_item$props4, _excluded4);
    return /*#__PURE__*/React.createElement(LinkButton, _extends({
      key: index,
      onClick: onClick,
      disabled: disabled
    }, rest), linkChildren);
  }), /*#__PURE__*/React.createElement(Dropdown, {
    trigger: ['click'],
    menu: {
      items: items,
      onClick: onMenuClick
    }
  }, /*#__PURE__*/React.createElement(MoreOutlined, {
    style: {
      fontSize: 12,
      cursor: 'pointer'
    }
  })));
};
export { Actions, LinkButton };