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 { observer } from 'mobx-react-lite';
import { clsx, ControlledDropdownMenu, ContextMenu, TimesIcon, MenuContentItem, MenuContent, } from '@finos/legend-art';
import { filterByType } from '@finos/legend-shared';
import { EntityDiffViewState, DIFF_VIEW_MODE, } from '../../stores/editor/editor-state/entity-diff-editor-state/EntityDiffViewState.js';
import { EntityDiffView } from '../editor/editor-group/diff-editor/EntityDiffView.js';
import { useEditorStore } from '../editor/EditorStoreProvider.js';
import { forwardRef } from 'react';
import { EditorDiffViewerState } from '../../stores/editor/editor-state/diff-viewer-state/EditorDiffViewerState.js';
import { ProjectConfigDiffView } from '../editor/editor-group/diff-editor/ProjectConfigDiffView.js';
import { ProjectConfigurationDiffEditorState } from '../../stores/editor/editor-state/diff-viewer-state/ProjectConfigurationDiffEditorState.js';
const ProjectReviewerPanelSplashScreen = () => (_jsx("div", { className: "workspace-review-panel__splash-screen" }));
const ProjectReviewerPanelHeaderTabContextMenu = observer(forwardRef(function ReviewPanelHeaderTabContextMenu(props, ref) {
    const { editorState } = props;
    const editorStore = useEditorStore();
    const close = () => editorStore.tabManagerState.closeTab(editorState);
    const closeOthers = () => editorStore.tabManagerState.closeAllTabs();
    const closeAll = () => editorStore.tabManagerState.closeAllTabs();
    return (_jsxs(MenuContent, { ref: ref, children: [_jsx(MenuContentItem, { onClick: close, children: "Close" }), _jsx(MenuContentItem, { disabled: editorStore.tabManagerState.tabs.length < 2, onClick: closeOthers, children: "Close Others" }), _jsx(MenuContentItem, { onClick: closeAll, children: "Close All" })] }));
}));
export const ProjectReviewerPanel = observer(() => {
    const editorStore = useEditorStore();
    const currentTabState = editorStore.tabManagerState.currentTab instanceof EditorDiffViewerState
        ? editorStore.tabManagerState.currentTab
        : undefined;
    const openedTabStates = editorStore.tabManagerState.tabs.filter(filterByType(EditorDiffViewerState));
    const closeTab = (diffState) => (event) => editorStore.tabManagerState.closeTab(diffState);
    const closeTabOnMiddleClick = (editorState) => (event) => {
        if (event.nativeEvent.button === 1) {
            editorStore.tabManagerState.closeTab(editorState);
        }
    };
    const switchTab = (editorState) => () => editorStore.tabManagerState.openTab(editorState);
    const switchViewMode = (mode) => () => {
        if (currentTabState instanceof EntityDiffViewState) {
            currentTabState.setDiffMode(mode);
        }
    };
    if (!currentTabState) {
        return _jsx(ProjectReviewerPanelSplashScreen, {});
    }
    const renderActiveTabState = (tab) => {
        if (tab instanceof EntityDiffViewState) {
            return _jsx(EntityDiffView, { entityDiffViewState: tab });
        }
        else if (tab instanceof ProjectConfigurationDiffEditorState) {
            return _jsx(ProjectConfigDiffView, { configDiffState: tab });
        }
        return null;
    };
    return (_jsxs("div", { className: "panel workspace-review-panel", children: [_jsxs("div", { className: "panel__header workspace-review-panel__header", children: [_jsx("div", { className: "workspace-review-panel__header__tabs", children: openedTabStates.map((editorState) => (_jsx("div", { className: clsx('workspace-review-panel__header__tab', {
                                'workspace-review-panel__header__tab--active': editorState === currentTabState,
                            }), onMouseUp: closeTabOnMiddleClick(editorState), children: _jsxs(ContextMenu, { className: "workspace-review-panel__header__tab__content", content: _jsx(ProjectReviewerPanelHeaderTabContextMenu, { editorState: editorState }), children: [_jsx("button", { className: "workspace-review-panel__header__tab__element__name", tabIndex: -1, onClick: switchTab(editorState), children: editorState.label }), _jsx("button", { className: "workspace-review-panel__header__tab__close-btn", onClick: closeTab(editorState), tabIndex: -1, children: _jsx(TimesIcon, {}) })] }) }, editorState.uuid))) }), _jsx("div", { className: "workspace-review-panel__header__actions", children: currentTabState instanceof EntityDiffViewState && (_jsx(ControlledDropdownMenu, { className: "workspace-review-panel__element-view__type", title: "View as...", content: _jsxs("div", { className: "workspace-review-panel__element-view__options", children: [_jsx("div", { className: "workspace-review-panel__element-view__option", onClick: switchViewMode(DIFF_VIEW_MODE.GRAMMAR), children: DIFF_VIEW_MODE.GRAMMAR }), _jsx("div", { className: "workspace-review-panel__element-view__option", onClick: switchViewMode(DIFF_VIEW_MODE.JSON), children: DIFF_VIEW_MODE.JSON })] }), menuProps: {
                                anchorOrigin: { vertical: 'bottom', horizontal: 'right' },
                                transformOrigin: { vertical: 'top', horizontal: 'right' },
                            }, children: _jsx("div", { className: "workspace-review-panel__element-view__type__label", children: currentTabState.diffMode }) })) })] }), _jsx("div", { className: "panel__content workspace-review-panel__content", children: renderActiveTabState(currentTabState) })] }));
});
//# sourceMappingURL=ProjectReviewerPanel.js.map