import { FilterActionType } from "./types";

export 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();
    }
    subscribe(listener) {
        return this.listeners.push(listener), () => {
            this.listeners = this.listeners.filter((l => l !== listener));
        };
    }
    notifyListeners() {
        this.listeners.forEach((listener => listener(this.state)));
    }
    reduce(state, action) {
        const {type: type, payload: payload} = action, newFilter = new Map(state.filters);
        switch (type) {
          case FilterActionType.ADD_FILTER:
            newFilter.set(payload.field, payload);
            break;

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

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

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

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

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

          case 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), this.updateColumnIcons();
    }
    updateColumnIcons() {
        const columns = this.table.columns;
        if (!columns) return;
        const plugin = this.table.pluginManager.getPluginByName("Filter");
        if (!plugin || !plugin.pluginOptions) return;
        const filterIcon = plugin.pluginOptions.filterIcon, filteringIcon = plugin.pluginOptions.filteringIcon;
        columns.forEach(((col, index) => {
            const field = col.field, filterConfig = this.state.filters.get(field);
            !plugin.shouldEnableFilterForColumn || plugin.shouldEnableFilterForColumn(index, col) ? filterConfig && filterConfig.enable ? col.headerIcon = filteringIcon : col.headerIcon = filterIcon : col.headerIcon !== filterIcon && col.headerIcon !== filteringIcon || delete col.headerIcon;
        })), this.table.updateColumns(columns);
    }
    shouldApplyFilter(action) {
        return [ FilterActionType.REMOVE_FILTER, FilterActionType.ENABLE_FILTER, FilterActionType.DISABLE_FILTER, FilterActionType.CLEAR_ALL_FILTERS, FilterActionType.APPLY_FILTERS ].includes(action.type) || action.payload.enable;
    }
}
//# sourceMappingURL=filter-state-manager.js.map