"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnFilterApiImpl = void 0;
const tslib_1 = require("tslib");
const SystemRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/SystemRedux"));
const LayoutRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/LayoutRedux"));
const ApiBase_1 = require("./ApiBase");
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
const ColumnFilterInternalApi_1 = require("../Internal/ColumnFilterInternalApi");
class ColumnFilterApiImpl extends ApiBase_1.ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new ColumnFilterInternalApi_1.ColumnFilterInternalApi(_adaptable);
    }
    getFilterPredicateDefsForColumn(column) {
        const scope = {
            ColumnIds: [column.columnId],
        };
        return this.getAdaptableApi()
            .predicateApi.internalApi.getFilterPredicateDefs(scope)
            .filter((predicate) => this.getColumnScopeApi().isColumnInScope(column, predicate.columnScope));
    }
    getFilterPredicateDefsForColumnId(columnId) {
        const column = this.getColumnApi().getColumnWithColumnId(columnId);
        return column ? this.getFilterPredicateDefsForColumn(column) : undefined;
    }
    showQuickFilterBar() {
        this.dispatchAction(SystemRedux.SystemQuickFilterBarShow());
    }
    hideQuickFilterBar() {
        this.dispatchAction(SystemRedux.SystemQuickFilterBarHide());
    }
    isQuickFilterAvailable() {
        return this._adaptable.isQuickFilterAvailable();
    }
    isQuickFilterVisible() {
        return (this.isQuickFilterAvailable() &&
            this.getAdaptableInternalApi().getSystemState().IsQuickFilterVisible);
    }
    getColumnFilters() {
        var _a;
        const currentLayout = this.getLayoutApi().getCurrentLayout();
        return (_a = currentLayout.ColumnFilters) !== null && _a !== void 0 ? _a : [];
    }
    getActiveColumnFilters() {
        return this.getColumnFilters().filter((columnFilter) => !columnFilter.IsSuspended);
    }
    getColumnFilterDefs() {
        const filters = this.getColumnFilters();
        return filters.map((columnFilter) => {
            const column = this.getColumnApi().getColumnWithColumnId(columnFilter.ColumnId);
            const predicate = this.getPredicateApi().getPredicateDefById(columnFilter.Predicate.PredicateId);
            return {
                predicate,
                dataType: column === null || column === void 0 ? void 0 : column.dataType,
                columnFilter: columnFilter,
            };
        });
    }
    getColumnFiltersForLayout(layoutName) {
        var _a;
        const layout = this.getLayoutApi().getLayoutByName(layoutName);
        return (_a = layout.ColumnFilters) !== null && _a !== void 0 ? _a : [];
    }
    setColumnFilters(columnFilters) {
        columnFilters.forEach((columnFilter) => {
            if (!columnFilter.Predicate) {
                this.logWarn('Column Filter has no Predicate', columnFilter);
            }
            else {
                const isValidPredicate = this.getPredicateApi().isValidPredicate(columnFilter.Predicate);
                if (!isValidPredicate) {
                    this.logWarn('Column Filter Predicate is not valid', columnFilter);
                }
                else {
                    this.addUidToAdaptableObject(columnFilter);
                    if (this.getColumnFilters().find((cf) => cf.ColumnId == columnFilter.ColumnId)) {
                        this.dispatchAction(LayoutRedux.LayoutColumnFilterSet(columnFilter));
                    }
                    else {
                        this.dispatchAction(LayoutRedux.LayoutColumnFilterAdd(columnFilter));
                    }
                }
            }
        });
        return columnFilters === null || columnFilters === void 0 ? void 0 : columnFilters.map((columnFilter) => this.getColumnFilterById(columnFilter.Uuid)).filter(Boolean);
    }
    clearColumnFilter(columnFilter) {
        this.dispatchAction(LayoutRedux.LayoutColumnFilterClear(columnFilter));
    }
    clearColumnFiltersForColumns(columns) {
        columns.forEach((c) => {
            this.clearColumnFilterForColumn(c);
        });
    }
    clearColumnFilters() {
        this.dispatchAction(LayoutRedux.LayoutColumnFilterClearAll());
        this._adaptable.clearColumnFiltering();
    }
    clearAndSetColumnFilters(columnFilters) {
        this.clearColumnFilters();
        this.setColumnFilters(columnFilters);
    }
    columnFilterToString(columnFilter) {
        return this.internalApi.columnFilterToString(columnFilter);
    }
    columnFiltersToString(columnFilters) {
        return this.internalApi.columnFiltersToString(columnFilters);
    }
    getColumnFilterById(id) {
        var _a;
        return (_a = this.getColumnFilters()) === null || _a === void 0 ? void 0 : _a.find((columnFilter) => columnFilter.Uuid === id);
    }
    /**
     * Checks if a filter is applied. It is applied when:
     * - has no inputs
     * - has inputs and inputs have values
     *
     * @param filter
     */
    isColumnFilterActive(filter) {
        if (!filter || !filter.Predicate || !filter.Predicate.PredicateId) {
            return false;
        }
        const predicateDef = this.getPredicateApi().getPredicateDefById(filter === null || filter === void 0 ? void 0 : filter.Predicate.PredicateId);
        if (ArrayExtensions_1.default.IsNull(predicateDef.inputs) &&
            // values predicates have no inputs in the definition
            !this.getPredicateApi().internalApi.hasPredicateValues(filter.Predicate)) {
            return true;
        }
        const inputs = filter.Predicate.Inputs;
        if (ArrayExtensions_1.default.IsEmpty(inputs)) {
            return false;
        }
        // all values in the array need to be non-empty (e.g. between has two values, [min, max])
        const allInputsHaveValues = inputs.every((value) => {
            return value !== undefined && value !== null && value !== '';
        });
        return allInputsHaveValues;
    }
    setColumnFilterForColumn(columnId, columnFilterConfig) {
        const columnFilterPredicate = {
            PredicateId: columnFilterConfig.PredicateId,
        };
        if (columnFilterConfig.PredicateInputs) {
            columnFilterPredicate.Inputs = columnFilterConfig.PredicateInputs;
        }
        const columnFilter = {
            ColumnId: columnId,
            Predicate: columnFilterPredicate,
        };
        this.setColumnFilters([columnFilter]);
    }
    suspendColumnFilter(columnFilter) {
        this.dispatchAction(LayoutRedux.LayoutColumnFilterSuspend(columnFilter));
    }
    unSuspendColumnFilter(columnFilter) {
        this.dispatchAction(LayoutRedux.LayoutColumnFilterUnSuspend(columnFilter));
    }
    suspendAllColumnFilters() {
        this.dispatchAction(LayoutRedux.LayoutColumnFilterSuspendAll());
    }
    unSuspendAllColumnFilters() {
        this.dispatchAction(LayoutRedux.LayoutColumnFilterUnSuspendAll());
    }
    getColumnFilterForColumn(columnId) {
        return this.getColumnFilters().find((columnFilter) => columnFilter.ColumnId == columnId);
    }
    clearColumnFilterForColumn(columnId) {
        let columnFilterForColumn = this.getColumnFilterForColumn(columnId);
        if (columnFilterForColumn) {
            this.dispatchAction(LayoutRedux.LayoutColumnFilterClear(columnFilterForColumn));
            this._adaptable.clearColumnFilteringForColumns([columnId]);
        }
    }
}
exports.ColumnFilterApiImpl = ColumnFilterApiImpl;
