"use strict";

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

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

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

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

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

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

var _react = require("react");

var ColumnManager = /*#__PURE__*/function () {
  function ColumnManager(columns, elements) {
    (0, _classCallCheck2["default"])(this, ColumnManager);
    (0, _defineProperty2["default"])(this, "_cached", {});
    this.columns = columns || this.normalize(elements);
  }

  (0, _createClass2["default"])(ColumnManager, [{
    key: "isAnyColumnsFixed",
    value: function isAnyColumnsFixed() {
      var _this = this;

      return this._cache('isAnyColumnsFixed', function () {
        return _this.columns.some(function (column) {
          return !!column.fixed;
        });
      });
    }
  }, {
    key: "isAnyColumnsLeftFixed",
    value: function isAnyColumnsLeftFixed() {
      var _this2 = this;

      return this._cache('isAnyColumnsLeftFixed', function () {
        return _this2.columns.some(function (column) {
          return column.fixed === 'left' || column.fixed === true;
        });
      });
    }
  }, {
    key: "isAnyColumnsRightFixed",
    value: function isAnyColumnsRightFixed() {
      var _this3 = this;

      return this._cache('isAnyColumnsRightFixed', function () {
        return _this3.columns.some(function (column) {
          return column.fixed === 'right';
        });
      });
    }
  }, {
    key: "hasFooter",
    value: function hasFooter() {
      var _this4 = this;

      return this._cache('hasFooter', function () {
        return _this4.leafColumns().some(function (column) {
          return column.footer;
        });
      });
    }
  }, {
    key: "leftColumns",
    value: function leftColumns() {
      var _this5 = this;

      return this._cache('leftColumns', function () {
        return _this5.groupedColumns().filter(function (column) {
          return column.fixed === 'left' || column.fixed === true;
        });
      });
    }
  }, {
    key: "rightColumns",
    value: function rightColumns() {
      var _this6 = this;

      return this._cache('rightColumns', function () {
        return _this6.groupedColumns().filter(function (column) {
          return column.fixed === 'right';
        });
      });
    }
  }, {
    key: "leafColumns",
    value: function leafColumns() {
      var _this7 = this;

      return this._cache('leafColumns', function () {
        return _this7._leafColumns(_this7.columns);
      });
    }
  }, {
    key: "leftLeafColumns",
    value: function leftLeafColumns() {
      var _this8 = this;

      return this._cache('leftLeafColumns', function () {
        return _this8._leafColumns(_this8.leftColumns());
      });
    }
  }, {
    key: "rightLeafColumns",
    value: function rightLeafColumns() {
      var _this9 = this;

      return this._cache('rightLeafColumns', function () {
        return _this9._leafColumns(_this9.rightColumns());
      });
    } // add appropriate rowspan and colspan to column

  }, {
    key: "groupedColumns",
    value: function groupedColumns() {
      var _this10 = this;

      return this._cache('groupedColumns', function () {
        var _groupColumns = function _groupColumns(columns) {
          var currentRow = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
          var parentColumn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
          var rows = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
          // track how many rows we got
          rows[currentRow] = rows[currentRow] || [];
          var grouped = [];

          var setRowSpan = function setRowSpan(column) {
            var rowSpan = rows.length - currentRow;

            if (column && !column.children && // parent columns are supposed to be one row
            rowSpan > 1 && (!column.rowSpan || column.rowSpan < rowSpan)) {
              column.rowSpan = rowSpan;
            }
          };

          columns.forEach(function (column, index) {
            var newColumn = (0, _objectSpread2["default"])({}, column);
            rows[currentRow].push(newColumn);
            parentColumn.colSpan = parentColumn.colSpan || 0;

            if (newColumn.children && newColumn.children.length > 0) {
              newColumn.children = _groupColumns(newColumn.children, currentRow + 1, newColumn, rows);
              parentColumn.colSpan += newColumn.colSpan;
            } else {
              parentColumn.colSpan++;
            } // update rowspan to all same row columns


            for (var i = 0; i < rows[currentRow].length - 1; ++i) {
              setRowSpan(rows[currentRow][i]);
            } // last column, update rowspan immediately


            if (index + 1 === columns.length) {
              setRowSpan(newColumn);
            }

            grouped.push(newColumn);
          });
          return grouped;
        };

        return _groupColumns(_this10.columns);
      });
    }
  }, {
    key: "normalize",
    value: function normalize(elements) {
      var _this11 = this;

      var columns = [];

      _react.Children.forEach(elements, function (element) {
        if (! /*#__PURE__*/(0, _react.isValidElement)(element)) {
          return;
        }

        var column = (0, _objectSpread2["default"])({}, element.props);

        if (element.key) {
          column.key = element.key;
        }

        if (element.type.isTableColumnGroup) {
          column.children = _this11.normalize(column.children);
        }

        columns.push(column);
      });

      return columns;
    }
  }, {
    key: "reset",
    value: function reset(columns, elements) {
      this.columns = columns || this.normalize(elements);
      this._cached = {};
    }
  }, {
    key: "_cache",
    value: function _cache(name, fn) {
      if (name in this._cached) {
        return this._cached[name];
      }

      this._cached[name] = fn();
      return this._cached[name];
    }
  }, {
    key: "_leafColumns",
    value: function _leafColumns(columns) {
      var _this12 = this;

      var leafColumns = [];
      columns.forEach(function (column) {
        if (!column.children) {
          leafColumns.push(column);
        } else {
          leafColumns.push.apply(leafColumns, (0, _toConsumableArray2["default"])(_this12._leafColumns(column.children)));
        }
      });
      return leafColumns;
    }
  }]);
  return ColumnManager;
}();

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