"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CollectionDatasetSyncState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _util = require("../../util");
var _mobx = require("mobx");
var _compareFilters = require("./compareFilters");
var _compareSortArrays = require("./compareSortArrays");
class CollectionDatasetSyncState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "dataset", void 0);
    (0, _defineProperty2.default)(this, "onDatasetChange", void 0);
    (0, _defineProperty2.default)(this, "collection", void 0);
    this.dataset = params.dataset;
    this.onDatasetChange = params.onDatasetChange;
    this.collection = params.collection;
    this._syncDatasetToCollection();
    (0, _mobx.makeObservable)(this, {
      collectionAsDataset: _mobx.computed,
      isDeepEqual: _mobx.computed
    });
  }
  _syncDatasetToCollection() {
    const {
      dataset,
      collection
    } = this;
    const {
      query
    } = collection;
    query.resetFilters();
    Object.entries(dataset.filters).forEach(([filterName, datasetFilterValue]) => {
      const queryFilter = query.filters[filterName];
      if (queryFilter) {
        queryFilter.setValue(queryFilter.decode(datasetFilterValue));
      }
    });
    query.resetSorting();
    if (dataset.sort && dataset.sort.length > 0) {
      query.sort.setSortQuery(dataset.sort.map(({
        fieldKey,
        order
      }) => ({
        field: fieldKey,
        direction: order
      })), {
        emitChangeEvent: true
      });
    } else {
      query.resetSorting({
        emitChangeEvent: true
      });
    }
  }
  setDataset(dataset) {
    // when props.dataset changes, we need to refresh the collection
    if (dataset === this.dataset) {
      return;
    }
    this.dataset = dataset;
    if (this.isDeepEqual) {
      return;
    }
    this._syncDatasetToCollection();
    this.collection.query.events.emit('refresh');
  }
  get isDeepEqual() {
    const {
      collectionAsDataset,
      dataset,
      collection
    } = this;

    // Compare filters
    const filtersEqual = (0, _compareFilters.compareFilters)(collection.query.filters, dataset.filters, collectionAsDataset.filters);
    if (!filtersEqual) {
      return false;
    }

    // Compare sort arrays
    const sortEqual = (0, _compareSortArrays.compareSortArrays)(dataset.sort, collectionAsDataset.sort);
    return sortEqual;
  }
  get collectionAsDataset() {
    const {
      collection
    } = this;
    const {
      query
    } = collection;

    // Extract and encode filters
    const filters = {};
    query.activeFiltersEntries.forEach(([filterName, filter]) => {
      filters[filterName] = filter.encode(filter.value);
    });

    // Convert sort configuration
    const sort = query.sort.value.map(sortItem => ({
      fieldKey: sortItem.field,
      order: sortItem.direction
    }));
    return {
      filters,
      sort
    };
  }
  init() {
    const {
      collection
    } = this;
    return (0, _util.addEventListener)(collection.emitter, 'fetch', () => {
      var _this$onDatasetChange;
      (_this$onDatasetChange = this.onDatasetChange) == null || _this$onDatasetChange.call(this, this.collectionAsDataset);
    });
  }
}
exports.CollectionDatasetSyncState = CollectionDatasetSyncState;
//# sourceMappingURL=CollectionDatasetSyncState.js.map