text: var __extends = (this && this.__extends) || (function () {
      var extendStatics = function (d, b) {
          extendStatics = Object.setPrototypeOf ||
              ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
              function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
          return extendStatics(d, b);
      };
      return function (d, b) {
          extendStatics(d, b);
          function __() { this.constructor = d; }
          d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
      };
  })();
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
      var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
      if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
      else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
      return c > 3 && r && Object.defineProperty(target, key, r), r;
  };
  import { Autowired, Bean, BeanStub, _ } from "@ag-grid-community/core";
  var PivotStage = /** @class */ (function (_super) {
      __extends(PivotStage, _super);
      function PivotStage() {
          var _this = _super !== null && _super.apply(this, arguments) || this;
          _this.uniqueValues = {};
          return _this;
      }
      PivotStage.prototype.execute = function (params) {
          var rootNode = params.rowNode;
          var changedPath = params.changedPath;
          if (this.columnController.isPivotActive()) {
              this.executePivotOn(rootNode, changedPath);
          }
          else {
              this.executePivotOff(changedPath);
          }
      };
      PivotStage.prototype.executePivotOff = function (changedPath) {
          this.aggregationColumnsHashLastTime = null;
          this.uniqueValues = {};
          if (this.columnController.isSecondaryColumnsPresent()) {
              this.columnController.setSecondaryColumns(null, "rowModelUpdated");
              if (changedPath) {
                  changedPath.setInactive();
              }
          }
      };
      PivotStage.prototype.executePivotOn = function (rootNode, changedPath) {
          var uniqueValues = this.bucketUpRowNodes(rootNode);
          var uniqueValuesChanged = this.setUniqueValues(uniqueValues);
          var aggregationColumns = this.columnController.getValueColumns();
          var aggregationColumnsHash = aggregationColumns.map(function (column) { return column.getId(); }).join('#');
          var aggregationFuncsHash = aggregationColumns.map(function (column) { return column.getAggFunc().toString(); }).join('#');
          var aggregationColumnsChanged = this.aggregationColumnsHashLastTime !== aggregationColumnsHash;
          var aggregationFuncsChanged = this.aggregationFuncsHashLastTime !== aggregationFuncsHash;
          this.aggregationColumnsHashLastTime = aggregationColumnsHash;
          this.aggregationFuncsHashLastTime = aggregationFuncsHash;
          if (uniqueValuesChanged || aggregationColumnsChanged || aggregationFuncsChanged) {
              var result = this.pivotColDefService.createPivotColumnDefs(this.uniqueValues);
              this.pivotColumnGroupDefs = result.pivotColumnGroupDefs;
              this.pivotColumnDefs = result.pivotColumnDefs;
              this.columnController.setSecondaryColumns(this.pivotColumnGroupDefs, "rowModelUpdated");
              // because the secondary columns have changed, then the aggregation needs to visit the whole
              // tree again, so we make the changedPath not active, to force aggregation to visit all paths.
              if (changedPath) {
                  changedPath.setInactive();
              }
          }
      };
      PivotStage.prototype.setUniqueValues = function (newValues) {
          var json1 = JSON.stringify(newValues);
          var json2 = JSON.stringify(this.uniqueValues);
          var uniqueValuesChanged = json1 !== json2;
          // we only continue the below if the unique values are different, as otherwise
          // the result will be the same as the last time we did it
          if (uniqueValuesChanged) {
              this.uniqueValues = newValues;
              return true;
          }
          else {
              return false;
          }
      };
      // returns true if values were different
      PivotStage.prototype.bucketUpRowNodes = function (rootNode) {
          var _this = this;
          // accessed from inside inner function
          var uniqueValues = {};
          // finds all leaf groups and calls mapRowNode with it
          var recursivelySearchForLeafNodes = function (rowNode) {
              if (rowNode.leafGroup) {
                  _this.bucketRowNode(rowNode, uniqueValues);
              }
              else {
                  rowNode.childrenAfterFilter.forEach(function (child) {
                      recursivelySearchForLeafNodes(child);
                  });
              }
          };
          recursivelySearchForLeafNodes(rootNode);
          return uniqueValues;
      };
      PivotStage.prototype.bucketRowNode = function (rowNode, uniqueValues) {
          var pivotColumns = this.columnController.getPivotColumns();
          if (pivotColumns.length === 0) {
              rowNode.childrenMapped = null;
              return;
          }
          rowNode.childrenMapped = this.bucketChildren(rowNode.childrenAfterFilter, pivotColumns, 0, uniqueValues);
      };
      PivotStage.prototype.bucketChildren = function (children, pivotColumns, pivotIndex, uniqueValues) {
          var _this = this;
          var mappedChildren = {};
          var pivotColumn = pivotColumns[pivotIndex];
          // map the children out based on the pivot column
          children.forEach(function (child) {
              var key = _this.valueService.getKeyForNode(pivotColumn, child);
              if (_.missing(key)) {
                  key = '';
              }
              if (!uniqueValues[key]) {
                  uniqueValues[key] = {};
              }
              if (!mappedChildren[key]) {
                  mappedChildren[key] = [];
              }
              mappedChildren[key].push(child);
          });
          // if it's the last pivot column, return as is, otherwise go one level further in the map
          if (pivotIndex === pivotColumns.length - 1) {
              return mappedChildren;
          }
          else {
              var result_1 = {};
              _.iterateObject(mappedChildren, function (key, value) {
                  result_1[key] = _this.bucketChildren(value, pivotColumns, pivotIndex + 1, uniqueValues[key]);
              });
              return result_1;
          }
      };
      PivotStage.prototype.getPivotColumnDefs = function () {
          return this.pivotColumnDefs;
      };
      __decorate([
          Autowired('valueService')
      ], PivotStage.prototype, "valueService", void 0);
      __decorate([
          Autowired('columnController')
      ], PivotStage.prototype, "columnController", void 0);
      __decorate([
          Autowired('pivotColDefService')
      ], PivotStage.prototype, "pivotColDefService", void 0);
      PivotStage = __decorate([
          Bean('pivotStage')
      ], PivotStage);
      return PivotStage;
  }(BeanStub));
  export { PivotStage };
  