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 { useEffect } from 'react';
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 { clsx, PanelLoadingIndicator, ResizablePanel, ResizablePanelGroup, ResizablePanelSplitter, ResizablePanelSplitterLine, CloudDownloadIcon, RefreshIcon, InfoCircleIcon, 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 { generateReviewRoute } from '../../../__lib__/LegendStudioNavigation.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 { useLegendStudioApplicationStore } from '../../LegendStudioFrameworkProvider.js';
export const WorkspaceUpdater = observer(() => {
    const editorStore = useEditorStore();
    const applicationStore = useLegendStudioApplicationStore();
    const sdlcState = editorStore.sdlcState;
    const currentTabState = editorStore.tabManagerState.currentTab;
    const workspaceUpdaterState = editorStore.workspaceUpdaterState;
    // Actions
    const updateWorkspace = () => {
        editorStore.localChangesState.alertUnsavedChanges(() => {
            flowResult(workspaceUpdaterState.updateWorkspace()).catch(applicationStore.alertUnhandledError);
        });
    };
    const refreshWorkspaceUpdater = applicationStore.guardUnhandledError(() => flowResult(workspaceUpdaterState.refreshWorkspaceUpdater()));
    const isDispatchingAction = workspaceUpdaterState.isUpdatingWorkspace ||
        sdlcState.isCheckingIfWorkspaceIsOutdated ||
        workspaceUpdaterState.isRefreshingWorkspaceUpdater;
    // Conflicts
    const conflicts = editorStore.changeDetectionState.potentialWorkspaceUpdateConflicts;
    const isSelectedConflict = (conflict) => currentTabState instanceof EntityChangeConflictEditorState &&
        conflict.entityPath === currentTabState.entityPath;
    const openPotentialConflict = (conflict) => () => workspaceUpdaterState.openPotentialWorkspaceUpdateConflict(conflict);
    // Changes
    const changes = editorStore.changeDetectionState.aggregatedProjectLatestChanges;
    const changesWithoutConflicts = changes.filter((change) => !conflicts
        .map((conflict) => conflict.entityPath)
        .includes(change.entityPath));
    const isSelectedDiff = (diff) => currentTabState instanceof EntityDiffViewState &&
        diff.oldPath === currentTabState.fromEntityPath &&
        diff.newPath === currentTabState.toEntityPath;
    const openChange = (diff) => () => workspaceUpdaterState.openProjectLatestChange(diff);
    // Committed Reviews
    const commitedReviews = workspaceUpdaterState.committedReviewsBetweenWorkspaceBaseAndProjectLatest;
    // since the project latest changes can be affected by other users, we refresh it more proactively
    useEffect(() => {
        flowResult(workspaceUpdaterState.refreshWorkspaceUpdater()).catch(applicationStore.alertUnhandledError);
    }, [applicationStore, workspaceUpdaterState]);
    return (_jsxs("div", { className: "panel workspace-updater", children: [_jsxs("div", { className: "panel__header side-bar__header", children: [_jsx("div", { className: "panel__header__title workspace-updater__header__title", children: _jsx("div", { className: "panel__header__title__content side-bar__header__title__content", children: "WORKSPACE UPDATER" }) }), _jsxs("div", { className: "panel__header__actions side-bar__header__actions", children: [_jsx("button", { className: clsx('panel__header__action side-bar__header__action workspace-updater__refresh-btn', {
                                    'workspace-updater__refresh-btn--loading': workspaceUpdaterState.isRefreshingWorkspaceUpdater,
                                }), onClick: refreshWorkspaceUpdater, disabled: isDispatchingAction, tabIndex: -1, title: "Refresh", children: _jsx(RefreshIcon, {}) }), _jsx("button", { className: "panel__header__action side-bar__header__action workspace-updater__update-btn", onClick: updateWorkspace, disabled: !sdlcState.isWorkspaceOutdated || isDispatchingAction, tabIndex: -1, title: "Update workspace", children: _jsx(CloudDownloadIcon, {}) })] })] }), _jsxs("div", { className: "panel__content side-bar__content", children: [_jsx(PanelLoadingIndicator, { isLoading: isDispatchingAction }), _jsxs(ResizablePanelGroup, { orientation: "horizontal", children: [_jsx(ResizablePanel, { size: 400, minSize: 28, children: _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 to project since the revision the workspace is created.\nPotential workspace update conflicts are also shown if they exist', 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 })] }), _jsxs(PanelContent, { children: [conflicts
                                                    .toSorted((a, b) => a.entityName.localeCompare(b.entityName))
                                                    .map((conflict) => (_jsx(EntityChangeConflictSideBarItem, { conflict: conflict, isSelected: isSelectedConflict(conflict), openConflict: openPotentialConflict(conflict) }, `conflict-${conflict.entityPath}`))), Boolean(conflicts.length) &&
                                                    Boolean(changesWithoutConflicts.length) && (_jsx("div", { className: "diff-panel__item-section-separator" })), changesWithoutConflicts
                                                    .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-100)" }) }), _jsx(ResizablePanel, { minSize: 28, children: _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: "COMMITTED REVIEWS" }), _jsx("div", { className: "side-bar__panel__title__info", title: "All committed reviews in the project since the revision the workspace is created", children: _jsx(InfoCircleIcon, {}) })] }), _jsx("div", { className: "side-bar__panel__header__changes-count", "data-testid": LEGEND_STUDIO_TEST_ID.SIDEBAR_PANEL_HEADER__CHANGES_COUNT, children: commitedReviews.length })] }), _jsx(PanelContent, { children: commitedReviews.map((review) => (_jsx("button", { className: "side-bar__panel__item workspace-updater__review__link", title: "See review", tabIndex: -1, onClick: () => applicationStore.navigationService.navigator.visitAddress(applicationStore.navigationService.navigator.generateAddress(generateReviewRoute(review.projectId, review.id))), children: _jsxs("div", { className: "workspace-updater__review", children: [_jsx("span", { className: "workspace-updater__review__name", children: review.title }), _jsx("span", { className: "workspace-updater__review__info", children: review.author.name })] }) }, review.id))) })] }) })] })] })] }));
});
//# sourceMappingURL=WorkspaceUpdater.js.map