"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _mobx = require("mobx");

var _TableContext = _interopRequireDefault(require("./TableContext"));

var _Dropdown = _interopRequireDefault(require("../dropdown/Dropdown"));

var _menu = _interopRequireDefault(require("../menu"));

var _localeContext = require("../locale-context");

var _EventManager = require("../_util/EventManager");

var _TableStore = require("./TableStore");

var AggregationButton = function AggregationButton(props) {
  var expanded = props.expanded,
      record = props.record,
      rowGroup = props.rowGroup,
      headerGroup = props.headerGroup,
      aggregationExpandKey = props.aggregationExpandKey,
      isColumnGroup = props.isColumnGroup;

  var _useContext = (0, _react.useContext)(_TableContext["default"]),
      tableStore = _useContext.tableStore,
      prefixCls = _useContext.prefixCls;

  var handleMenuClick = (0, _react.useCallback)((0, _mobx.action)(function (_ref) {
    var key = _ref.key;

    switch (key) {
      case 'cell':
        {
          if (record) {
            tableStore.setAggregationCellExpanded(record, aggregationExpandKey, !expanded);
          } else if (headerGroup) {
            headerGroup.setState(_TableStore.EXPAND_KEY, !expanded);
          }

          break;
        }

      case 'row':
        {
          if (rowGroup && (headerGroup || isColumnGroup)) {
            tableStore.columnGroups.allLeafs.forEach(function (_ref2) {
              var col = _ref2.column,
                  columnKey = _ref2.key;

              if (col.aggregation) {
                rowGroup.totalRecords.forEach(function (r) {
                  tableStore.setAggregationCellExpanded(r, columnKey, !expanded);
                });
              }
            });
          } else if (headerGroup) {
            tableStore.columnGroups.allLeafs.forEach(function (_ref3) {
              var $headerGroup = _ref3.headerGroup;

              if ($headerGroup) {
                $headerGroup.setState(_TableStore.EXPAND_KEY, !expanded);
              }
            });
          } else if (record) {
            tableStore.columnGroups.allLeafs.forEach(function (_ref4) {
              var col = _ref4.column,
                  columnKey = _ref4.key;
              return col.aggregation && tableStore.setAggregationCellExpanded(record, columnKey, !expanded);
            });
          }

          break;
        }

      case 'column':
        {
          if (record) {
            record.dataSet.forEach(function (r) {
              return tableStore.setAggregationCellExpanded(r, aggregationExpandKey, !expanded);
            });

            if (headerGroup) {
              headerGroup.setState(_TableStore.EXPAND_KEY, !expanded);
            }
          } else if (headerGroup) {
            headerGroup.totalRecords.forEach(function (r) {
              return tableStore.setAggregationCellExpanded(r, aggregationExpandKey, !expanded);
            });
            headerGroup.setState(_TableStore.EXPAND_KEY, !expanded);
          }

          break;
        }

      default:
    }
  }), [tableStore, record, aggregationExpandKey, expanded, headerGroup, rowGroup]);
  var handleClick = (0, _react.useCallback)(function () {
    handleMenuClick({
      key: tableStore.aggregationExpandType
    });
  }, [handleMenuClick, tableStore]);
  var getOverlay = (0, _react.useCallback)(function () {
    return /*#__PURE__*/_react["default"].createElement(_menu["default"], {
      prefixCls: "".concat(prefixCls, "-dropdown-menu"),
      onClick: handleMenuClick
    }, /*#__PURE__*/_react["default"].createElement(_menu["default"].Item, {
      key: "cell"
    }, (0, _localeContext.$l)('Table', expanded ? 'collapse_cell' : 'expand_cell')), /*#__PURE__*/_react["default"].createElement(_menu["default"].Item, {
      key: "row"
    }, (0, _localeContext.$l)('Table', expanded ? 'collapse_row' : 'expand_row')), /*#__PURE__*/_react["default"].createElement(_menu["default"].Item, {
      key: "column"
    }, (0, _localeContext.$l)('Table', expanded ? 'collapse_column' : 'expand_column')));
  }, [prefixCls, handleMenuClick, expanded]);
  return /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefixCls, "-cell-expand"),
    onClick: _EventManager.stopEvent
  }, /*#__PURE__*/_react["default"].createElement("button", {
    type: "button",
    className: "".concat(prefixCls, "-cell-expand-btn"),
    onClick: handleClick
  }, (0, _localeContext.$l)('Table', expanded ? 'collapse' : 'expand_button')), /*#__PURE__*/_react["default"].createElement(_Dropdown["default"], {
    overlay: getOverlay
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(prefixCls, "-cell-expand-menu-btn")
  })));
};

AggregationButton.displayName = 'AggregationButton';

var _default = /*#__PURE__*/(0, _react.memo)(AggregationButton);

exports["default"] = _default;
//# sourceMappingURL=AggregationButton.js.map
