text: import * as tslib_1 from "tslib";
  import { Directive, Input, EventEmitter, IterableDiffers, Output } from "@angular/core";
  import * as _ from "lodash";
  import { ReplaySubject } from "rxjs";
  let DataTableService = class DataTableService {
      constructor(differs) {
          this.differs = differs;
          this.inputData = [];
          this.sortBy = "";
          this.sortOrder = "asc";
          this.sortByChange = new EventEmitter();
          this.sortOrderChange = new EventEmitter();
          this.rowsOnPage = 1000;
          this.activePage = 1;
          this.mustRecalculateData = false;
          this.onSortChange = new ReplaySubject(1);
          this.onPageChange = new EventEmitter();
          this.diff = differs.find([]).create(null);
      }
      getSort() {
          return { sortBy: this.sortBy, sortOrder: this.sortOrder };
      }
      setSort(sortBy, sortOrder) {
          if (this.sortBy !== sortBy || this.sortOrder !== sortOrder) {
              this.sortBy = sortBy;
              this.sortOrder = _.includes(["asc", "desc"], sortOrder) ? sortOrder : "asc";
              this.mustRecalculateData = true;
              this.onSortChange.next({ sortBy: sortBy, sortOrder: sortOrder });
              this.sortByChange.emit(this.sortBy);
              this.sortOrderChange.emit(this.sortOrder);
          }
      }
      getPage() {
          return { activePage: this.activePage, rowsOnPage: this.rowsOnPage, dataLength: this.inputData.length };
      }
      setPage(activePage, rowsOnPage) {
          if (this.rowsOnPage !== rowsOnPage || this.activePage !== activePage) {
              this.activePage = this.activePage !== activePage ? activePage : this.calculateNewActivePage(this.rowsOnPage, rowsOnPage);
              this.rowsOnPage = rowsOnPage;
              this.mustRecalculateData = true;
              this.onPageChange.emit({
                  activePage: this.activePage,
                  rowsOnPage: this.rowsOnPage,
                  dataLength: this.inputData ? this.inputData.length : 0
              });
          }
      }
      calculateNewActivePage(previousRowsOnPage, currentRowsOnPage) {
          let firstRowOnPage = (this.activePage - 1) * previousRowsOnPage + 1;
          let newActivePage = Math.ceil(firstRowOnPage / currentRowsOnPage);
          return newActivePage;
      }
      recalculatePage() {
          let lastPage = Math.ceil(this.inputData.length / this.rowsOnPage);
          this.activePage = lastPage < this.activePage ? lastPage : this.activePage;
          this.activePage = this.activePage || 1;
          this.onPageChange.emit({
              activePage: this.activePage,
              rowsOnPage: this.rowsOnPage,
              dataLength: this.inputData.length
          });
      }
      ngOnChanges(changes) {
          if (changes["rowsOnPage"]) {
              this.rowsOnPage = changes["rowsOnPage"].previousValue;
              this.setPage(this.activePage, changes["rowsOnPage"].currentValue);
              this.mustRecalculateData = true;
          }
          if (changes["sortBy"] || changes["sortOrder"]) {
              if (!_.includes(["asc", "desc"], this.sortOrder)) {
                  console.warn("angular2-datatable: value for input mfSortOrder must be one of ['asc', 'desc'], but is:", this.sortOrder);
                  this.sortOrder = "asc";
              }
              if (this.sortBy) {
                  this.onSortChange.next({ sortBy: this.sortBy, sortOrder: this.sortOrder });
              }
              this.mustRecalculateData = true;
          }
          if (changes["inputData"]) {
              this.inputData = changes["inputData"].currentValue || [];
              this.recalculatePage();
              this.mustRecalculateData = true;
          }
      }
      ngDoCheck() {
          let changes = this.diff.diff(this.inputData);
          if (changes) {
              this.recalculatePage();
              this.mustRecalculateData = true;
          }
          if (this.mustRecalculateData) {
              this.fillData();
              this.mustRecalculateData = false;
          }
      }
      fillData() {
          this.activePage = this.activePage;
          this.rowsOnPage = this.rowsOnPage;
          let offset = (this.activePage - 1) * this.rowsOnPage;
          let data = this.inputData;
          var sortBy = this.sortBy;
          if (typeof sortBy === 'string' || sortBy instanceof String) {
              data = _.orderBy(data, this.caseInsensitiveIteratee(sortBy), this.sortOrder == 'asc' ? 'asc' : 'desc');
          }
          else {
              data = _.orderBy(data, sortBy, this.sortOrder == 'asc' ? 'asc' : 'desc');
          }
          data = _.slice(data, offset, offset + this.rowsOnPage);
          this.data = data;
      }
      caseInsensitiveIteratee(sortBy) {
          return (row) => {
              var value = row;
              for (let sortByProperty of sortBy.split('.')) {
                  if (value) {
                      value = value[sortByProperty];
                  }
              }
              if (value && typeof value === 'string' || value instanceof String) {
                  return value.toLowerCase();
              }
              return value;
          };
      }
  };
  tslib_1.__decorate([
      Input("mfData"),
      tslib_1.__metadata("design:type", Array)
  ], DataTableService.prototype, "inputData", void 0);
  tslib_1.__decorate([
      Input("mfSortBy"),
      tslib_1.__metadata("design:type", Object)
  ], DataTableService.prototype, "sortBy", void 0);
  tslib_1.__decorate([
      Input("mfSortOrder"),
      tslib_1.__metadata("design:type", Object)
  ], DataTableService.prototype, "sortOrder", void 0);
  tslib_1.__decorate([
      Output("mfSortByChange"),
      tslib_1.__metadata("design:type", Object)
  ], DataTableService.prototype, "sortByChange", void 0);
  tslib_1.__decorate([
      Output("mfSortOrderChange"),
      tslib_1.__metadata("design:type", Object)
  ], DataTableService.prototype, "sortOrderChange", void 0);
  tslib_1.__decorate([
      Input("mfRowsOnPage"),
      tslib_1.__metadata("design:type", Object)
  ], DataTableService.prototype, "rowsOnPage", void 0);
  tslib_1.__decorate([
      Input("mfActivePage"),
      tslib_1.__metadata("design:type", Object)
  ], DataTableService.prototype, "activePage", void 0);
  DataTableService = tslib_1.__decorate([
      Directive({
          selector: 'table[mfData]',
          exportAs: 'mfDataTable'
      }),
      tslib_1.__metadata("design:paramtypes", [IterableDiffers])
  ], DataTableService);
  export { DataTableService };
  //# sourceMappingURL=datatable.service.js.map