import { addEventListener } from '../../util';
import { computed, makeObservable } from 'mobx';
import { compareFilters } from './compareFilters';
import { compareSortArrays } from './compareSortArrays';
export class CollectionDatasetSyncState {
    constructor(params) {
        this.dataset = params.dataset;
        this.onDatasetChange = params.onDatasetChange;
        this.collection = params.collection;
        this._syncDatasetToCollection();
        makeObservable(this, {
            collectionAsDataset: computed,
            isDeepEqual: 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 = compareFilters(collection.query.filters, dataset.filters, collectionAsDataset.filters);
        if (!filtersEqual) {
            return false;
        }
        // Compare sort arrays
        const sortEqual = 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 addEventListener(collection.emitter, 'fetch', () => {
            this.onDatasetChange?.(this.collectionAsDataset);
        });
    }
}
//# sourceMappingURL=CollectionDatasetSyncState.js.map