"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.FilterStateManager = void 0;

const types_1 = require("./types");

class FilterStateManager {
    constructor(table, engine) {
        this.listeners = [], this.state = {
            filters: new Map
        }, this.engine = engine, this.table = table;
    }
    getFilterState(fieldId) {
        return this.state.filters.get(fieldId);
    }
    getActiveFilterFields() {
        const activeFields = [];
        return this.state.filters.forEach(((config, field) => {
            config.enable && activeFields.push(field);
        })), activeFields;
    }
    getAllFilterStates() {
        return this.state;
    }
    reapplyCurrentFilters() {
        this.getActiveFilterFields().length > 0 && this.applyFilters();
    }
    dispatch(action) {
        this.state = this.reduce(this.state, action), this.shouldApplyFilter(action) && this.applyFilters(), 
        this.notifyListeners(action);
    }
    subscribe(listener) {
        return this.listeners.push(listener), () => {
            this.listeners = this.listeners.filter((l => l !== listener));
        };
    }
    notifyListeners(action) {
        this.listeners.forEach((listener => listener(this.state, action)));
    }
    reduce(state, action) {
        const {type: type, payload: payload} = action, newFilter = new Map(state.filters);
        switch (type) {
          case types_1.FilterActionType.ADD_FILTER:
            newFilter.set(payload.field, payload);
            break;

          case types_1.FilterActionType.REMOVE_FILTER:
            newFilter.delete(payload.field);
            break;

          case types_1.FilterActionType.UPDATE_FILTER:
            newFilter.set(payload.field, Object.assign(Object.assign({}, newFilter.get(payload.field)), payload));
            break;

          case types_1.FilterActionType.ENABLE_FILTER:
            newFilter.set(payload.field, Object.assign(Object.assign({}, newFilter.get(payload.field)), {
                enable: !0
            }));
            break;

          case types_1.FilterActionType.DISABLE_FILTER:
            newFilter.set(payload.field, Object.assign(Object.assign({}, newFilter.get(payload.field)), {
                enable: !1
            }));
            break;

          case types_1.FilterActionType.CLEAR_ALL_FILTERS:
            newFilter.clear();
            break;

          case types_1.FilterActionType.APPLY_FILTERS:
            newFilter.set(payload.field, Object.assign(Object.assign(Object.assign({}, newFilter.get(payload.field)), payload), {
                enable: !0
            }));
        }
        return Object.assign(Object.assign({}, state), {
            filters: newFilter
        });
    }
    applyFilters() {
        this.engine.applyFilter(this.state, this.table);
    }
    shouldApplyFilter(action) {
        return [ types_1.FilterActionType.REMOVE_FILTER, types_1.FilterActionType.ENABLE_FILTER, types_1.FilterActionType.DISABLE_FILTER, types_1.FilterActionType.CLEAR_ALL_FILTERS, types_1.FilterActionType.APPLY_FILTERS ].includes(action.type);
    }
}

exports.FilterStateManager = FilterStateManager;
//# sourceMappingURL=filter-state-manager.js.map