import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { localizeWithPath } from '../../../../nls.js';
import { $, append, addDisposableListener, EventType, scheduleAtNextAnimationFrame, getWindow, getContentWidth, getContentHeight } from '../../../../base/browser/dom.js';
import { CommentMode } from '../../../../editor/common/languages.js';
import { ActionBar } from '../../../../base/browser/ui/actionbar/actionbar.js';
import { Separator, ActionRunner, Action } from '../../../../base/common/actions.js';
import { Disposable, dispose } from '../../../../base/common/lifecycle.js';
import { URI } from '../../../../base/common/uri.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { ICommentService } from './commentService.service.js';
import { MIN_EDITOR_HEIGHT, SimpleCommentEditor, calculateEditorHeight } from './simpleCommentEditor.js';
import { Selection } from '../../../../editor/common/core/selection.js';
import { Emitter } from '../../../../base/common/event.js';
import { INotificationService } from '../../../../platform/notification/common/notification.service.js';
import { ToolBar } from '../../../../base/browser/ui/toolbar/toolbar.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.service.js';
import { ToggleReactionsAction, ReactionAction, ReactionActionViewItem } from './reactionsAction.js';
import { MenuItemAction, SubmenuItemAction, MenuId } from '../../../../platform/actions/common/actions.js';
import { MenuEntryActionViewItem, SubmenuEntryActionViewItem } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { CommentFormActions } from './commentFormActions.js';
import { MOUSE_CURSOR_TEXT_CSS_CLASS_NAME } from '../../../../base/browser/ui/mouseCursor/mouseCursor.js';
import { ActionViewItem } from '../../../../base/browser/ui/actionbar/actionViewItems.js';
import { DropdownMenuActionViewItem } from '../../../../base/browser/ui/dropdown/dropdownActionViewItem.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { TimestampWidget } from './timestamp.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { Scrollable } from '../../../../base/common/scrollable.js';
import { SmoothScrollableElement } from '../../../../base/browser/ui/scrollbar/scrollableElement.js';
import { DomEmitter } from '../../../../base/browser/event.js';
import { CommentContextKeys } from '../common/commentContextKeys.js';
import { FileAccess, Schemas } from '../../../../base/common/network.js';
import { COMMENTS_SECTION } from '../common/commentsConfiguration.js';
import { StandardMouseEvent } from '../../../../base/browser/mouseEvent.js';
import { IAccessibilityService } from '../../../../platform/accessibility/common/accessibility.service.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import { IHoverService } from '../../../../platform/hover/browser/hover.service.js';
import { ITextModelService } from '../../../../editor/common/services/resolverService.js';
const _moduleId = "vs/workbench/contrib/comments/browser/commentNode";
class CommentsActionRunner extends ActionRunner {
    async runAction(action, context) {
        await action.run(...context);
    }
}
let CommentNode = class CommentNode extends Disposable {
    get domNode() {
        return this._domNode;
    }
    constructor(parentEditor, commentThread, comment, pendingEdit, owner, resource, parentThread, markdownRenderer, instantiationService, commentService, notificationService, contextMenuService, contextKeyService, configurationService, hoverService, accessibilityService, keybindingService, textModelService) {
        super();
        this.parentEditor = parentEditor;
        this.commentThread = commentThread;
        this.comment = comment;
        this.pendingEdit = pendingEdit;
        this.owner = owner;
        this.resource = resource;
        this.parentThread = parentThread;
        this.markdownRenderer = markdownRenderer;
        this.instantiationService = instantiationService;
        this.commentService = commentService;
        this.notificationService = notificationService;
        this.contextMenuService = contextMenuService;
        this.configurationService = configurationService;
        this.hoverService = hoverService;
        this.accessibilityService = accessibilityService;
        this.keybindingService = keybindingService;
        this.textModelService = textModelService;
        this._editAction = null;
        this._commentEditContainer = null;
        this._commentEditor = null;
        this._commentEditorDisposables = [];
        this._commentEditorModel = null;
        this._editorHeight = MIN_EDITOR_HEIGHT;
        this._commentFormActions = null;
        this._commentEditorActions = null;
        this._onDidClick = ( (new Emitter()));
        this.isEditing = false;
        this._domNode = $('div.review-comment');
        this._contextKeyService = contextKeyService.createScoped(this._domNode);
        this._commentContextValue = CommentContextKeys.commentContext.bindTo(this._contextKeyService);
        if (this.comment.contextValue) {
            this._commentContextValue.set(this.comment.contextValue);
        }
        this._commentMenus = this.commentService.getCommentMenus(this.owner);
        this._domNode.tabIndex = -1;
        this._avatar = append(this._domNode, $('div.avatar-container'));
        this.updateCommentUserIcon(this.comment.userIconPath);
        this._commentDetailsContainer = append(this._domNode, $('.review-comment-contents'));
        this.createHeader(this._commentDetailsContainer);
        this._body = document.createElement(`div`);
        this._body.classList.add('comment-body', MOUSE_CURSOR_TEXT_CSS_CLASS_NAME);
        if (configurationService.getValue(COMMENTS_SECTION)?.maxHeight !== false) {
            this._body.classList.add('comment-body-max-height');
        }
        this.createScroll(this._commentDetailsContainer, this._body);
        this.updateCommentBody(this.comment.body);
        if (this.comment.commentReactions && this.comment.commentReactions.length && this.comment.commentReactions.filter(reaction => !!reaction.count).length) {
            this.createReactionsContainer(this._commentDetailsContainer);
        }
        this._domNode.setAttribute('aria-label', `${comment.userName}, ${this.commentBodyValue}`);
        this._domNode.setAttribute('role', 'treeitem');
        this._clearTimeout = null;
        this._register(addDisposableListener(this._domNode, EventType.CLICK, () => this.isEditing || this._onDidClick.fire(this)));
        this._register(addDisposableListener(this._domNode, EventType.CONTEXT_MENU, e => {
            return this.onContextMenu(e);
        }));
        if (pendingEdit) {
            this.switchToEditMode();
        }
        this._register(this.accessibilityService.onDidChangeScreenReaderOptimized(() => {
            this.toggleToolbarHidden(true);
        }));
        this.activeCommentListeners();
    }
    activeCommentListeners() {
        this._register(addDisposableListener(this._domNode, EventType.FOCUS_IN, () => {
            this.commentService.setActiveCommentAndThread(this.owner, { thread: this.commentThread, comment: this.comment });
        }, true));
    }
    createScroll(container, body) {
        this._scrollable = ( (new Scrollable({
            forceIntegerValues: true,
            smoothScrollDuration: 125,
            scheduleAtNextAnimationFrame: cb => scheduleAtNextAnimationFrame(getWindow(container), cb)
        })));
        this._scrollableElement = this._register(( (new SmoothScrollableElement(body, {
            horizontal: 3 ,
            vertical: 3
        }, this._scrollable))));
        this._register(this._scrollableElement.onScroll(e => {
            if (e.scrollLeftChanged) {
                body.scrollLeft = e.scrollLeft;
            }
            if (e.scrollTopChanged) {
                body.scrollTop = e.scrollTop;
            }
        }));
        const onDidScrollViewContainer = this._register(( (new DomEmitter(body, 'scroll')))).event;
        this._register(onDidScrollViewContainer(_ => {
            const position = this._scrollableElement.getScrollPosition();
            const scrollLeft = Math.abs(body.scrollLeft - position.scrollLeft) <= 1 ? undefined : body.scrollLeft;
            const scrollTop = Math.abs(body.scrollTop - position.scrollTop) <= 1 ? undefined : body.scrollTop;
            if (scrollLeft !== undefined || scrollTop !== undefined) {
                this._scrollableElement.setScrollPosition({ scrollLeft, scrollTop });
            }
        }));
        container.appendChild(this._scrollableElement.getDomNode());
    }
    updateCommentBody(body) {
        this._body.innerText = '';
        this._md = undefined;
        this._plainText = undefined;
        if (typeof body === 'string') {
            this._plainText = append(this._body, $('.comment-body-plainstring'));
            this._plainText.innerText = body;
        }
        else {
            this._md = this.markdownRenderer.render(body).element;
            this._body.appendChild(this._md);
        }
    }
    updateCommentUserIcon(userIconPath) {
        this._avatar.textContent = '';
        if (userIconPath) {
            const img = append(this._avatar, $('img.avatar'));
            img.src = ( (FileAccess.uriToBrowserUri(URI.revive(userIconPath)).toString(true)));
            img.onerror = _ => img.remove();
        }
    }
    get onDidClick() {
        return this._onDidClick.event;
    }
    createTimestamp(container) {
        this._timestamp = append(container, $('span.timestamp-container'));
        this.updateTimestamp(this.comment.timestamp);
    }
    updateTimestamp(raw) {
        if (!this._timestamp) {
            return;
        }
        const timestamp = raw !== undefined ? ( (new Date(raw))) : undefined;
        if (!timestamp) {
            this._timestampWidget?.dispose();
        }
        else {
            if (!this._timestampWidget) {
                this._timestampWidget = ( (new TimestampWidget(this.configurationService, this.hoverService, this._timestamp, timestamp)));
                this._register(this._timestampWidget);
            }
            else {
                this._timestampWidget.setTimestamp(timestamp);
            }
        }
    }
    createHeader(commentDetailsContainer) {
        const header = append(commentDetailsContainer, $(`div.comment-title.${MOUSE_CURSOR_TEXT_CSS_CLASS_NAME}`));
        const infoContainer = append(header, $('comment-header-info'));
        const author = append(infoContainer, $('strong.author'));
        author.innerText = this.comment.userName;
        this.createTimestamp(infoContainer);
        this._isPendingLabel = append(infoContainer, $('span.isPending'));
        if (this.comment.label) {
            this._isPendingLabel.innerText = this.comment.label;
        }
        else {
            this._isPendingLabel.innerText = '';
        }
        this._actionsToolbarContainer = append(header, $('.comment-actions'));
        this.toggleToolbarHidden(true);
        this.createActionsToolbar();
    }
    toggleToolbarHidden(hidden) {
        if (hidden && !this.accessibilityService.isScreenReaderOptimized()) {
            this._actionsToolbarContainer.classList.add('hidden');
        }
        else {
            this._actionsToolbarContainer.classList.remove('hidden');
        }
    }
    getToolbarActions(menu) {
        const contributedActions = menu.getActions({ shouldForwardArgs: true });
        const primary = [];
        const secondary = [];
        const result = { primary, secondary };
        fillInActions(contributedActions, result, false, g => /^inline/.test(g));
        return result;
    }
    get commentNodeContext() {
        return [{
                thread: this.commentThread,
                commentUniqueId: this.comment.uniqueIdInThread,
                $mid: 10
            },
            {
                commentControlHandle: this.commentThread.controllerHandle,
                commentThreadHandle: this.commentThread.commentThreadHandle,
                $mid: 7
            }];
    }
    createToolbar() {
        this.toolbar = ( (new ToolBar(this._actionsToolbarContainer, this.contextMenuService, {
            actionViewItemProvider: (action, options) => {
                if (action.id === ToggleReactionsAction.ID) {
                    return (
                         (new DropdownMenuActionViewItem(action, action.menuActions, this.contextMenuService, {
                            ...options,
                            actionViewItemProvider: (action, options) => this.actionViewItemProvider(action, options),
                            actionRunner: this.actionRunner,
                            classNames: ['toolbar-toggle-pickReactions', ...ThemeIcon.asClassNameArray(Codicon.reactions)],
                            anchorAlignmentProvider: () => 1
                        }))
                    );
                }
                return this.actionViewItemProvider(action, options);
            },
            orientation: 0
        })));
        this.toolbar.context = this.commentNodeContext;
        this.toolbar.actionRunner = ( (new CommentsActionRunner()));
        this.registerActionBarListeners(this._actionsToolbarContainer);
        this._register(this.toolbar);
    }
    createActionsToolbar() {
        const actions = [];
        const hasReactionHandler = this.commentService.hasReactionHandler(this.owner);
        if (hasReactionHandler) {
            const toggleReactionAction = this.createReactionPicker(this.comment.commentReactions || []);
            actions.push(toggleReactionAction);
        }
        const menu = this._commentMenus.getCommentTitleActions(this.comment, this._contextKeyService);
        this._register(menu);
        this._register(menu.onDidChange(e => {
            const { primary, secondary } = this.getToolbarActions(menu);
            if (!this.toolbar && (primary.length || secondary.length)) {
                this.createToolbar();
            }
            this.toolbar.setActions(primary, secondary);
        }));
        const { primary, secondary } = this.getToolbarActions(menu);
        actions.push(...primary);
        if (actions.length || secondary.length) {
            this.createToolbar();
            this.toolbar.setActions(actions, secondary);
        }
    }
    actionViewItemProvider(action, options) {
        if (action.id === ToggleReactionsAction.ID) {
            options = { label: false, icon: true };
        }
        else {
            options = { label: false, icon: true };
        }
        if (action.id === ReactionAction.ID) {
            const item = ( (new ReactionActionViewItem(action)));
            return item;
        }
        else if (action instanceof MenuItemAction) {
            return this.instantiationService.createInstance(MenuEntryActionViewItem, action, { hoverDelegate: options.hoverDelegate });
        }
        else if (action instanceof SubmenuItemAction) {
            return this.instantiationService.createInstance(SubmenuEntryActionViewItem, action, options);
        }
        else {
            const item = ( (new ActionViewItem({}, action, options)));
            return item;
        }
    }
    async submitComment() {
        if (this._commentEditor && this._commentFormActions) {
            await this._commentFormActions.triggerDefaultAction();
            this.pendingEdit = undefined;
        }
    }
    createReactionPicker(reactionGroup) {
        const toggleReactionAction = this._register(( (new ToggleReactionsAction(() => {
            toggleReactionActionViewItem?.show();
        }, ( localizeWithPath(_moduleId, 0, "Toggle Reaction"))))));
        let reactionMenuActions = [];
        if (reactionGroup && reactionGroup.length) {
            reactionMenuActions = ( (reactionGroup.map((reaction) => {
                return (
                     (new Action(
                        `reaction.command.${reaction.label}`,
                        `${reaction.label}`,
                        '',
                        true,
                        async () => {
                            try {
                                await this.commentService.toggleReaction(this.owner, this.resource, this.commentThread, this.comment, reaction);
                            }
                            catch (e) {
                                const error = e.message
                                    ? ( localizeWithPath(_moduleId, 1, "Toggling the comment reaction failed: {0}.", e.message))
                                    : ( localizeWithPath(_moduleId, 2, "Toggling the comment reaction failed"));
                                this.notificationService.error(error);
                            }
                        }
                    ))
                );
            })));
        }
        toggleReactionAction.menuActions = reactionMenuActions;
        const toggleReactionActionViewItem = ( (new DropdownMenuActionViewItem(
            toggleReactionAction,
            toggleReactionAction.menuActions,
            this.contextMenuService,
            {
                actionViewItemProvider: (action, options) => {
                    if (action.id === ToggleReactionsAction.ID) {
                        return toggleReactionActionViewItem;
                    }
                    return this.actionViewItemProvider(action, options);
                },
                actionRunner: this.actionRunner,
                classNames: 'toolbar-toggle-pickReactions',
                anchorAlignmentProvider: () => 1
            }
        )));
        return toggleReactionAction;
    }
    createReactionsContainer(commentDetailsContainer) {
        this._reactionActionsContainer = append(commentDetailsContainer, $('div.comment-reactions'));
        this._reactionsActionBar = ( (new ActionBar(this._reactionActionsContainer, {
            actionViewItemProvider: (action, options) => {
                if (action.id === ToggleReactionsAction.ID) {
                    return (
                         (new DropdownMenuActionViewItem(action, action.menuActions, this.contextMenuService, {
                            actionViewItemProvider: (action, options) => this.actionViewItemProvider(action, options),
                            actionRunner: this.actionRunner,
                            classNames: ['toolbar-toggle-pickReactions', ...ThemeIcon.asClassNameArray(Codicon.reactions)],
                            anchorAlignmentProvider: () => 1
                        }))
                    );
                }
                return this.actionViewItemProvider(action, options);
            }
        })));
        this._register(this._reactionsActionBar);
        const hasReactionHandler = this.commentService.hasReactionHandler(this.owner);
        if (hasReactionHandler) {
            const toggleReactionAction = this.createReactionPicker(this.comment.commentReactions || []);
            this._reactionsActionBar.push(toggleReactionAction, { label: false, icon: true });
        }
    }
    get commentBodyValue() {
        return (typeof this.comment.body === 'string') ? this.comment.body : this.comment.body.value;
    }
    async createCommentEditor(editContainer) {
        const container = append(editContainer, $('.edit-textarea'));
        this._commentEditor = this.instantiationService.createInstance(SimpleCommentEditor, container, SimpleCommentEditor.getEditorOptions(this.configurationService), this._contextKeyService, this.parentThread);
        const resource = ( (URI.from({
            scheme: Schemas.commentsInput,
            path: `/commentinput-${this.comment.uniqueIdInThread}-${Date.now()}.md`
        })));
        const modelRef = await this.textModelService.createModelReference(resource);
        this._commentEditorModel = modelRef;
        this._commentEditor.setModel(this._commentEditorModel.object.textEditorModel);
        this._commentEditor.setValue(this.pendingEdit ?? this.commentBodyValue);
        this.pendingEdit = undefined;
        this._commentEditor.layout({ width: container.clientWidth - 14, height: this._editorHeight });
        this._commentEditor.focus();
        scheduleAtNextAnimationFrame(getWindow(editContainer), () => {
            this._commentEditor.layout({ width: container.clientWidth - 14, height: this._editorHeight });
            this._commentEditor.focus();
        });
        const lastLine = this._commentEditorModel.object.textEditorModel.getLineCount();
        const lastColumn = this._commentEditorModel.object.textEditorModel.getLineLength(lastLine) + 1;
        this._commentEditor.setSelection(( (new Selection(lastLine, lastColumn, lastLine, lastColumn))));
        const commentThread = this.commentThread;
        commentThread.input = {
            uri: this._commentEditor.getModel().uri,
            value: this.commentBodyValue
        };
        this.commentService.setActiveEditingCommentThread(commentThread);
        this.commentService.setActiveCommentAndThread(this.owner, { thread: commentThread, comment: this.comment });
        this._commentEditorDisposables.push(this._commentEditor.onDidFocusEditorWidget(() => {
            commentThread.input = {
                uri: this._commentEditor.getModel().uri,
                value: this.commentBodyValue
            };
            this.commentService.setActiveEditingCommentThread(commentThread);
            this.commentService.setActiveCommentAndThread(this.owner, { thread: commentThread, comment: this.comment });
        }));
        this._commentEditorDisposables.push(this._commentEditor.onDidChangeModelContent(e => {
            if (commentThread.input && this._commentEditor && this._commentEditor.getModel().uri === commentThread.input.uri) {
                const newVal = this._commentEditor.getValue();
                if (newVal !== commentThread.input.value) {
                    const input = commentThread.input;
                    input.value = newVal;
                    commentThread.input = input;
                    this.commentService.setActiveEditingCommentThread(commentThread);
                    this.commentService.setActiveCommentAndThread(this.owner, { thread: commentThread, comment: this.comment });
                }
            }
        }));
        this.calculateEditorHeight();
        this._register((this._commentEditorModel.object.textEditorModel.onDidChangeContent(() => {
            if (this._commentEditor && this.calculateEditorHeight()) {
                this._commentEditor.layout({ height: this._editorHeight, width: this._commentEditor.getLayoutInfo().width });
                this._commentEditor.render(true);
            }
        })));
        this._register(this._commentEditor);
        this._register(this._commentEditorModel);
    }
    calculateEditorHeight() {
        if (this._commentEditor) {
            const newEditorHeight = calculateEditorHeight(this.parentEditor, this._commentEditor, this._editorHeight);
            if (newEditorHeight !== this._editorHeight) {
                this._editorHeight = newEditorHeight;
                return true;
            }
        }
        return false;
    }
    getPendingEdit() {
        const model = this._commentEditor?.getModel();
        if (model && model.getValueLength() > 0) {
            return model.getValue();
        }
        return undefined;
    }
    removeCommentEditor() {
        this.isEditing = false;
        if (this._editAction) {
            this._editAction.enabled = true;
        }
        this._body.classList.remove('hidden');
        this._commentEditorModel?.dispose();
        dispose(this._commentEditorDisposables);
        this._commentEditorDisposables = [];
        this._commentEditor?.dispose();
        this._commentEditor = null;
        this._commentEditContainer.remove();
    }
    layout(widthInPixel) {
        const editorWidth = widthInPixel !== undefined ? widthInPixel - 72  : (this._commentEditor?.getLayoutInfo().width ?? 0);
        this._commentEditor?.layout({ width: editorWidth, height: this._editorHeight });
        const scrollWidth = this._body.scrollWidth;
        const width = getContentWidth(this._body);
        const scrollHeight = this._body.scrollHeight;
        const height = getContentHeight(this._body) + 4;
        this._scrollableElement.setScrollDimensions({ width, scrollWidth, height, scrollHeight });
    }
    async switchToEditMode() {
        if (this.isEditing) {
            return;
        }
        this.isEditing = true;
        this._body.classList.add('hidden');
        this._commentEditContainer = append(this._commentDetailsContainer, $('.edit-container'));
        await this.createCommentEditor(this._commentEditContainer);
        const formActions = append(this._commentEditContainer, $('.form-actions'));
        const otherActions = append(formActions, $('.other-actions'));
        this.createCommentWidgetFormActions(otherActions);
        const editorActions = append(formActions, $('.editor-actions'));
        this.createCommentWidgetEditorActions(editorActions);
    }
    createCommentWidgetFormActions(container) {
        const menus = this.commentService.getCommentMenus(this.owner);
        const menu = menus.getCommentActions(this.comment, this._contextKeyService);
        this._register(menu);
        this._register(menu.onDidChange(() => {
            this._commentFormActions?.setActions(menu);
        }));
        this._commentFormActions = ( (new CommentFormActions(this.keybindingService, this._contextKeyService, container, (action) => {
            const text = this._commentEditor.getValue();
            action.run({
                thread: this.commentThread,
                commentUniqueId: this.comment.uniqueIdInThread,
                text: text,
                $mid: 11
            });
            this.removeCommentEditor();
        })));
        this._register(this._commentFormActions);
        this._commentFormActions.setActions(menu);
    }
    createCommentWidgetEditorActions(container) {
        const menus = this.commentService.getCommentMenus(this.owner);
        const menu = menus.getCommentEditorActions(this._contextKeyService);
        this._register(menu);
        this._register(menu.onDidChange(() => {
            this._commentEditorActions?.setActions(menu);
        }));
        this._commentEditorActions = ( (new CommentFormActions(this.keybindingService, this._contextKeyService, container, (action) => {
            const text = this._commentEditor.getValue();
            action.run({
                thread: this.commentThread,
                commentUniqueId: this.comment.uniqueIdInThread,
                text: text,
                $mid: 11
            });
            this._commentEditor?.focus();
        })));
        this._register(this._commentEditorActions);
        this._commentEditorActions.setActions(menu, true);
    }
    setFocus(focused, visible = false) {
        if (focused) {
            this._domNode.focus();
            this.toggleToolbarHidden(false);
            this._actionsToolbarContainer.classList.add('tabfocused');
            this._domNode.tabIndex = 0;
            if (this.comment.mode === CommentMode.Editing) {
                this._commentEditor?.focus();
            }
        }
        else {
            if (this._actionsToolbarContainer.classList.contains('tabfocused') && !this._actionsToolbarContainer.classList.contains('mouseover')) {
                this.toggleToolbarHidden(true);
                this._domNode.tabIndex = -1;
            }
            this._actionsToolbarContainer.classList.remove('tabfocused');
        }
    }
    registerActionBarListeners(actionsContainer) {
        this._register(addDisposableListener(this._domNode, 'mouseenter', () => {
            this.toggleToolbarHidden(false);
            actionsContainer.classList.add('mouseover');
        }));
        this._register(addDisposableListener(this._domNode, 'mouseleave', () => {
            if (actionsContainer.classList.contains('mouseover') && !actionsContainer.classList.contains('tabfocused')) {
                this.toggleToolbarHidden(true);
            }
            actionsContainer.classList.remove('mouseover');
        }));
    }
    async update(newComment) {
        if (newComment.body !== this.comment.body) {
            this.updateCommentBody(newComment.body);
        }
        if (this.comment.userIconPath && newComment.userIconPath && (( (( (URI.from(this.comment.userIconPath))).toString())) !== ( (( (URI.from(newComment.userIconPath))).toString())))) {
            this.updateCommentUserIcon(newComment.userIconPath);
        }
        const isChangingMode = newComment.mode !== undefined && newComment.mode !== this.comment.mode;
        this.comment = newComment;
        if (isChangingMode) {
            if (newComment.mode === CommentMode.Editing) {
                await this.switchToEditMode();
            }
            else {
                this.removeCommentEditor();
            }
        }
        if (newComment.label) {
            this._isPendingLabel.innerText = newComment.label;
        }
        else {
            this._isPendingLabel.innerText = '';
        }
        this._reactionActionsContainer?.remove();
        this._reactionsActionBar?.clear();
        if (this.comment.commentReactions && ( (this.comment.commentReactions.some(reaction => !!reaction.count)))) {
            this.createReactionsContainer(this._commentDetailsContainer);
        }
        if (this.comment.contextValue) {
            this._commentContextValue.set(this.comment.contextValue);
        }
        else {
            this._commentContextValue.reset();
        }
        if (this.comment.timestamp) {
            this.updateTimestamp(this.comment.timestamp);
        }
    }
    onContextMenu(e) {
        const event = ( (new StandardMouseEvent(getWindow(this._domNode), e)));
        this.contextMenuService.showContextMenu({
            getAnchor: () => event,
            menuId: MenuId.CommentThreadCommentContext,
            menuActionOptions: { shouldForwardArgs: true },
            contextKeyService: this._contextKeyService,
            actionRunner: ( (new CommentsActionRunner())),
            getActionsContext: () => {
                return this.commentNodeContext;
            },
        });
    }
    focus() {
        this.domNode.focus();
        if (!this._clearTimeout) {
            this.domNode.classList.add('focus');
            this._clearTimeout = setTimeout(() => {
                this.domNode.classList.remove('focus');
            }, 3000);
        }
    }
    dispose() {
        super.dispose();
        dispose(this._commentEditorDisposables);
    }
};
CommentNode = ( (__decorate([
    ( (__param(8, IInstantiationService))),
    ( (__param(9, ICommentService))),
    ( (__param(10, INotificationService))),
    ( (__param(11, IContextMenuService))),
    ( (__param(12, IContextKeyService))),
    ( (__param(13, IConfigurationService))),
    ( (__param(14, IHoverService))),
    ( (__param(15, IAccessibilityService))),
    ( (__param(16, IKeybindingService))),
    ( (__param(17, ITextModelService)))
], CommentNode)));
function fillInActions(groups, target, useAlternativeActions, isPrimaryGroup = group => group === 'navigation') {
    for (const tuple of groups) {
        let [group, actions] = tuple;
        if (useAlternativeActions) {
            actions = ( (actions.map(a => (a instanceof MenuItemAction) && !!a.alt ? a.alt : a)));
        }
        if (isPrimaryGroup(group)) {
            const to = Array.isArray(target) ? target : target.primary;
            to.unshift(...actions);
        }
        else {
            const to = Array.isArray(target) ? target : target.secondary;
            if (to.length > 0) {
                to.push(( (new Separator())));
            }
            to.push(...actions);
        }
    }
}
export { CommentNode };
