"use strict";

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

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

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

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

var _mobxReactLite = require("mobx-react-lite");

var _omit = _interopRequireDefault(require("lodash/omit"));

var _classnames = _interopRequireDefault(require("classnames"));

var _UnitConvertor = require("../../../lib/_util/UnitConvertor");

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

var _utils = require("./utils");

var _enum = require("./enum");

var _TableCellInner = _interopRequireDefault(require("./TableCellInner"));

var _AggregationTree = require("./AggregationTree");

var TableFooterCell = function TableFooterCell(props) {
  var _classNames;

  var columnGroup = props.columnGroup,
      style = props.style,
      className = props.className,
      colSpan = props.colSpan,
      right = props.right;

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

  var column = columnGroup.column;
  var autoFootHeight = tableStore.autoFootHeight,
      footerRowHeight = tableStore.footerRowHeight,
      aggregation = tableStore.aggregation;
  var footer = column.footer,
      footerClassName = column.footerClassName,
      _column$footerStyle = column.footerStyle,
      footerStyle = _column$footerStyle === void 0 ? {} : _column$footerStyle,
      align = column.align,
      name = column.name,
      command = column.command,
      lock = column.lock;
  var columnLock = (0, _utils.isStickySupport)() && tableStore.overflowX && (0, _utils.getColumnLock)(lock);
  var classString = (0, _classnames["default"])("".concat(prefixCls, "-cell"), (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-cell-fix-").concat(columnLock), columnLock), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-cell-no-transition"), !tableStore.tableColumnResizeTransition), _classNames), className, footerClassName);
  var innerClassNames = ["".concat(prefixCls, "-cell-inner")];
  var innerProps = {};

  if (!autoFootHeight) {
    var $rowHeight = footerRowHeight === undefined ? rowHeight : footerRowHeight;

    if ($rowHeight !== 'auto') {
      innerProps.style = {
        height: (0, _UnitConvertor.pxToRem)($rowHeight)
      };
      innerClassNames.push("".concat(prefixCls, "-cell-inner-row-height-fixed"));
    }
  }

  var cellStyle = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
    textAlign: align || (command ? _enum.ColumnAlign.center : tableStore.getConfig('tableColumnAlign')(column, dataSet.getField(name)))
  }, footerStyle), style);
  var aggregationTree = (0, _react.useMemo)(function () {
    if (aggregation) {
      var $column = columnGroup.column,
          headerGroup = columnGroup.headerGroup;

      if (headerGroup) {
        var tableGroup = columnGroup.tableGroup;

        if (tableGroup) {
          var columnProps = tableGroup.columnProps;
          var totalRecords = headerGroup.totalRecords;

          if (columnProps && totalRecords.length) {
            var children = columnProps.children;

            if (children && children.length) {
              var renderer = function renderer(_ref) {
                var colGroup = _ref.colGroup,
                    style = _ref.style;
                return /*#__PURE__*/_react["default"].createElement(_TableCellInner["default"], {
                  record: totalRecords[0],
                  column: colGroup.column,
                  style: style,
                  inAggregation: true
                });
              };

              return (0, _AggregationTree.groupedAggregationTree)({
                columns: children,
                headerGroup: headerGroup,
                column: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, $column), columnProps),
                renderer: renderer
              });
            }
          }
        }
      }
    }
  }, [columnGroup, aggregation]);

  if (columnLock) {
    if (columnLock === _enum.ColumnLock.left) {
      cellStyle.left = (0, _UnitConvertor.pxToRem)(columnGroup.left, true);
    } else if (columnLock === _enum.ColumnLock.right) {
      cellStyle.right = (0, _UnitConvertor.pxToRem)(colSpan && colSpan > 1 ? right : columnGroup.right + right, true);
    }
  }

  var getFooter = function getFooter() {
    switch ((0, _typeof2["default"])(footer)) {
      case 'function':
        {
          var footerHookOptions = {
            dataSet: dataSet,
            name: name,
            aggregationTree: aggregationTree
          };

          try {
            return footer(footerHookOptions);
          } catch (e) {
            return footer(dataSet, name);
          }
        }

      case 'string':
        return /*#__PURE__*/_react["default"].createElement("span", null, footer);

      default:
        return footer;
    }
  };

  return /*#__PURE__*/_react["default"].createElement("th", {
    className: classString,
    style: (0, _omit["default"])(cellStyle, ['width', 'height']),
    colSpan: colSpan,
    scope: "col"
  }, /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, innerProps, {
    className: innerClassNames.join(' ')
  }), getFooter()));
};

TableFooterCell.displayName = 'TableFooterCell';

var _default = (0, _mobxReactLite.observer)(TableFooterCell);

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