import { jsx as _jsx, jsxs as _jsxs } 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 { useRef, useState, useEffect } from 'react';
import { observer } from 'mobx-react-lite';
import { ELEMENT_PATH_DELIMITER, resolvePackagePathAndElementName, Package, } from '@finos/legend-graph';
import { guaranteeType } from '@finos/legend-shared';
import { FileGenerationConfigurationEditor } from './FileGenerationEditor.js';
import { flowResult } from 'mobx';
import { Dialog, ResizablePanel, ResizablePanelGroup, ResizablePanelSplitter, ResizablePanelSplitterLine, ArrowCircleLeftIcon, } from '@finos/legend-art';
import { useEditorStore } from '../../EditorStoreProvider.js';
import { useApplicationStore } from '@finos/legend-application';
import { FileSystemViewer } from './FileSystemViewer.js';
const NewFileGenerationModal = observer((props) => {
    const { elementGenerationState, currentElementState } = props;
    const applicationStore = useApplicationStore();
    const isReadOnly = currentElementState.isReadOnly;
    const element = currentElementState.element;
    const mappingPackage = guaranteeType(element.package, Package);
    const nameRef = useRef(null);
    const editorStore = useEditorStore();
    const defaultFileGenerationName = `${element.path}_${elementGenerationState.fileGenerationType}`;
    const [servicePath, setServicePath] = useState(defaultFileGenerationName);
    const [packagePath, serviceName] = resolvePackagePathAndElementName(servicePath, mappingPackage.path);
    const close = () => elementGenerationState.setShowNewFileGenerationModal(false);
    const handleEnter = () => nameRef.current?.focus();
    const elementAlreadyExists = editorStore.graphManagerState.graph.allOwnElements
        .map((el) => el.path)
        .includes(packagePath + ELEMENT_PATH_DELIMITER + serviceName);
    const promoteToFileGeneration = async () => {
        if (servicePath && !isReadOnly && !elementAlreadyExists) {
            await flowResult(elementGenerationState.promoteToFileGeneration(packagePath, serviceName));
            close();
        }
    };
    const changeValue = (event) => setServicePath(event.target.value);
    return (_jsx(Dialog, { open: elementGenerationState.showNewFileGenerationModal, onClose: close, slotProps: {
            transition: {
                onEnter: handleEnter,
            },
            paper: {
                classes: {
                    root: 'search-modal__inner-container',
                },
            },
        }, children: _jsxs("form", { onSubmit: (event) => {
                event.preventDefault();
                promoteToFileGeneration().catch(applicationStore.alertUnhandledError);
            }, className: "modal search-modal modal--dark", children: [_jsx("div", { className: "modal__title", children: "Promote file generation specification" }), _jsxs("div", { className: "input-group", children: [_jsx("input", { ref: nameRef, className: "input input--dark input-group__input", disabled: isReadOnly, value: servicePath, spellCheck: false, onChange: changeValue, placeholder: `Enter a name, use ${ELEMENT_PATH_DELIMITER} to create new package(s) for the service` }), elementAlreadyExists && (_jsx("div", { className: "input-group__error-message", children: "Element with same path already exists" }))] }), _jsx("div", { className: "search-modal__actions", children: _jsx("button", { className: "btn btn--dark", disabled: isReadOnly || elementAlreadyExists, children: "Create" }) })] }) }));
});
export const ElementGenerationEditor = observer((props) => {
    const { elementGenerationState, currentElementState } = props;
    const applicationStore = useApplicationStore();
    const isReadOnly = currentElementState.isReadOnly;
    const leaveElementGenerationView = () => currentElementState.setGenerationModeState(undefined);
    useEffect(() => {
        flowResult(elementGenerationState.regenerate()).catch(applicationStore.alertUnhandledError);
    }, [applicationStore, currentElementState, elementGenerationState]);
    return (_jsxs("div", { className: "panel element-generation-editor", children: [_jsx("div", { className: "panel__header element-generation-editor__header", children: _jsx("div", { className: "panel__header__title", children: _jsxs("button", { className: "panel__header__action element-generation-editor__leave-btn", tabIndex: -1, onClick: leaveElementGenerationView, title: "Leave element generation view mode", children: [_jsx(ArrowCircleLeftIcon, {}), " exit generation view"] }) }) }), _jsx("div", { className: "panel__content element-generation-editor__content", children: _jsxs("div", { className: "file-generation-editor", children: [_jsxs(ResizablePanelGroup, { orientation: "vertical", children: [_jsx(ResizablePanel, { size: 300, minSize: 300, className: "file-generation-editor__split-pane", children: _jsx(FileGenerationConfigurationEditor, { fileGenerationState: elementGenerationState.fileGenerationState, isReadOnly: isReadOnly, elementGenerationState: elementGenerationState }) }), _jsx(ResizablePanelSplitter, { children: _jsx(ResizablePanelSplitterLine, { color: "var(--color-dark-grey-200)" }) }), _jsx(ResizablePanel, { children: _jsx(FileSystemViewer, { generatedFileState: elementGenerationState.fileGenerationState }) })] }), _jsx(NewFileGenerationModal, { elementGenerationState: elementGenerationState, currentElementState: currentElementState })] }) })] }));
});
//# sourceMappingURL=ElementGenerationEditor.js.map