export class AdaptableFilterHandler {
    adaptableApi;
    colId;
    filterDisplayValuesResult;
    previousFilterDisplayValuesResult;
    constructor(adaptableApi) {
        this.adaptableApi = adaptableApi;
    }
    init(params) {
        this.colId = params.column.getColId();
    }
    doesFilterPass(params) {
        try {
            const rowNode = params.node;
            // first assess if the Row is filterable - if not, then return true so it always appears in Grid
            const isRowFilterable = this.adaptableApi.gridApi.internalApi.isRowFilterable(rowNode);
            if (!isRowFilterable) {
                return true;
            }
            const columnFilterModel = params.model;
            if (!columnFilterModel) {
                this.adaptableApi.logWarn(`AdaptableFilterHandler.doesFilterPass: No filter model found for column ${this.colId}`);
                return true;
            }
            if (columnFilterModel.IsSuspended) {
                return true;
            }
            return this.adaptableApi.filterApi.columnFilterApi.internalApi.evaluateColumnFilter(columnFilterModel, rowNode);
        }
        catch (ex) {
            this.adaptableApi.consoleError(ex);
            return false;
        }
    }
    getCachedFilterDisplayValues() {
        return this.filterDisplayValuesResult;
    }
    getLastCachedFilterDisplayValues() {
        return this.filterDisplayValuesResult ?? this.previousFilterDisplayValuesResult;
    }
    getFromCacheOrFetchFilterDisplayValues(options) {
        if (this.filterDisplayValuesResult) {
            return Promise.resolve(this.filterDisplayValuesResult);
        }
        return this.fetchFilterDisplayValues(options);
    }
    async fetchFilterDisplayValues(options) {
        return this.adaptableApi.gridApi.internalApi
            .getDistinctFilterDisplayValuesForColumn({
            columnId: this.colId,
            get currentSearchValue() {
                return options.currentSearchValue;
            },
            previousResult: this.filterDisplayValuesResult,
        })
            .then((result) => {
            this.filterDisplayValuesResult = result;
            return result;
        });
    }
    onNewRowsLoaded() {
        this.resetFilterDisplayValues();
    }
    onAnyFilterChanged() {
        const filterOptions = this.adaptableApi.optionsApi.getFilterOptions();
        if (!filterOptions.customInFilterValues) {
            // no-one is using value.count or value.visible
            // or context.sortedValues
            // so no need to reset the filter display values
            return;
        }
        this.resetFilterDisplayValues();
    }
    resetFilterDisplayValues() {
        if (this.filterDisplayValuesResult) {
            this.previousFilterDisplayValuesResult = this.filterDisplayValuesResult;
        }
        // Reset the filter display values manually
        this.filterDisplayValuesResult = undefined;
    }
    async refreshFilterDisplayValues() {
        this.resetFilterDisplayValues();
        return this.getFromCacheOrFetchFilterDisplayValues({ currentSearchValue: '' });
    }
    refresh(_params) { }
    destroy() {
        this.filterDisplayValuesResult = undefined;
        this.previousFilterDisplayValuesResult = undefined;
    }
}
