export class AdaptableFilterHandler {
    constructor(adaptableApi, columnSetup) {
        this.adaptableApi = adaptableApi;
        this.colId = columnSetup.colId;
    }
    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 columnFilters = this.adaptableApi.filterApi.columnFilterApi
                .getActiveColumnFilters()
                .filter((columnFilter) => this.adaptableApi.filterApi.columnFilterApi.isColumnFilterActive(columnFilter))
                .filter((columnFilter) => columnFilter.ColumnId === this.colId)
                .filter((columnFilter) => {
                const shouldEvaluateFilterOnClient = this.adaptableApi.expressionApi.internalApi.shouldEvaluatePredicatesInAdaptableQL('ColumnFilter', columnFilter, columnFilter.Predicates);
                return shouldEvaluateFilterOnClient;
            });
            const anyFilterFailed = columnFilters.some((columnFilter) => !this.adaptableApi.filterApi.columnFilterApi.internalApi.evaluateColumnFilter(columnFilter, rowNode));
            return anyFilterFailed ? false : true;
        }
        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);
    }
    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) {
        // No specific refresh logic needed for this handler
        // The filter display values will be reset on new rows loaded or any filter changed
    }
    destroy() {
        this.filterDisplayValuesResult = undefined;
        this.previousFilterDisplayValuesResult = undefined;
    }
}
