"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalculatedColumnExpressionWizardSection = exports.isValidCalculatedColumnExpression = exports.renderCalculatedColumnExpressionSummary = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const rebass_1 = require("rebass");
const OnePageAdaptableWizard_1 = require("../../Wizard/OnePageAdaptableWizard");
const ExpressionEditor_1 = require("../../../components/ExpressionEditor");
const Tabs_1 = require("../../../components/Tabs");
const CodeBlock_1 = require("../../../components/CodeBlock");
const StringExtensions_1 = require("../../../Utilities/Extensions/StringExtensions");
const renderCalculatedColumnExpressionSummary = (data) => {
    const { api } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    return (React.createElement(rebass_1.Box, { fontSize: 2 },
        "Column scalar expression:",
        ' ',
        React.createElement(CodeBlock_1.CodeBlock, null, api.internalApi.getAdaptableQueryExpressionText(data.Query))));
};
exports.renderCalculatedColumnExpressionSummary = renderCalculatedColumnExpressionSummary;
const isValidCalculatedColumnExpression = (data, api) => {
    const calculatedColumnExpressionService = api.internalApi.getCalculatedColumnExpressionService();
    const expression = api.expressionApi.getAdaptableQueryExpression(data.Query)?.trim();
    if (!expression) {
        return 'Calculated column expression cannot be empty';
    }
    const isValid = calculatedColumnExpressionService.isCalculatedColumnQueryValid(data.Query);
    if (!isValid) {
        return 'Calculated column expression is not valid';
    }
    return true;
};
exports.isValidCalculatedColumnExpression = isValidCalculatedColumnExpression;
const CalculatedColumnExpressionWizardSection = (props) => {
    const { api, data, moduleInfo } = (0, OnePageAdaptableWizard_1.useOnePageAdaptableWizardContext)();
    const initialData = (0, react_1.useMemo)(() => api.internalApi.getQueryPreviewData(), []);
    const setCalculatedColumnExpression = (calculatedColumnQuery) => {
        const calculatedColumnExpressionService = api.internalApi.getCalculatedColumnExpressionService();
        const dataType = calculatedColumnExpressionService.getCalculatedColumnDataType(calculatedColumnQuery);
        props.onChange({
            ...data,
            Query: calculatedColumnQuery,
            CalculatedColumnSettings: {
                ...data.CalculatedColumnSettings,
                DataType: dataType,
                //  Pivotable,
                //  Aggregatable,
            },
        });
    };
    const getEditorType = (expressionType) => {
        switch (expressionType) {
            case 'AggregatedScalarExpression':
                return 'aggregatedScalar';
            case 'CumulativeAggregatedExpression':
                return 'cumulativeAggregatedScalar';
            case 'QuantileAggregatedExpression':
                return 'quantileAggregatedScalar';
        }
    };
    const columns = api.columnApi.internalApi.getQueryableColumnsForUIEditor();
    return (React.createElement(Tabs_1.Tabs, { autoFocus: false, height: "100%" },
        React.createElement(Tabs_1.Tabs.Tab, null, (0, StringExtensions_1.Humanize)(props.expressionType)),
        React.createElement(Tabs_1.Tabs.Content, null, (() => {
            switch (props.expressionType) {
                case 'ScalarExpression':
                    return (React.createElement(ExpressionEditor_1.ExpressionEditor, { type: 'scalar', module: moduleInfo.ModuleName, style: { paddingLeft: 0 }, value: data.Query.ScalarExpression, onChange: (ScalarExpression) => setCalculatedColumnExpression({ ScalarExpression }), initialData: initialData, columns: columns, fields: api.expressionApi.internalApi.getAvailableFields(), namedQueries: api.namedQueryApi.getNamedQueries(), isFullExpression: true, api: api }));
                case 'AggregatedScalarExpression':
                case 'CumulativeAggregatedExpression':
                case 'QuantileAggregatedExpression':
                    return (React.createElement(ExpressionEditor_1.ExpressionEditor, { type: getEditorType(props.expressionType), module: moduleInfo.ModuleName, style: { paddingLeft: 0 }, value: data.Query.AggregatedScalarExpression, onChange: (AggregatedScalarExpression) => setCalculatedColumnExpression({ AggregatedScalarExpression }), initialData: initialData, columns: columns, namedQueries: api.namedQueryApi.getNamedQueries(), isFullExpression: true, api: api }));
                default:
                    return React.createElement(React.Fragment, null);
            }
        })())));
};
exports.CalculatedColumnExpressionWizardSection = CalculatedColumnExpressionWizardSection;
