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 { observer } from 'mobx-react-lite';
import { useEditorStore } from '../EditorStoreProvider.js';
import { noop } from '@finos/legend-shared';
import { clsx, ResizablePanel, ResizablePanelGroup, ResizablePanelSplitter, ResizablePanelSplitterLine, InfoCircleIcon, TimesIcon, Dialog, PanelContent, Modal, ModalBody, ModalFooter, ModalHeader, ModalFooterButton, } from '@finos/legend-art';
import { EntityChangeConflictEditor, EntityChangeConflictSideBarItem, } from '../editor-group/diff-editor/EntityChangeConflictEditor.js';
import { EntityChangeConflictEditorState } from '../../../stores/editor/editor-state/entity-diff-editor-state/EntityChangeConflictEditorState.js';
import { EntityDiffSideBarItem, EntityDiffView, } from '../editor-group/diff-editor/EntityDiffView.js';
import { EntityDiffViewState } from '../../../stores/editor/editor-state/entity-diff-editor-state/EntityDiffViewState.js';
import { entityDiffSorter } from '../../../stores/editor/EditorSDLCState.js';
import { flowResult } from 'mobx';
import { useApplicationStore } from '@finos/legend-application';
export const WorkspaceSyncConflictResolver = observer(() => {
    const editorStore = useEditorStore();
    const applicationStore = useApplicationStore();
    const updateState = editorStore.localChangesState.workspaceSyncState;
    const updateConflictState = updateState.workspaceSyncConflictResolutionState;
    const currentDiffEditorState = updateConflictState.currentDiffEditorState;
    const openMergedEditorStates = updateConflictState.openMergedEditorStates;
    const conflicts = updateConflictState.pendingConflicts;
    const hadResolvedAllConflicts = !conflicts.length;
    const changes = updateConflictState.changes;
    const openConflict = (conflict) => () => {
        updateConflictState.openConflict(conflict);
    };
    const isSelectedConflict = (conflict) => currentDiffEditorState instanceof EntityChangeConflictEditorState &&
        conflict.entityPath === currentDiffEditorState.entityPath;
    const closeTabOnMiddleClick = (conflictState) => (event) => {
        if (event.nativeEvent.button === 1) {
            updateConflictState.closeConflict(conflictState);
        }
    };
    const closeTab = (conflictState) => (event) => updateConflictState.closeConflict(conflictState);
    const openTab = (conflictState) => () => updateConflictState.openState(conflictState);
    const isSelectedDiff = (diff) => currentDiffEditorState instanceof EntityDiffViewState &&
        diff.oldPath === currentDiffEditorState.fromEntityPath &&
        diff.newPath === currentDiffEditorState.toEntityPath;
    const openChange = (diff) => () => {
        updateConflictState.openConflictResolutionChange(diff);
    };
    const applyResolutions = () => {
        if (!conflicts.length) {
            flowResult(updateState.applyResolutionChanges()).catch(applicationStore.alertUnhandledError);
        }
    };
    const abort = () => updateState.resetConflictState();
    return (_jsx(Dialog, { open: updateConflictState.showModal, onClose: noop, classes: { container: 'search-modal__container' }, slotProps: {
            paper: {
                classes: { root: 'search-modal__inner-container' },
            },
        }, children: _jsxs(Modal, { darkMode: !applicationStore.layoutService.TEMPORARY__isLightColorThemeEnabled, className: "editor-modal query-builder__dialog", children: [_jsx(ModalHeader, { title: "Resolve Merge Conflicts" }), _jsx(ModalBody, { children: _jsxs(ResizablePanelGroup, { orientation: "vertical", children: [_jsx(ResizablePanel, { size: 350, minSize: 350, children: _jsxs("div", { className: "panel explorer", children: [_jsx("div", { className: "panel__header workspace-sync-conflict-resolver__changes__header", children: _jsx("div", { className: "panel__header__title", children: _jsx("div", { className: "panel__header__title__content workspace-sync-conflict-resolver__changes__header__title__content", children: "All Changes" }) }) }), _jsx("div", { className: "panel__content explorer__content__container", children: _jsx(ResizablePanelGroup, { orientation: "horizontal", children: _jsxs("div", { className: "panel workspace-sync-conflict-resolver__changes__panel", children: [_jsxs("div", { className: "panel__header", children: [_jsxs("div", { className: "panel__header__title", children: [_jsx("div", { className: "panel__header__title__content", children: "CHANGES" }), _jsx("div", { className: "workspace-sync-conflict-resolver__changes__panel__title__info", title: "All local changes that have not been yet pushed with the server", children: _jsx(InfoCircleIcon, {}) })] }), _jsx("div", { className: "workspace-sync-conflict-resolver__changes__panel__header__changes-count", children: changes.length + conflicts.length })] }), _jsxs(PanelContent, { children: [!hadResolvedAllConflicts && (_jsxs(_Fragment, { children: [conflicts
                                                                            .toSorted((a, b) => a.entityName.localeCompare(b.entityName))
                                                                            .map((conflict) => (_jsx(EntityChangeConflictSideBarItem, { conflict: conflict, isSelected: isSelectedConflict(conflict), openConflict: openConflict(conflict) }, `conflict-${conflict.entityPath}`))), Boolean(conflicts.length) &&
                                                                            Boolean(changes.length) && (_jsx("div", { className: "diff-panel__item-section-separator" })), changes.toSorted(entityDiffSorter).map((diff) => (_jsx(EntityDiffSideBarItem, { diff: diff, isSelected: isSelectedDiff(diff), openDiff: openChange(diff) }, diff.key)))] })), hadResolvedAllConflicts &&
                                                                    changes
                                                                        .toSorted(entityDiffSorter)
                                                                        .map((diff) => (_jsx(EntityDiffSideBarItem, { diff: diff, isSelected: isSelectedDiff(diff), openDiff: openChange(diff) }, diff.key)))] })] }) }) })] }) }), _jsx(ResizablePanelSplitter, { children: _jsx(ResizablePanelSplitterLine, { color: "var(--color-dark-grey-200)" }) }), _jsx(ResizablePanel, { children: _jsxs("div", { className: "workspace-sync-conflict-resolver", children: [_jsx("div", { className: "workspace-sync-conflict-resolver__header", children: _jsx("div", { className: "workspace-sync-conflict-resolver__header__tabs", children: openMergedEditorStates.map((mergedState) => (_jsx("div", { className: clsx('workspace-sync-conflict-resolver__header__tab', {
                                                        'workspace-sync-conflict-resolver__header__tab--active': mergedState === currentDiffEditorState,
                                                    }), onMouseUp: closeTabOnMiddleClick(mergedState), children: _jsxs("div", { className: "editor-group__header__tab__content", children: [_jsx("button", { className: "workspace-sync-conflict-resolver__header__tab__label", tabIndex: -1, onClick: openTab(mergedState), title: mergedState.label, children: mergedState.label }), _jsx("button", { className: "workspace-sync-conflict-resolver__header__tab__close-btn", onClick: closeTab(mergedState), tabIndex: -1, title: "Close", children: _jsx(TimesIcon, {}) })] }) }, mergedState.uuid))) }) }), _jsxs(PanelContent, { children: [updateConflictState.currentDiffEditorState instanceof
                                                    EntityChangeConflictEditorState && (_jsx(EntityChangeConflictEditor, { conflictEditorState: updateConflictState.currentDiffEditorState })), updateConflictState.currentDiffEditorState instanceof
                                                    EntityDiffViewState && (_jsx(EntityDiffView, { entityDiffViewState: updateConflictState.currentDiffEditorState }))] })] }) })] }) }), _jsxs(ModalFooter, { children: [_jsx(ModalFooterButton, { text: "Apply Resolutions", title: "Apply Resolutions", disabled: Boolean(conflicts.length), onClick: applyResolutions }), _jsx(ModalFooterButton, { text: "Abort", title: "Aborts", onClick: abort, type: "secondary" })] })] }) }));
});
//# sourceMappingURL=WorkspaceSyncConflictResolver.js.map