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 { useEffect } from 'react';
import { observer } from 'mobx-react-lite';
import { clsx, Dialog, Modal, ModalBody, ModalFooter, ModalFooterButton, ModalFooterStatus, ModalHeader, PanelLoadingIndicator, RefreshIcon, } from '@finos/legend-art';
import { QueryBuilderTextEditorMode } from '../stores/QueryBuilderTextEditorState.js';
import { flowResult } from 'mobx';
import { useApplicationStore } from '@finos/legend-application';
import { LambdaEditor } from './shared/LambdaEditor.js';
import { CODE_EDITOR_LANGUAGE } from '@finos/legend-code-editor';
import { CodeEditor } from '@finos/legend-lego/code-editor';
export const QueryBuilderTextEditor = observer((props) => {
    const { queryBuilderState } = props;
    const applicationStore = useApplicationStore();
    const queryTextEditorState = queryBuilderState.textEditorState;
    const isReadOnly = queryTextEditorState.isReadOnly;
    const close = applicationStore.guardUnhandledError(() => flowResult(queryBuilderState.textEditorState.closeModal()));
    const discardChanges = () => {
        queryBuilderState.textEditorState.setMode(undefined);
        // force close the backdrop just in case changes are discarded when there are grammar issues
        applicationStore.layoutService.setShowBackdrop(false);
    };
    const mode = queryTextEditorState.mode;
    const isEditingPure = mode === QueryBuilderTextEditorMode.TEXT && !isReadOnly;
    const title = mode === QueryBuilderTextEditorMode.TEXT
        ? isReadOnly
            ? 'Pure Query'
            : 'Edit Pure Query'
        : 'Pure Query Protocol';
    const copyExpression = () => {
        applicationStore.clipboardService
            .copyTextToClipboard(queryTextEditorState.text ?? '')
            .then(() => applicationStore.notificationService.notifySuccess('Query Copied', undefined, 2500))
            .catch(applicationStore.alertUnhandledError);
    };
    useEffect(() => {
        flowResult(queryTextEditorState.convertLambdaObjectToGrammarString({
            pretty: true,
        })).catch(applicationStore.alertUnhandledError);
    }, [applicationStore, queryTextEditorState]);
    return (_jsx(Dialog, { open: Boolean(mode), onClose: close, classes: {
            root: 'editor-modal__root-container',
            container: 'editor-modal__container',
            paper: 'editor-modal__content',
        }, children: _jsxs(Modal, { darkMode: !applicationStore.layoutService.TEMPORARY__isLightColorThemeEnabled, className: clsx('editor-modal query-builder-text-mode__modal', {
                'query-builder-text-mode__modal--has-error': Boolean(queryTextEditorState.parserError),
            }), children: [_jsxs(ModalHeader, { children: [_jsx("div", { className: "modal__title", children: title }), queryTextEditorState.parserError && (_jsx("div", { className: "modal__title__error-badge", children: "Failed to parse query" }))] }), _jsx(PanelLoadingIndicator, { isLoading: queryBuilderState.textEditorState.closingQueryState.isInProgress }), _jsx(ModalBody, { children: _jsxs("div", { className: clsx('query-builder-text-mode__modal__content', {
                            backdrop__element: Boolean(queryTextEditorState.parserError),
                        }), children: [mode === QueryBuilderTextEditorMode.TEXT &&
                                (isReadOnly ? (_jsx(CodeEditor, { language: CODE_EDITOR_LANGUAGE.PURE, inputValue: queryTextEditorState.fullLambdaString, isReadOnly: true })) : (_jsx(LambdaEditor, { className: "query-builder-text-mode__lambda-editor", disabled: queryTextEditorState.isConvertingLambdaToString, lambdaEditorState: queryTextEditorState, forceBackdrop: false, autoFocus: true }))), mode === QueryBuilderTextEditorMode.JSON && (_jsx(CodeEditor, { language: CODE_EDITOR_LANGUAGE.JSON, inputValue: queryTextEditorState.readOnlylambdaJson, isReadOnly: true }))] }) }), _jsxs(ModalFooter, { children: [queryBuilderState.textEditorState.closingQueryState
                            .isInProgress && (_jsx(ModalFooterStatus, { children: "Closing Query..." })), queryBuilderState.queryCompileState.isInProgress && (_jsxs(ModalFooterStatus, { children: [_jsx("div", { className: "loading-icon__container--spinning", children: _jsx(RefreshIcon, {}) }), "Compiling Query..."] })), isEditingPure ? (_jsxs(_Fragment, { children: [_jsx(ModalFooterButton, { onClick: close, disabled: Boolean(queryTextEditorState.parserError) ||
                                        queryBuilderState.textEditorState.closingQueryState
                                            .isInProgress, children: queryBuilderState.textEditorState.closingQueryState
                                        .isInProgress ? (_jsx(_Fragment, { children: _jsxs("div", { className: "loading-icon__container--spinning", children: [_jsx(RefreshIcon, {}), "Proceeding"] }) })) : (_jsx(_Fragment, { children: " Proceed " })) }), _jsx(ModalFooterButton, { className: "btn--caution", onClick: discardChanges, type: "secondary", text: "Cancel" })] })) : (_jsxs(_Fragment, { children: [_jsx(ModalFooterButton, { formatText: false, onClick: copyExpression, text: "Copy to Clipboard" }), _jsx(ModalFooterButton, { onClick: close, disabled: Boolean(queryTextEditorState.parserError) ||
                                        queryBuilderState.textEditorState.closingQueryState
                                            .isInProgress, type: "secondary", children: queryBuilderState.textEditorState.closingQueryState
                                        .isInProgress ? (_jsx(_Fragment, { children: _jsxs("div", { className: "loading-icon__container--spinning", children: [_jsx(RefreshIcon, {}), "Closing"] }) })) : (_jsx(_Fragment, { children: " Close " })) })] }))] })] }) }));
});
//# sourceMappingURL=QueryBuilderTextEditor.js.map