import _Button from "@alifd/next/es/button";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _MenuButton from "@alifd/next/es/menu-button";
import React from "react";
import classnames from 'classnames';
import actionTitleRender from "../utils/render/actionTitleRender";
import runColumnActionCallback from "../utils/runColumnActionCallback";
import filterActionColumn from "../utils/filterActionColumn";
import dispatchResizeEvent from "../utils/dispatchResizeEvent";
var Item = _MenuButton.Item;
var WebNextTableActionCell = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(WebNextTableActionCell, _React$Component);
  function WebNextTableActionCell() {
    return _React$Component.apply(this, arguments) || this;
  }
  var _proto = WebNextTableActionCell.prototype;
  _proto.refreshCellUI = function refreshCellUI() {
    this.forceUpdate(function () {
      dispatchResizeEvent();
    });
  };
  _proto.componentDidMount = function componentDidMount() {
    setTimeout(function () {
      dispatchResizeEvent();
    }, 400);
  };
  _proto.render = function render() {
    var _this = this;
    var _this$props = this.props,
      actionColumn = _this$props.actionColumn,
      actionType = _this$props.actionType,
      _this$props$maxWebSho = _this$props.maxWebShownActionCount,
      maxWebShownActionCount = _this$props$maxWebSho === void 0 ? 3 : _this$props$maxWebSho,
      nextTablePrefix = _this$props.nextTablePrefix,
      rowData = _this$props.rowData,
      device = _this$props.device,
      index = _this$props.index,
      _this$props$locale = _this$props.locale,
      locale = _this$props$locale === void 0 ? {} : _this$props$locale;
    var currentActionColumn = filterActionColumn(actionColumn, rowData, device);
    var showMoreAction = currentActionColumn.length > maxWebShownActionCount;
    var actionColumnToRender = currentActionColumn.slice(0, showMoreAction ? maxWebShownActionCount - 1 : undefined);
    var remainActions = currentActionColumn.slice(maxWebShownActionCount - 1);
    var isLink = actionType === 'link';
    var menu = remainActions.map(function (action) {
      var render = actionTitleRender(action, rowData);
      return render ? /*#__PURE__*/React.createElement(Item, {
        key: action.title
      }, render) : null;
    }).filter(function (i) {
      return i !== null;
    });
    return /*#__PURE__*/React.createElement("div", {
      className: nextTablePrefix + "action-cell"
    }, actionColumnToRender.map(function (action) {
      var render = actionTitleRender(action, rowData);
      return render ? /*#__PURE__*/React.createElement(_Button, {
        key: action.title,
        className: classnames(isLink ? nextTablePrefix + "action-link" : nextTablePrefix + "action-button"),
        text: isLink,
        type: isLink ? 'primary' : 'secondary',
        onClick: function onClick(e) {
          e.stopPropagation();
          runColumnActionCallback({
            index: index,
            rowData: rowData,
            action: action
          })["finally"](function () {
            _this.refreshCellUI();
          });
        }
      }, render) : null;
    }), showMoreAction && menu.length ? /*#__PURE__*/React.createElement(_MenuButton, {
      text: isLink,
      className: classnames(nextTablePrefix + "action-more", isLink ? nextTablePrefix + "action-link" : nextTablePrefix + "action-button"),
      type: isLink ? 'primary' : 'secondary',
      popupTriggerType: "hover",
      autoWidth: false,
      onItemClick: function onItemClick(i, item) {
        var title = item.props.title;
        var action = remainActions.find(function (item) {
          return item.title = title;
        });
        runColumnActionCallback({
          index: index,
          rowData: rowData,
          action: action
        })["finally"](function () {
          _this.refreshCellUI();
        });
      },
      label: locale.more
    }, menu) : null);
  };
  return WebNextTableActionCell;
}(React.Component);
export { WebNextTableActionCell as default };