"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NamedQueryExpressionWizardSection = exports.renderNamedQueryExpressionSummary = exports.isValidNamedQueryExpression = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const ExpressionEditor_1 = require("../../../components/ExpressionEditor");
const Tag_1 = require("../../../components/Tag");
const ModuleConstants_1 = require("../../../Utilities/Constants/ModuleConstants");
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const Flex_1 = require("../../../components/Flex");
const isValidNamedQueryExpression = (data, api) => {
    if (!data.BooleanExpression) {
        return 'An expression is required.';
    }
    const valid = api.expressionApi.isValidBooleanExpression(data.BooleanExpression, ModuleConstants_1.NamedQueryModuleId);
    if (!valid) {
        return 'The query is not a valid Boolean expression.';
    }
    return valid;
};
exports.isValidNamedQueryExpression = isValidNamedQueryExpression;
const renderNamedQueryExpressionSummary = (data) => {
    const { api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    return (React.createElement(Flex_1.Box, { className: "twa:text-2" },
        "Expression: ",
        React.createElement(Tag_1.Tag, null, api.internalApi.getAdaptableQueryExpressionText(data))));
};
exports.renderNamedQueryExpressionSummary = renderNamedQueryExpressionSummary;
const NamedQueryExpressionWizardSection = (props) => {
    const { data, moduleInfo, api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const initialData = (0, react_1.useMemo)(() => api.internalApi.getQueryPreviewData(), []);
    return (React.createElement(ExpressionEditor_1.ExpressionEditor, { allowSaveNamedQuery: false, showQueryBuilder: true, type: 'boolean', module: moduleInfo.ModuleName, value: data.BooleanExpression, onChange: (BooleanExpression) => {
            props.onChange({ ...data, BooleanExpression });
        }, initialData: initialData, columns: api.columnApi.internalApi.getQueryableColumnsForUIEditor(), fields: api.expressionApi.internalApi.getAvailableFields(), namedQueries: api.namedQueryApi.getNamedQueries(), api: api }));
};
exports.NamedQueryExpressionWizardSection = NamedQueryExpressionWizardSection;
