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 { useState, useRef } from 'react';
import { observer } from 'mobx-react-lite';
import { guaranteeType } from '@finos/legend-shared';
import { Dialog, ModalTitle, PanelDivider, PanelFormSection, PanelFormValidatedTextField, } from '@finos/legend-art';
import { useEditorStore } from '../../EditorStoreProvider.js';
import { useApplicationStore } from '@finos/legend-application';
import { ELEMENT_PATH_DELIMITER, resolvePackagePathAndElementName, Package, } from '@finos/legend-graph';
export const NewServiceModal = observer((props) => {
    const { isReadOnly, mapping, close, promoteToService, showModal } = props;
    const editorStore = useEditorStore();
    const applicationStore = useApplicationStore();
    const mappingPackage = guaranteeType(mapping.package, Package);
    const nameRef = useRef(null);
    const defaultServiceName = `${mapping.path}Service`;
    const [servicePath, setServicePath] = useState(defaultServiceName);
    const [packagePath, serviceName] = resolvePackagePathAndElementName(servicePath, mappingPackage.path);
    const [isValid, setIsValid] = useState(true);
    const handleEnter = () => nameRef.current?.focus();
    const create = () => {
        if (servicePath && !isReadOnly && isValid) {
            promoteToService(packagePath, serviceName)
                .then(() => close())
                .catch(applicationStore.alertUnhandledError);
        }
    };
    const validateElementDoesNotAlreadyExist = (newServiceName) => {
        const elementAlreadyExists = editorStore.graphManagerState.graph.allOwnElements
            .map((s) => s.path)
            .includes(packagePath + ELEMENT_PATH_DELIMITER + newServiceName);
        if (!elementAlreadyExists) {
            return undefined;
        }
        else {
            return 'Element with same path already exists';
        }
    };
    const changeValue = (value) => {
        setServicePath(value);
    };
    return (_jsx(Dialog, { open: showModal, onClose: close, slotProps: {
            transition: {
                onEnter: handleEnter,
            },
            paper: {
                classes: {
                    root: 'search-modal__inner-container',
                },
            },
        }, children: _jsxs("form", { onSubmit: (event) => {
                event.preventDefault();
                create();
            }, className: "modal search-modal modal--dark", children: [_jsx(ModalTitle, { title: "Promote to Service" }), _jsx(PanelFormValidatedTextField, { ref: nameRef, isReadOnly: isReadOnly ?? false, update: (value) => {
                        changeValue(value ?? '');
                    }, validate: validateElementDoesNotAlreadyExist, onValidate: (issue) => setIsValid(!issue), value: servicePath, placeholder: `Enter a name, use ${ELEMENT_PATH_DELIMITER} to create new package(s) for the service` }), _jsx(PanelDivider, {}), _jsx(PanelFormSection, { children: _jsx("div", { className: "search-modal__actions", children: _jsx("button", { className: "btn btn--dark", disabled: Boolean(isReadOnly) || !isValid, onClick: create, children: "Create" }) }) })] }) }));
});
//# sourceMappingURL=NewServiceModal.js.map