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 { Dialog, PanelLoadingIndicator, Modal, PanelFormActions, PanelFormTextField, PanelDivider, PanelForm, PanelFormBooleanField, CustomSelectorInput, } from '@finos/legend-art';
import { flowResult } from 'mobx';
import { WorkspaceType, Patch } from '@finos/legend-server-sdlc';
import { useApplicationStore } from '@finos/legend-application';
import { LEGEND_STUDIO_DOCUMENTATION_KEY } from '../../__lib__/LegendStudioDocumentation.js';
import { DEFAULT_WORKSPACE_SOURCE, useWorkspaceSetupStore, } from './WorkspaceSetup.js';
import { DocumentationLink } from '@finos/legend-lego/application';
export const CreateWorkspaceModal = observer((props) => {
    const { selectedProject } = props;
    const setupStore = useWorkspaceSetupStore();
    const applicationStore = useApplicationStore();
    const workspaceNameInputRef = useRef(null);
    const [workspaceName, setWorkspaceName] = useState('');
    const [isGroupWorkspace, setIsGroupWorkspace] = useState(true);
    const [patchOptions] = useState([
        {
            label: DEFAULT_WORKSPACE_SOURCE,
            value: DEFAULT_WORKSPACE_SOURCE,
        },
    ].concat(setupStore.patches.map((p) => ({
        label: `patch/${p.patchReleaseVersionId.id}`,
        value: p,
    }))));
    const [selectedPatchOption, setSelectedPatchOption] = useState({
        label: DEFAULT_WORKSPACE_SOURCE,
        value: DEFAULT_WORKSPACE_SOURCE,
    });
    const onPatchOptionChange = (val) => {
        if ((val !== null || selectedPatchOption !== null) &&
            (!val ||
                !selectedPatchOption ||
                val.value !== selectedPatchOption.value)) {
            setSelectedPatchOption(val);
        }
    };
    const workspaceAlreadyExists = Boolean(setupStore.workspaces.find((workspace) => workspace.workspaceId === workspaceName &&
        ((workspace.workspaceType === WorkspaceType.GROUP &&
            isGroupWorkspace) ||
            (workspace.workspaceType === WorkspaceType.USER &&
                !isGroupWorkspace)) &&
        ((!workspace.source &&
            !(selectedPatchOption?.value instanceof Patch)) ||
            (selectedPatchOption?.value instanceof Patch &&
                workspace.source ===
                    selectedPatchOption.value.patchReleaseVersionId.id))));
    const createWorkspace = () => {
        if (workspaceName &&
            setupStore.currentProjectConfigurationStatus?.isConfigured) {
            flowResult(setupStore.createWorkspace(selectedProject.projectId, selectedPatchOption?.value instanceof Patch
                ? selectedPatchOption.value.patchReleaseVersionId.id
                : undefined, workspaceName, isGroupWorkspace ? WorkspaceType.GROUP : WorkspaceType.USER)).catch(applicationStore.alertUnhandledError);
        }
    };
    const toggleGroupWorkspace = () => {
        setIsGroupWorkspace(!isGroupWorkspace);
    };
    const handleEnter = () => {
        workspaceNameInputRef.current?.focus();
    };
    const onClose = () => {
        setupStore.setShowCreateWorkspaceModal(false);
    };
    return (_jsx(Dialog, { open: setupStore.showCreateWorkspaceModal, onClose: onClose, slotProps: {
            transition: {
                onEnter: handleEnter,
            },
            paper: {
                classes: { root: 'search-modal__inner-container' },
            },
        }, classes: { container: 'search-modal__container' }, children: _jsxs(Modal, { darkMode: !applicationStore.layoutService.TEMPORARY__isLightColorThemeEnabled, className: "workspace-setup__create-workspace-modal", children: [_jsxs("div", { className: "modal__title", children: ["Create Workspace", _jsx(DocumentationLink, { documentationKey: LEGEND_STUDIO_DOCUMENTATION_KEY.CREATE_WORKSPACE })] }), _jsxs("form", { onSubmit: (event) => {
                        event.preventDefault();
                        createWorkspace();
                    }, children: [_jsx(PanelLoadingIndicator, { isLoading: setupStore.createWorkspaceState.isInProgress }), _jsxs(PanelForm, { className: "workspace-setup__create-workspace-modal__form workspace-setup__create-workspace-modal__form__workspace", children: [_jsx(PanelDivider, {}), _jsx(PanelFormTextField, { ref: workspaceNameInputRef, name: "Workspace Name", isReadOnly: setupStore.createWorkspaceState.isInProgress ||
                                        setupStore.createOrImportProjectState.isInProgress, placeholder: "MyWorkspace", fullWidth: true, className: "workspace-setup__create-workspace-modal__form__workspace-name__input", value: workspaceName, update: (val) => setWorkspaceName(val ?? ''), errorMessage: workspaceAlreadyExists
                                        ? 'Workspace with same name already exists '
                                        : '' }), _jsxs("div", { className: "workspace-setup__create-workspace-modal__form__workspace--source", children: [_jsx("div", { className: "workspace-setup__create-workspace-modal__form__workspace--source__label", children: "Workspace Source" }), _jsx(CustomSelectorInput, { className: "workspace-setup__create-workspace-modal__form__workspace--source__selector", options: patchOptions, onChange: onPatchOptionChange, value: selectedPatchOption, isClearable: true, escapeClearsValue: true, darkMode: !applicationStore.layoutService
                                                .TEMPORARY__isLightColorThemeEnabled })] }), _jsx(PanelFormBooleanField, { name: "Group Workspace", prompt: "Group workspaces can be accessed by all users in the project", value: isGroupWorkspace, isReadOnly: false, update: toggleGroupWorkspace })] }), _jsx(PanelFormActions, { children: _jsx("button", { disabled: setupStore.createWorkspaceState.isInProgress ||
                                    setupStore.createOrImportProjectState.isInProgress ||
                                    !workspaceName, className: "btn btn--dark", children: "Create" }) })] })] }) }));
});
//# sourceMappingURL=CreateWorkspaceModal.js.map