import { $, append, addDisposableListener, EventType, getWindow } from '../../../../base/browser/dom.js';
import { ActionBar } from '../../../../base/browser/ui/actionbar/actionbar.js';
import { Action, ActionRunner } from '../../../../base/common/actions.js';
import { Codicon } from '../../../../base/common/codicons.js';
import { Disposable, toDisposable } from '../../../../base/common/lifecycle.js';
import { escape } from '../../../../base/common/strings.js';
import { localizeWithPath } from '../../../../nls.js';
import { createActionViewItem } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';
import { registerIcon } from '../../../../platform/theme/common/iconRegistry.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { MarshalledId } from '../../../../base/common/marshallingIds.js';
import { StandardMouseEvent } from '../../../../base/browser/mouseEvent.js';
const _moduleId = "vs/workbench/contrib/comments/browser/commentThreadHeader";
const collapseIcon = registerIcon('review-comment-collapse', Codicon.chevronUp, ( localizeWithPath(_moduleId, 0, 'Icon to collapse a review comment.')));
const COLLAPSE_ACTION_CLASS = 'expand-review-action ' + ThemeIcon.asClassName(collapseIcon);
class CommentThreadHeader extends Disposable {
    constructor(container, _delegate, _commentMenus, _commentThread, _contextKeyService, instantiationService, _contextMenuService) {
        super();
        this._delegate = _delegate;
        this._commentMenus = _commentMenus;
        this._commentThread = _commentThread;
        this._contextKeyService = _contextKeyService;
        this.instantiationService = instantiationService;
        this._contextMenuService = _contextMenuService;
        this._headElement = $('.head');
        container.appendChild(this._headElement);
        this._register(toDisposable(() => this._headElement.remove()));
        this._fillHead();
    }
    _fillHead() {
        const titleElement = append(this._headElement, $('.review-title'));
        this._headingLabel = append(titleElement, $('span.filename'));
        this.createThreadLabel();
        const actionsContainer = append(this._headElement, $('.review-actions'));
        this._actionbarWidget = ( (new ActionBar(actionsContainer, {
            actionViewItemProvider: createActionViewItem.bind(undefined, this.instantiationService)
        })));
        this._register(this._actionbarWidget);
        this._collapseAction = ( (new Action('review.expand', ( localizeWithPath(_moduleId, 1, "Collapse")), COLLAPSE_ACTION_CLASS, true, () => this._delegate.collapse())));
        const menu = this._commentMenus.getCommentThreadTitleActions(this._contextKeyService);
        this._register(menu);
        this.setActionBarActions(menu);
        this._register(menu);
        this._register(menu.onDidChange(e => {
            this.setActionBarActions(menu);
        }));
        this._register(addDisposableListener(this._headElement, EventType.CONTEXT_MENU, e => {
            return this.onContextMenu(e);
        }));
        this._actionbarWidget.context = this._commentThread;
    }
    setActionBarActions(menu) {
        const groups = menu.getActions({ shouldForwardArgs: true }).reduce((r, [, actions]) => [...r, ...actions], []);
        this._actionbarWidget.clear();
        this._actionbarWidget.push([...groups, this._collapseAction], { label: false, icon: true });
    }
    updateCommentThread(commentThread) {
        this._commentThread = commentThread;
        this._actionbarWidget.context = this._commentThread;
        this.createThreadLabel();
    }
    createThreadLabel() {
        let label;
        label = this._commentThread.label;
        if (label === undefined) {
            if (!(this._commentThread.comments && this._commentThread.comments.length)) {
                label = ( localizeWithPath(_moduleId, 2, "Start discussion"));
            }
        }
        if (label) {
            this._headingLabel.textContent = escape(label);
            this._headingLabel.setAttribute('aria-label', label);
        }
    }
    updateHeight(headHeight) {
        this._headElement.style.height = `${headHeight}px`;
        this._headElement.style.lineHeight = this._headElement.style.height;
    }
    onContextMenu(e) {
        const actions = ( (this._commentMenus.getCommentThreadTitleContextActions(this._contextKeyService).getActions({ shouldForwardArgs: true }).map((value) => value[1]))).flat();
        if (!actions.length) {
            return;
        }
        const event = ( (new StandardMouseEvent(getWindow(this._headElement), e)));
        this._contextMenuService.showContextMenu({
            getAnchor: () => event,
            getActions: () => actions,
            actionRunner: ( (new ActionRunner())),
            getActionsContext: () => {
                return {
                    commentControlHandle: this._commentThread.controllerHandle,
                    commentThreadHandle: this._commentThread.commentThreadHandle,
                    $mid: MarshalledId.CommentThread
                };
            },
        });
    }
}
export { CommentThreadHeader };
