"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
const BaseEditorInput_1 = require("./BaseEditorInput");
const editorButtonsSearch_1 = require("./editorButtonsSearch");
const EditorContext_1 = require("./EditorContext");
const editorButtonsAggregatedScalar_1 = require("./editorButtonsAggregatedScalar");
const aggregatedScalarExpressionFunctions_1 = require("../../Utilities/ExpressionFunctions/aggregatedScalarExpressionFunctions");
const editorButtonsCumulativeAggregatedScalar_1 = require("./editorButtonsCumulativeAggregatedScalar");
const editorButtonsQuantileAggregatedScalar_1 = require("./editorButtonsQuantileAggregatedScalar");
function EditorInput(props) {
    const moduleExpressionFunctions = props.api.internalApi
        .getQueryLanguageService()
        .getModuleExpressionFunctionsMap(props.module);
    const getFilteredAggregatedExpressionFunctions = (availableAggregatedExpressionFunctions, type) => {
        let filteredCollection = [];
        if (type === 'aggregatedScalar') {
            const excludeSet = new Set([
                // we cannot have a hardcodede set of functions,
                // we need to allow custom expressions
                ...aggregatedScalarExpressionFunctions_1.cumulativeAggregatedExpressionFunctions,
                ...aggregatedScalarExpressionFunctions_1.quantileAggregatedExpressionFunctions,
            ]);
            filteredCollection = Object.keys(availableAggregatedExpressionFunctions).filter((key) => 
            //  need to allow standard ones
            aggregatedScalarExpressionFunctions_1.cumulativeAggregatedExpressionFunctions.includes(key) ||
                // and anything else that is not cumul or quantile
                !excludeSet.has(key));
        }
        else {
            const includeExpressionFunctions = type === 'cumulativeAggregatedScalar'
                ? aggregatedScalarExpressionFunctions_1.cumulativeAggregatedExpressionFunctions
                : aggregatedScalarExpressionFunctions_1.quantileAggregatedExpressionFunctions;
            filteredCollection = Object.keys(availableAggregatedExpressionFunctions).filter((key) => includeExpressionFunctions.includes(key));
        }
        return filteredCollection.reduce((obj, key) => {
            obj[key] = availableAggregatedExpressionFunctions[key];
            return obj;
        }, {});
    };
    const expressionFunctions = (0, react_1.useMemo)(() => {
        return props.type === 'aggregatedScalar' ||
            props.type === 'cumulativeAggregatedScalar' ||
            props.type === 'quantileAggregatedScalar'
            ? getFilteredAggregatedExpressionFunctions(moduleExpressionFunctions.aggregatedScalarFunctions, props.type)
            : {
                ...moduleExpressionFunctions.booleanFunctions,
                ...moduleExpressionFunctions.scalarFunctions,
            };
    }, [props.type]);
    const { setSelectedFunction } = (0, EditorContext_1.useExpressionEditor)();
    let queryName;
    switch (props.type) {
        case 'boolean':
            queryName = 'Create an Expression that returns true / false';
            break;
        case 'scalar':
            queryName = "Create an Expression that returns a single value of any type";
            break;
        case 'aggregatedScalar':
            queryName = "Create an Expression that evaluates multiple rows and returns a value of any type";
            break;
        case 'cumulativeAggregatedScalar':
            queryName = "Create a Cumulative Expression";
            break;
        case 'quantileAggregatedScalar':
            queryName = "Create a Quantile Expression";
            break;
    }
    return (React.createElement(BaseEditorInput_1.BaseEditorInput, { type: 'main', value: props.value, placeholder: queryName, onChange: props.onChange, onSelectedFunctionChange: setSelectedFunction, expressionFunctions: expressionFunctions, editorButtons: props.type === 'aggregatedScalar'
            ? editorButtonsAggregatedScalar_1.editorButtonsAggregatedScalar
            : props.type === 'cumulativeAggregatedScalar'
                ? editorButtonsCumulativeAggregatedScalar_1.editorButtonsCumulativeAggregatedScalar
                : props.type === 'quantileAggregatedScalar'
                    ? editorButtonsQuantileAggregatedScalar_1.editorButtonsQuantileAggregatedScalar
                    : editorButtonsSearch_1.editorButtonsSearch, testData: props.testData, isFullExpression: props.isFullExpression, hideResultPreview: props.type === 'aggregatedScalar' ||
            props.type === 'cumulativeAggregatedScalar' ||
            props.type === 'quantileAggregatedScalar', api: props.api }));
}
exports.default = EditorInput;
