import * as React from 'react';
import { useMemo } from 'react';
import { BaseEditorInput } from './BaseEditorInput';
import { editorButtonsSearch } from './editorButtonsSearch';
import { useExpressionEditor } from './EditorContext';
import { editorButtonsAggregatedScalar } from './editorButtonsAggregatedScalar';
import { cumulativeAggregatedExpressionFunctions, quantileAggregatedExpressionFunctions, } from '../../Utilities/ExpressionFunctions/aggregatedScalarExpressionFunctions';
import { editorButtonsCumulativeAggregatedScalar } from './editorButtonsCumulativeAggregatedScalar';
import { editorButtonsQuantileAggregatedScalar } from './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
                ...cumulativeAggregatedExpressionFunctions,
                ...quantileAggregatedExpressionFunctions,
            ]);
            filteredCollection = Object.keys(availableAggregatedExpressionFunctions).filter((key) => 
            //  need to allow standard ones
            cumulativeAggregatedExpressionFunctions.includes(key) ||
                // and anything else that is not cumul or quantile
                !excludeSet.has(key));
        }
        else {
            const includeExpressionFunctions = type === 'cumulativeAggregatedScalar'
                ? cumulativeAggregatedExpressionFunctions
                : quantileAggregatedExpressionFunctions;
            filteredCollection = Object.keys(availableAggregatedExpressionFunctions).filter((key) => includeExpressionFunctions.includes(key));
        }
        return filteredCollection.reduce((obj, key) => {
            obj[key] = availableAggregatedExpressionFunctions[key];
            return obj;
        }, {});
    };
    const expressionFunctions = useMemo(() => {
        return props.type === 'aggregatedScalar' ||
            props.type === 'cumulativeAggregatedScalar' ||
            props.type === 'quantileAggregatedScalar'
            ? getFilteredAggregatedExpressionFunctions(moduleExpressionFunctions.aggregatedScalarFunctions, props.type)
            : {
                ...moduleExpressionFunctions.booleanFunctions,
                ...moduleExpressionFunctions.scalarFunctions,
            };
    }, [props.type]);
    const { setSelectedFunction } = 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, { type: 'main', value: props.value, placeholder: queryName, onChange: props.onChange, onSelectedFunctionChange: setSelectedFunction, expressionFunctions: expressionFunctions, editorButtons: props.type === 'aggregatedScalar'
            ? editorButtonsAggregatedScalar
            : props.type === 'cumulativeAggregatedScalar'
                ? editorButtonsCumulativeAggregatedScalar
                : props.type === 'quantileAggregatedScalar'
                    ? editorButtonsQuantileAggregatedScalar
                    : editorButtonsSearch, testData: props.testData, isFullExpression: props.isFullExpression, hideResultPreview: props.type === 'aggregatedScalar' ||
            props.type === 'cumulativeAggregatedScalar' ||
            props.type === 'quantileAggregatedScalar', api: props.api }));
}
export default EditorInput;
