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 { DIFF_VIEW_MODE, } from '../../../../stores/editor/editor-state/entity-diff-editor-state/EntityDiffViewState.js';
import { clsx, GoToFileIcon, CompareIcon } from '@finos/legend-art';
import { getPrettyLabelForRevision } from '../../../../stores/editor/editor-state/entity-diff-editor-state/EntityDiffEditorState.js';
import { flowResult } from 'mobx';
import { EntityChangeType } from '@finos/legend-server-sdlc';
import { useEditorStore } from '../../EditorStoreProvider.js';
import { useApplicationStore } from '@finos/legend-application';
import { sortObjectKeys } from '@finos/legend-shared';
import { CodeDiffView, JSONDiffView } from '@finos/legend-lego/code-editor';
import { CODE_EDITOR_LANGUAGE } from '@finos/legend-code-editor';
const getDiffItemTitle = (diff) => {
    switch (diff.entityChangeType) {
        case EntityChangeType.RENAME:
            return `${diff.newPath ? `${diff.newPath} \u2022 ` : ''}Renamed`;
        case EntityChangeType.DELETE:
            return `${diff.oldPath ? `${diff.oldPath} \u2022 ` : ''}Deleted`;
        case EntityChangeType.CREATE:
            return `${diff.newPath ? `${diff.newPath} \u2022 ` : ''}Created`;
        case EntityChangeType.MODIFY:
            return `${diff.newPath ? `${diff.newPath} \u2022 ` : ''}Modified`;
        default:
            return undefined; // no title
    }
};
export const EntityDiffSideBarItem = observer((props) => {
    const { diff, isSelected, openDiff } = props;
    return (_jsxs("button", { className: clsx('side-bar__panel__item', {
            'side-bar__panel__item--selected': isSelected,
        }), tabIndex: -1, title: getDiffItemTitle(diff), onClick: openDiff, children: [_jsxs("div", { className: "diff-panel__item__info", children: [_jsx("span", { className: clsx('diff-panel__item__info-name', `diff-panel__item__info-name--${diff.entityChangeType.toLowerCase()}`), children: diff.entityName }), _jsx("span", { className: "diff-panel__item__info-path", children: diff.entityPath })] }), _jsx("div", { className: clsx('diff-panel__item__type', `diff-panel__item__type--${diff.entityChangeType.toLowerCase()}`), children: diff.getChangeTypeIcon() })] }));
});
export const EntityDiffView = observer((props) => {
    const editorStore = useEditorStore();
    const applicationStore = useApplicationStore();
    const diffEditorState = props.entityDiffViewState;
    const { fromEntity, fromGrammarText, toEntity, toGrammarText, fromRevision, toRevision, } = diffEditorState;
    const goToElement = () => {
        if (diffEditorState.element) {
            editorStore.graphEditorMode.openElement(diffEditorState.element);
        }
    };
    useEffect(() => {
        diffEditorState.refresh();
    }, [diffEditorState]);
    useEffect(() => {
        flowResult(diffEditorState.getFromGrammar()).catch(applicationStore.alertUnhandledError);
    }, [applicationStore, diffEditorState, diffEditorState.fromEntity]);
    useEffect(() => {
        flowResult(diffEditorState.getToGrammar()).catch(applicationStore.alertUnhandledError);
    }, [applicationStore, diffEditorState, diffEditorState.toEntity]);
    return (_jsxs("div", { className: "entity-diff-view", children: [_jsxs("div", { className: "entity-diff-view__header", children: [_jsxs("div", { className: "entity-diff-view__header__info", children: [_jsxs("div", { className: "entity-diff-view__header__info__revision-summary", children: [_jsx("div", { className: "entity-diff-view__header__info__revision-summary__revision", children: getPrettyLabelForRevision(fromRevision) }), _jsx("div", { className: "entity-diff-view__header__info__revision-summary__icon", children: _jsx(CompareIcon, {}) }), _jsx("div", { className: "entity-diff-view__header__info__revision-summary__revision", children: getPrettyLabelForRevision(toRevision) })] }), _jsx("div", { className: "entity-diff-view__header__info__revision-summary__icon", children: _jsx(CompareIcon, {}) }), _jsx("div", { className: "entity-diff-view__header__info__revision-summary__revision", children: getPrettyLabelForRevision(toRevision) })] }), _jsx("div", { className: "entity-diff-view__header__actions", children: _jsx("button", { className: "entity-diff-view__header__action", disabled: !diffEditorState.element, tabIndex: -1, onClick: goToElement, title: "Go to element", children: _jsx(GoToFileIcon, {}) }) })] }), _jsxs("div", { className: "entity-diff-view__content", children: [diffEditorState.diffMode === DIFF_VIEW_MODE.GRAMMAR && (_jsx(CodeDiffView, { language: CODE_EDITOR_LANGUAGE.PURE, from: fromGrammarText, to: toGrammarText })), diffEditorState.diffMode === DIFF_VIEW_MODE.JSON && (_jsx(JSONDiffView, { from: fromEntity?.content
                            ? sortObjectKeys(fromEntity.content)
                            : undefined, to: toEntity?.content ? sortObjectKeys(toEntity.content) : undefined }))] })] }));
});
//# sourceMappingURL=EntityDiffView.js.map