import * as React from 'react';
import { Box } from 'rebass';
import { useAdaptable } from '../../../AdaptableContext';
import { useOnePageAdaptableWizardContext } from '../../../Wizard/OnePageAdaptableWizard';
import { Tag } from '../../../../components/Tag';
import { ExpressionEditor } from '../../../../components/ExpressionEditor';
import { GridFilterModuleId } from '../../../../Utilities/Constants/ModuleConstants';
import StringExtensions from '../../../../Utilities/Extensions/StringExtensions';
import { useGridFilterOptionsForExpressionEditorProps } from '../../../GridFilter/useGridFilterOptionsForExpressionEditor';
export const isGridFiltersValid = (layout, api) => {
    const expression = layout?.GridFilter?.Expression;
    if (StringExtensions.IsNullOrEmpty(expression)) {
        return true;
    }
    return (api.internalApi.getQueryLanguageService().validateBoolean(expression, GridFilterModuleId)
        .isValid || 'Invalid Expression');
};
export const GridFilterSectionSummary = () => {
    const { data: layout } = useOnePageAdaptableWizardContext();
    return (React.createElement(Box, null, React.createElement(Tag, { mb: 1, mr: 1 }, layout?.GridFilter?.Expression ?? 'No Grid Filter')));
};
export const GridFilterSection = (props) => {
    const { api } = useAdaptable();
    const { data: layout } = useOnePageAdaptableWizardContext();
    const initialData = React.useMemo(() => api.internalApi.getQueryPreviewData(), []);
    const expressionEditorProps = useGridFilterOptionsForExpressionEditorProps();
    const expressionEditorContent = (React.createElement(ExpressionEditor, { ...expressionEditorProps, allowSaveNamedQuery: false, type: 'boolean', module: GridFilterModuleId, value: layout?.GridFilter?.Expression ?? '', onChange: (expression) => {
            props.onChange({
                ...layout,
                GridFilter: {
                    ...layout.GridFilter,
                    Expression: expression,
                },
            });
        }, initialData: initialData, columns: api.columnApi.getQueryableColumns().filter((c) => !c.isGeneratedPivotResultColumn), fields: api.expressionApi.internalApi.getAvailableFields(), namedQueries: api.namedQueryApi.getNamedQueries(), api: api }));
    return React.createElement("div", null, expressionEditorContent);
};
