"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GridFilterSection = exports.GridFilterSectionSummary = exports.isGridFiltersValid = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const rebass_1 = require("rebass");
const AdaptableContext_1 = require("../../../AdaptableContext");
const OnePageAdaptableWizard_1 = require("../../../Wizard/OnePageAdaptableWizard");
const Tag_1 = require("../../../../components/Tag");
const ExpressionEditor_1 = require("../../../../components/ExpressionEditor");
const ModuleConstants_1 = require("../../../../Utilities/Constants/ModuleConstants");
const StringExtensions_1 = tslib_1.__importDefault(require("../../../../Utilities/Extensions/StringExtensions"));
const useGridFilterOptionsForExpressionEditor_1 = require("../../../GridFilter/useGridFilterOptionsForExpressionEditor");
const isGridFiltersValid = (layout, api) => {
    const expression = layout?.GridFilter?.Expression;
    if (StringExtensions_1.default.IsNullOrEmpty(expression)) {
        return true;
    }
    return (api.internalApi.getQueryLanguageService().validateBoolean(expression, ModuleConstants_1.GridFilterModuleId)
        .isValid || 'Invalid Expression');
};
exports.isGridFiltersValid = isGridFiltersValid;
const GridFilterSectionSummary = () => {
    const { data: layout } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    return (React.createElement(rebass_1.Box, null, React.createElement(Tag_1.Tag, { mb: 1, mr: 1 }, layout?.GridFilter?.Expression ?? 'No Grid Filter')));
};
exports.GridFilterSectionSummary = GridFilterSectionSummary;
const GridFilterSection = (props) => {
    const { api } = (0, AdaptableContext_1.useAdaptable)();
    const { data: layout } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const initialData = React.useMemo(() => api.internalApi.getQueryPreviewData(), []);
    const expressionEditorProps = (0, useGridFilterOptionsForExpressionEditor_1.useGridFilterOptionsForExpressionEditorProps)();
    const expressionEditorContent = (React.createElement(ExpressionEditor_1.ExpressionEditor, { ...expressionEditorProps, allowSaveNamedQuery: false, type: 'boolean', module: ModuleConstants_1.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);
};
exports.GridFilterSection = GridFilterSection;
