import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import { __decorate } from "tslib";
import { computed } from 'mobx';
import { columnWidth } from './Column';
import ColumnGroup from './ColumnGroup';
import { ColumnLock } from './enum';

var ColumnGroups = /*#__PURE__*/function () {
  function ColumnGroups(columns, store, parent) {
    var _this = this;

    _classCallCheck(this, ColumnGroups);

    this.store = store;
    this.parent = parent;
    var prev;
    this.columns = columns.map(function (col) {
      var group = new ColumnGroup(col, _this, store);

      if (prev) {
        prev.next = group;
        group.prev = prev;
      }

      prev = group;
      return group;
    });
  }

  _createClass(ColumnGroups, [{
    key: "aggregation",
    get: function get() {
      return this.store.aggregation;
    }
  }, {
    key: "wide",
    get: function get() {
      return this.columns.reduce(function (sum, _ref) {
        var colSpan = _ref.colSpan,
            hidden = _ref.hidden;
        return hidden ? sum : sum + colSpan;
      }, 0);
    }
  }, {
    key: "deep",
    get: function get() {
      return Math.max.apply(Math, _toConsumableArray(this.columns.map(function (_ref2) {
        var deep = _ref2.deep;
        return deep;
      })));
    }
  }, {
    key: "hidden",
    get: function get() {
      return this.columns.every(function (_ref3) {
        var hidden = _ref3.hidden;
        return hidden;
      });
    }
  }, {
    key: "width",
    get: function get() {
      return this.columns.reduce(function (sum, _ref4) {
        var width = _ref4.width;
        return sum + width;
      }, 0);
    }
  }, {
    key: "left",
    get: function get() {
      var parent = this.parent;

      if (parent) {
        return parent.left;
      }

      return 0;
    }
  }, {
    key: "right",
    get: function get() {
      var parent = this.parent;

      if (parent) {
        return parent.right;
      }

      return 0;
    }
  }, {
    key: "lastLeaf",
    get: function get() {
      var leafs = this.leafs;
      var length = leafs.length;
      return length ? leafs[length - 1] : undefined;
    }
  }, {
    key: "allLeafs",
    get: function get() {
      var aggregation = this.aggregation;
      return this.columns.reduce(function (leafs, group) {
        return leafs.concat(aggregation && group.column.aggregation ? group : group.allLeafs);
      }, []);
    }
  }, {
    key: "inView",
    get: function get() {
      return this.allLeafs.some(function (group) {
        return group.inView !== false;
      });
    }
  }, {
    key: "leafs",
    get: function get() {
      var aggregation = this.aggregation,
          hidden = this.hidden;
      return hidden ? [] : this.columns.reduce(function (leafs, group) {
        return group.hidden ? leafs : leafs.concat(aggregation && group.column.aggregation ? group : group.leafs);
      }, []);
    }
  }, {
    key: "leftLeafs",
    get: function get() {
      if (!this.parent) {
        var aggregation = this.aggregation;
        return this.columns.reduce(function (leafs, group) {
          return group.lock === ColumnLock.left && !group.hidden ? leafs.concat(aggregation && group.column.aggregation ? group : group.leafs) : leafs;
        }, []);
      }

      return [];
    }
  }, {
    key: "rightLeafs",
    get: function get() {
      if (!this.parent) {
        var aggregation = this.aggregation;
        return this.columns.reduce(function (leafs, group) {
          return group.lock === ColumnLock.right && !group.hidden ? leafs.concat(aggregation && group.column.aggregation ? group : group.leafs) : leafs;
        }, []);
      }

      return [];
    }
  }, {
    key: "leafColumnsWidth",
    get: function get() {
      var _this2 = this;

      return this.allLeafs.reduce(function (total, _ref5) {
        var column = _ref5.column;
        return total + columnWidth(column, _this2.store);
      }, 0);
    }
  }, {
    key: "leftLeafColumnsWidth",
    get: function get() {
      var _this3 = this;

      return this.leftLeafs.reduce(function (total, _ref6) {
        var column = _ref6.column;
        return total + columnWidth(column, _this3.store);
      }, 0);
    }
  }, {
    key: "rightLeafColumnsWidth",
    get: function get() {
      var _this4 = this;

      return this.rightLeafs.reduce(function (total, _ref7) {
        var column = _ref7.column;
        return total + columnWidth(column, _this4.store);
      }, 0);
    }
  }, {
    key: "headerGroup",
    get: function get() {
      var parent = this.parent;

      if (parent) {
        return parent.headerGroup;
      }
    }
  }, {
    key: "headerGroups",
    get: function get() {
      var parent = this.parent;

      if (parent) {
        return parent.headerGroups;
      }
    }
  }, {
    key: "tableGroup",
    get: function get() {
      var parent = this.parent;

      if (parent) {
        return parent.tableGroup;
      }
    }
  }]);

  return ColumnGroups;
}();

export { ColumnGroups as default };

__decorate([computed], ColumnGroups.prototype, "wide", null);

__decorate([computed], ColumnGroups.prototype, "deep", null);

__decorate([computed], ColumnGroups.prototype, "hidden", null);

__decorate([computed], ColumnGroups.prototype, "width", null);

__decorate([computed], ColumnGroups.prototype, "allLeafs", null);

__decorate([computed], ColumnGroups.prototype, "inView", null);

__decorate([computed], ColumnGroups.prototype, "leafs", null);

__decorate([computed], ColumnGroups.prototype, "leftLeafs", null);

__decorate([computed], ColumnGroups.prototype, "rightLeafs", null);
//# sourceMappingURL=ColumnGroups.js.map
