import * as LayoutRedux from '../../Redux/ActionsReducers/LayoutRedux';
import { WINDOW_GRID_FILTER_EDITOR } from '../../View/Components/Popups/WindowPopups/windowFactory';
import { GridFilterInternalApi } from '../Internal/GridFilterInternalApi';
import { ApiBase } from './ApiBase';
import * as ModuleConstants from '../../Utilities/Constants/ModuleConstants';
export class GridFilterApiImpl extends ApiBase {
    internalApi;
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new GridFilterInternalApi(_adaptable);
    }
    reApplyGridFilter() {
        const currentGridFilter = this.getCurrentGridFilter();
        if (currentGridFilter?.Expression) {
            this.setGridFilterExpression(currentGridFilter.Expression);
        }
    }
    getCurrentGridFilter() {
        return this.getLayoutApi().getCurrentLayout()?.GridFilter;
    }
    getCurrentGridFilterExpression() {
        const gridFilter = this.getCurrentGridFilter();
        if (!gridFilter || gridFilter.IsSuspended) {
            return '';
        }
        return gridFilter.Expression;
    }
    setGridFilterExpression(filter) {
        this.dispatchAction(LayoutRedux.LayoutGridFilterSet(filter));
    }
    setGridFilterExpressionUsingNamedQuery(namedQuery) {
        const expressionString = 'QUERY("' + namedQuery.Name + '")';
        this.setGridFilterExpression(expressionString);
    }
    clearGridFilter() {
        this.dispatchAction(LayoutRedux.LayoutGridFilterClear());
    }
    openUIEditorForGridFilter(expression) {
        const preparedQuery = expression ?? this.getCurrentGridFilterExpression();
        this.getAdaptableInternalApi().showPopupWindow({
            id: WINDOW_GRID_FILTER_EDITOR,
            title: ModuleConstants.GridFilterFriendlyName,
            icon: 'filter-list',
            factoryId: WINDOW_GRID_FILTER_EDITOR,
            popupProps: {
                value: preparedQuery,
            },
        });
    }
    suspendGridFilter() {
        const gridFilter = this.getCurrentGridFilter();
        if (gridFilter && !gridFilter.IsSuspended) {
            this.dispatchAction(LayoutRedux.LayoutGridFilterSuspend());
        }
    }
    unSuspendGridFilter() {
        const gridFilter = this.getCurrentGridFilter();
        if (gridFilter && gridFilter.IsSuspended) {
            this.dispatchAction(LayoutRedux.LayoutGridFilterUnSuspend());
        }
    }
}
