"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _menuButton = _interopRequireDefault(require("@alifd/next/lib/menu-button"));
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _actionTitleRender = _interopRequireDefault(require("../utils/render/actionTitleRender"));
var _runColumnActionCallback = _interopRequireDefault(require("../utils/runColumnActionCallback"));
var _filterActionColumn = _interopRequireDefault(require("../utils/filterActionColumn"));
var _dispatchResizeEvent = _interopRequireDefault(require("../utils/dispatchResizeEvent"));
var Item = _menuButton["default"].Item;
var WebNextTableActionCell = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(WebNextTableActionCell, _React$Component);
  function WebNextTableActionCell() {
    return _React$Component.apply(this, arguments) || this;
  }
  var _proto = WebNextTableActionCell.prototype;
  _proto.refreshCellUI = function refreshCellUI() {
    this.forceUpdate(function () {
      (0, _dispatchResizeEvent["default"])();
    });
  };
  _proto.componentDidMount = function componentDidMount() {
    setTimeout(function () {
      (0, _dispatchResizeEvent["default"])();
    }, 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 = (0, _filterActionColumn["default"])(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 = (0, _actionTitleRender["default"])(action, rowData);
      return render ? /*#__PURE__*/_react["default"].createElement(Item, {
        key: action.title
      }, render) : null;
    }).filter(function (i) {
      return i !== null;
    });
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: nextTablePrefix + "action-cell"
    }, actionColumnToRender.map(function (action) {
      var render = (0, _actionTitleRender["default"])(action, rowData);
      return render ? /*#__PURE__*/_react["default"].createElement(_button["default"], {
        key: action.title,
        className: (0, _classnames["default"])(isLink ? nextTablePrefix + "action-link" : nextTablePrefix + "action-button"),
        text: isLink,
        type: isLink ? 'primary' : 'secondary',
        onClick: function onClick(e) {
          e.stopPropagation();
          (0, _runColumnActionCallback["default"])({
            index: index,
            rowData: rowData,
            action: action
          })["finally"](function () {
            _this.refreshCellUI();
          });
        }
      }, render) : null;
    }), showMoreAction && menu.length ? /*#__PURE__*/_react["default"].createElement(_menuButton["default"], {
      text: isLink,
      className: (0, _classnames["default"])(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;
        });
        (0, _runColumnActionCallback["default"])({
          index: index,
          rowData: rowData,
          action: action
        })["finally"](function () {
          _this.refreshCellUI();
        });
      },
      label: locale.more
    }, menu) : null);
  };
  return WebNextTableActionCell;
}(_react["default"].Component);
exports["default"] = WebNextTableActionCell;