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 { EntityDiffViewState } from '../../../stores/editor/editor-state/entity-diff-editor-state/EntityDiffViewState.js';
import { EntityDiffSideBarItem } from '../editor-group/diff-editor/EntityDiffView.js';
import { PanelLoadingIndicator, CloudDownloadIcon, CheckIcon, TimesIcon, InfoCircleIcon, BanIcon, PanelContent, } from '@finos/legend-art';
import { EntityChangeConflictSideBarItem } from '../editor-group/diff-editor/EntityChangeConflictEditor.js';
import { EntityChangeConflictEditorState } from '../../../stores/editor/editor-state/entity-diff-editor-state/EntityChangeConflictEditorState.js';
import { LEGEND_STUDIO_TEST_ID } from '../../../__lib__/LegendStudioTesting.js';
import { flowResult } from 'mobx';
import { entityDiffSorter } from '../../../stores/editor/EditorSDLCState.js';
import { useEditorStore } from '../EditorStoreProvider.js';
import { useApplicationStore } from '@finos/legend-application';
export const WorkspaceUpdateConflictResolver = observer(() => {
    const editorStore = useEditorStore();
    const applicationStore = useApplicationStore();
    const sdlcState = editorStore.sdlcState;
    const currentTabState = editorStore.tabManagerState.currentTab;
    const conflictResolutionState = editorStore.conflictResolutionState;
    // Actions
    const isRunningTask = editorStore.workspaceUpdaterState.isUpdatingWorkspace ||
        conflictResolutionState.isInitializingConflictResolution ||
        conflictResolutionState.isAcceptingConflictResolution ||
        conflictResolutionState.isDiscardingConflictResolutionChanges ||
        conflictResolutionState.isAbortingConflictResolution;
    const updateWorkspace = applicationStore.guardUnhandledError(() => flowResult(editorStore.workspaceUpdaterState.updateWorkspace()));
    const accept = applicationStore.guardUnhandledError(() => flowResult(conflictResolutionState.acceptConflictResolution()));
    const discardChanges = applicationStore.guardUnhandledError(() => flowResult(conflictResolutionState.discardConflictResolutionChanges()));
    const abort = applicationStore.guardUnhandledError(() => flowResult(conflictResolutionState.abortConflictResolution()));
    // Conflicts
    const conflicts = conflictResolutionState.conflicts;
    const isSelectedConflict = (conflict) => currentTabState instanceof EntityChangeConflictEditorState &&
        conflict.entityPath === currentTabState.entityPath;
    const openConflict = (conflict) => () => conflictResolutionState.openConflict(conflict);
    // Changes
    const changes = conflictResolutionState.changes;
    const openChange = (diff) => () => conflictResolutionState.openConflictResolutionChange(diff);
    const isSelectedDiff = (diff) => currentTabState instanceof EntityDiffViewState &&
        diff.oldPath === currentTabState.fromEntityPath &&
        diff.newPath === currentTabState.toEntityPath;
    return (_jsxs("div", { className: "panel conflict-resolution", children: [_jsxs("div", { className: "panel__header side-bar__header", children: [_jsx("div", { className: "panel__header__title conflict-resolution__header__title", children: _jsx("div", { className: "panel__header__title__content side-bar__header__title__content", children: "CONFLICT RESOLUTION" }) }), _jsxs("div", { className: "panel__header__actions side-bar__header__actions", children: [_jsx("button", { className: "panel__header__action side-bar__header__action conflict-resolution__update-btn", onClick: updateWorkspace, disabled: !sdlcState.isWorkspaceOutdated || isRunningTask, tabIndex: -1, title: "Update workspace", children: _jsx(CloudDownloadIcon, {}) }), _jsx("button", { className: "panel__header__action side-bar__header__action", onClick: accept, disabled: !conflictResolutionState.hasResolvedAllConflicts || isRunningTask, tabIndex: -1, title: "Accept resolution", children: _jsx(CheckIcon, {}) }), _jsx("button", { className: "panel__header__action side-bar__header__action", onClick: discardChanges, disabled: isRunningTask, tabIndex: -1, title: "Discard all changes made in the workspace", children: _jsx(TimesIcon, {}) }), _jsx("button", { className: "panel__header__action side-bar__header__action", onClick: abort, disabled: isRunningTask, tabIndex: -1, title: "Abort conflict resolution", children: _jsx(BanIcon, {}) })] })] }), _jsxs("div", { className: "panel__content side-bar__content", children: [_jsx(PanelLoadingIndicator, { isLoading: isRunningTask }), _jsxs("div", { className: "panel side-bar__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: "side-bar__panel__title__info", title: "All changes made in the workspace applied on top of the project revision the workspace is updated to", children: _jsx(InfoCircleIcon, {}) })] }), _jsx("div", { className: "side-bar__panel__header__changes-count", "data-testid": LEGEND_STUDIO_TEST_ID.SIDEBAR_PANEL_HEADER__CHANGES_COUNT, children: changes.length +
                                            (conflictResolutionState.hasResolvedAllConflicts
                                                ? 0
                                                : conflicts.length) })] }), _jsxs(PanelContent, { children: [!conflictResolutionState.hasResolvedAllConflicts && (_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)))] })), conflictResolutionState.hasResolvedAllConflicts &&
                                        changes
                                            .toSorted(entityDiffSorter)
                                            .map((diff) => (_jsx(EntityDiffSideBarItem, { diff: diff, isSelected: isSelectedDiff(diff), openDiff: openChange(diff) }, diff.key)))] })] })] })] }));
});
//# sourceMappingURL=WorkspaceUpdateConflictResolver.js.map