import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { CalculatorIcon, ContextMenu, DollarIcon, DragPreviewLayer, InfoCircleIcon, MenuContent, MenuContentItem, PanelFormListItems, PencilIcon, TimesIcon, clsx, useDragPreviewLayer, } from '@finos/legend-art';
import { SimpleFunctionExpression, } from '@finos/legend-graph';
import { observer } from 'mobx-react-lite';
import { useDrag } from 'react-dnd';
import { getValueSpecificationStringValue } from '../../stores/shared/ValueSpecificationEditorHelper.js';
import { QUERY_BUILDER_VARIABLE_DND_TYPE, VariableInfoTooltip, } from './BasicValueSpecificationEditor.js';
import { buildDatePickerOption } from './CustomDatePickerHelper.js';
import { QueryBuilderSimpleConstantExpressionState } from '../../stores/QueryBuilderConstantsState.js';
import { forwardRef, useRef, useState } from 'react';
const CALCULATED = '(calculated)';
export const getNameOfValueSpecification = (value, queryBuilderState) => {
    if (value instanceof SimpleFunctionExpression) {
        const possibleDateLabel = buildDatePickerOption(value, queryBuilderState.applicationStore).label;
        if (possibleDateLabel) {
            return possibleDateLabel;
        }
    }
    return getValueSpecificationStringValue(value, queryBuilderState.applicationStore);
};
const QueryBuilderVariableContextMenu = observer(forwardRef(function QueryBuilderVariableContextMenu(props, ref) {
    const { actions, extraContextMenuActions, variableInUse } = props;
    return (_jsxs(MenuContent, { ref: ref, children: [extraContextMenuActions?.map((action) => (_jsx(MenuContentItem, { onClick: action.handler, children: action.label }, action.key))), actions?.editVariable && (_jsx(MenuContentItem, { onClick: actions.editVariable, children: "Edit" })), actions?.deleteVariable && (_jsx(MenuContentItem, { disabled: variableInUse, onClick: actions.deleteVariable, children: "Remove" }))] }));
}));
export const VariableViewer = observer((props) => {
    const { variable, value, actions, isReadOnly, queryBuilderState, extraContextMenuActions, option, } = props;
    const isVariableUsed = queryBuilderState.isVariableUsed(variable);
    const [isSelectedFromContextMenu, setIsSelectedFromContextMenu] = useState(false);
    const onContextMenuOpen = () => setIsSelectedFromContextMenu(true);
    const onContextMenuClose = () => setIsSelectedFromContextMenu(false);
    const isConstant = Boolean(value);
    const constantValueString = value?.val
        ? getNameOfValueSpecification(value.val, queryBuilderState)
        : undefined;
    const name = variable.name;
    const variableTypeName = variable.genericType?.value.rawType.name ??
        (isConstant ? CALCULATED : undefined);
    const isMilestoningParameter = queryBuilderState.milestoningState.isMilestoningParameter(variable);
    const milestoningParameterValue = queryBuilderState.milestoningState.getMilestoningParameterValue(variable);
    const milestoningParameterValueString = isMilestoningParameter
        ? milestoningParameterValue
            ? getNameOfValueSpecification(milestoningParameterValue, queryBuilderState)
            : undefined
        : undefined;
    const deleteDisabled = isReadOnly || isVariableUsed;
    const deleteTitle = isVariableUsed ? 'Used in query' : 'Remove';
    const editVariable = () => {
        actions?.editVariable();
    };
    const deleteVariable = () => {
        actions?.deleteVariable();
    };
    const [, dragConnector, dragPreviewConnector] = useDrag(() => ({
        type: QUERY_BUILDER_VARIABLE_DND_TYPE,
        item: { variable: variable },
    }), [variable]);
    const ref = useRef(null);
    dragConnector(ref);
    useDragPreviewLayer(dragPreviewConnector);
    return (_jsx("div", { className: "query-builder__variables__variable", ref: ref, children: _jsxs(ContextMenu, { content: _jsx(QueryBuilderVariableContextMenu, { variable: variable, variableInUse: isVariableUsed, actions: actions, extraContextMenuActions: extraContextMenuActions }), disabled: isReadOnly || !actions, className: clsx('query-builder__variables__variable__context-menu', {
                'query-builder__variables__variable--selected-from-context-menu': isSelectedFromContextMenu,
            }), menuProps: { elevation: 7 }, onOpen: onContextMenuOpen, onClose: onContextMenuClose, children: [_jsx(DragPreviewLayer, { labelGetter: (item) => item.variable.name === '' ? '(unknown)' : item.variable.name, types: [QUERY_BUILDER_VARIABLE_DND_TYPE] }), _jsxs("div", { onClick: editVariable, className: "query-builder__variables__variable__content", children: [_jsx("div", { className: "query-builder__variables__variable__icon", children: _jsx("div", { className: "query-builder__variables__variable-icon", children: isConstant ? (_jsx("div", { className: "icon query-builder__variables__variable-icon", children: "C" })) : (_jsx(DollarIcon, {})) }) }), _jsxs("div", { className: "query-builder__variables__variable__label", children: [name, isConstant ? (_jsxs("div", { className: clsx('query-builder__constants__value', {
                                        'query-builder__constants__value--icon': !constantValueString,
                                    }), children: [constantValueString, !constantValueString && (_jsx(CalculatorIcon, { title: "Calculated Constant" }))] })) : (_jsxs("div", { className: "query-builder__variables__variable__type", children: [_jsx("div", { className: "query-builder__variables__variable__type__label", children: variableTypeName ?? 'unknown' }), isMilestoningParameter && (_jsxs(_Fragment, { children: [_jsx("div", { className: "query-builder__variables__variable__type__label query-builder__variables__variable__type__label--milestoning", children: "milestoning" }), !option?.hideMilestoningParameterValueString && (_jsx("div", { className: "query-builder__constants__value", children: milestoningParameterValueString }))] }))] }))] })] }), actions && (_jsxs("div", { className: "query-builder__variables__variable__actions", children: [_jsx("button", { className: "query-builder__variables__variable__action", tabIndex: -1, disabled: isReadOnly, onClick: editVariable, title: "Edit", children: _jsx(PencilIcon, {}) }), _jsx("button", { className: "query-builder__variables__variable__action", tabIndex: -1, onClick: deleteVariable, disabled: deleteDisabled, title: deleteTitle, children: _jsx(TimesIcon, {}) }), _jsx(VariableInfoTooltip, { variable: variable, children: _jsx("div", { className: "query-builder__variables__variable__action value-spec-editor__variable__info", children: _jsx(InfoCircleIcon, {}) }) })] }))] }) }));
});
export const VariableSelector = observer((props) => {
    const { queryBuilderState, filterBy } = props;
    const filteredParameterStates = queryBuilderState.parametersState.parameterStates.filter((p) => filterBy ? filterBy(p.parameter) : true);
    const filteredConstantState = queryBuilderState.constantState.constants.filter((c) => filterBy ? filterBy(c.variable) : true);
    return (_jsxs(_Fragment, { children: [_jsxs(PanelFormListItems, { title: "Available parameters", children: [filteredParameterStates.length === 0 && (_jsx(_Fragment, { children: " No available parameters " })), filteredParameterStates.map((pState) => (_jsx(VariableViewer, { variable: pState.parameter, isReadOnly: true, queryBuilderState: queryBuilderState }, pState.uuid)))] }), Boolean(filteredConstantState.length) && (_jsx(PanelFormListItems, { title: "Available constants", children: filteredConstantState.map((constantState) => (_jsx(VariableViewer, { variable: constantState.variable, value: {
                        val: constantState instanceof
                            QueryBuilderSimpleConstantExpressionState
                            ? constantState.value
                            : undefined,
                    }, queryBuilderState: queryBuilderState, isReadOnly: true }, constantState.uuid))) }))] }));
});
//# sourceMappingURL=QueryBuilderVariableSelector.js.map