text: "use strict";
  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;
  };
  var __metadata = (this && this.__metadata) || function (k, v) {
      if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  /**
   * @license
   * Copyright Google LLC All Rights Reserved.
   *
   * Use of this source code is governed by an MIT-style license that can be
   * found in the LICENSE file at https://angular.io/license
   */
  var tree_1 = require("@angular/cdk/tree");
  var core_1 = require("@angular/core");
  var tree_2 = require("@angular/material/tree");
  var rxjs_1 = require("rxjs");
  var LOAD_MORE = 'LOAD_MORE';
  /** Nested node */
  var LoadmoreNode = /** @class */ (function () {
      function LoadmoreNode(item, hasChildren, loadMoreParentItem) {
          if (hasChildren === void 0) { hasChildren = false; }
          if (loadMoreParentItem === void 0) { loadMoreParentItem = null; }
          this.item = item;
          this.hasChildren = hasChildren;
          this.loadMoreParentItem = loadMoreParentItem;
          this.childrenChange = new rxjs_1.BehaviorSubject([]);
      }
      Object.defineProperty(LoadmoreNode.prototype, "children", {
          get: function () {
              return this.childrenChange.value;
          },
          enumerable: true,
          configurable: true
      });
      return LoadmoreNode;
  }());
  exports.LoadmoreNode = LoadmoreNode;
  /** Flat node with expandable and level information */
  var LoadmoreFlatNode = /** @class */ (function () {
      function LoadmoreFlatNode(item, level, expandable, loadMoreParentItem) {
          if (level === void 0) { level = 1; }
          if (expandable === void 0) { expandable = false; }
          if (loadMoreParentItem === void 0) { loadMoreParentItem = null; }
          this.item = item;
          this.level = level;
          this.expandable = expandable;
          this.loadMoreParentItem = loadMoreParentItem;
      }
      return LoadmoreFlatNode;
  }());
  exports.LoadmoreFlatNode = LoadmoreFlatNode;
  /**
   * A database that only load part of the data initially. After user clicks on the `Load more`
   * button, more data will be loaded.
   */
  var LoadmoreDatabase = /** @class */ (function () {
      function LoadmoreDatabase() {
          this.batchNumber = 5;
          this.dataChange = new rxjs_1.BehaviorSubject([]);
          this.nodeMap = new Map();
          /** The data */
          this.rootLevelNodes = ['Vegetables', 'Fruits'];
          this.dataMap = new Map([
              ['Fruits', ['Apple', 'Orange', 'Banana']],
              ['Vegetables', ['Tomato', 'Potato', 'Onion']],
              ['Apple', ['Fuji', 'Macintosh']],
              ['Onion', ['Yellow', 'White', 'Purple', 'Green', 'Shallot', 'Sweet', 'Red', 'Leek']],
          ]);
      }
      LoadmoreDatabase.prototype.initialize = function () {
          var _this = this;
          var data = this.rootLevelNodes.map(function (name) { return _this._generateNode(name); });
          this.dataChange.next(data);
      };
      /** Expand a node whose children are not loaded */
      LoadmoreDatabase.prototype.loadMore = function (item, onlyFirstTime) {
          var _this = this;
          if (onlyFirstTime === void 0) { onlyFirstTime = false; }
          if (!this.nodeMap.has(item) || !this.dataMap.has(item)) {
              return;
          }
          var parent = this.nodeMap.get(item);
          var children = this.dataMap.get(item);
          if (onlyFirstTime && parent.children.length > 0) {
              return;
          }
          var newChildrenNumber = parent.children.length + this.batchNumber;
          var nodes = children.slice(0, newChildrenNumber)
              .map(function (name) { return _this._generateNode(name); });
          if (newChildrenNumber < children.length) {
              // Need a new load more node
              nodes.push(new LoadmoreNode(LOAD_MORE, false, item));
          }
          parent.childrenChange.next(nodes);
          this.dataChange.next(this.dataChange.value);
      };
      LoadmoreDatabase.prototype._generateNode = function (item) {
          if (this.nodeMap.has(item)) {
              return this.nodeMap.get(item);
          }
          var result = new LoadmoreNode(item, this.dataMap.has(item));
          this.nodeMap.set(item, result);
          return result;
      };
      LoadmoreDatabase = __decorate([
          core_1.Injectable()
      ], LoadmoreDatabase);
      return LoadmoreDatabase;
  }());
  exports.LoadmoreDatabase = LoadmoreDatabase;
  /**
   * @title Tree with partially loaded data
   */
  var TreeLoadmoreExample = /** @class */ (function () {
      function TreeLoadmoreExample(database) {
          var _this = this;
          this.database = database;
          this.nodeMap = new Map();
          this.getChildren = function (node) { return node.childrenChange; };
          this.transformer = function (node, level) {
              var existingNode = _this.nodeMap.get(node.item);
              if (existingNode) {
                  return existingNode;
              }
              var newNode = new LoadmoreFlatNode(node.item, level, node.hasChildren, node.loadMoreParentItem);
              _this.nodeMap.set(node.item, newNode);
              return newNode;
          };
          this.getLevel = function (node) { return node.level; };
          this.isExpandable = function (node) { return node.expandable; };
          this.hasChild = function (_, _nodeData) { return _nodeData.expandable; };
          this.isLoadMore = function (_, _nodeData) { return _nodeData.item === LOAD_MORE; };
          this.treeFlattener = new tree_2.MatTreeFlattener(this.transformer, this.getLevel, this.isExpandable, this.getChildren);
          this.treeControl = new tree_1.FlatTreeControl(this.getLevel, this.isExpandable);
          this.dataSource = new tree_2.MatTreeFlatDataSource(this.treeControl, this.treeFlattener);
          database.dataChange.subscribe(function (data) {
              _this.dataSource.data = data;
          });
          database.initialize();
      }
      /** Load more nodes from data source */
      TreeLoadmoreExample.prototype.loadMore = function (item) {
          this.database.loadMore(item);
      };
      TreeLoadmoreExample.prototype.loadChildren = function (node) {
          this.database.loadMore(node.item, true);
      };
      TreeLoadmoreExample = __decorate([
          core_1.Component({
              selector: 'tree-loadmore-example',
              template: "<mat-tree [dataSource]=\"dataSource\" [treeControl]=\"treeControl\"><mat-tree-node *matTreeNodeDef=\"let node\" matTreeNodePadding><button mat-icon-button disabled=\"disabled\"></button> {{node.item}}</mat-tree-node><mat-tree-node *matTreeNodeDef=\"let node; when: hasChild\" matTreeNodePadding><button mat-icon-button [attr.aria-label]=\"'toggle ' + node.filename\" (click)=\"loadChildren(node)\" matTreeNodeToggle><mat-icon class=\"mat-icon-rtl-mirror\">{{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}</mat-icon></button> {{node.item}}</mat-tree-node><mat-tree-node *matTreeNodeDef=\"let node; when: isLoadMore\"><button mat-button (click)=\"loadMore(node.loadMoreParentItem)\">Load more...</button></mat-tree-node></mat-tree>",
              styles: [""],
              providers: [LoadmoreDatabase]
          }),
          __metadata("design:paramtypes", [LoadmoreDatabase])
      ], TreeLoadmoreExample);
      return TreeLoadmoreExample;
  }());
  exports.TreeLoadmoreExample = TreeLoadmoreExample;
  //# sourceMappingURL=tree-loadmore-example.js.map