
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
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, MutableDisposable } from '../../../../base/common/lifecycle.js';
import { escape } from '../../../../base/common/strings.js';
import { localize } from '../../../../nls.js';
import { createActionViewItem } from '../../../../platform/actions/browser/menuEntryActionViewItem.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { registerIcon } from '../../../../platform/theme/common/iconRegistry.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.service.js';
import { MarshalledId } from '../../../../base/common/marshallingIds.js';
import { StandardMouseEvent } from '../../../../base/browser/mouseEvent.js';
import { CommentCommandId } from '../common/commentCommandIds.js';

const collapseIcon = registerIcon("review-comment-collapse", Codicon.chevronUp, ( localize(8606, "Icon to collapse a review comment.")));
const COLLAPSE_ACTION_CLASS = "expand-review-action " + ThemeIcon.asClassName(collapseIcon);
const DELETE_ACTION_CLASS = "expand-review-action " + ThemeIcon.asClassName(Codicon.trashcan);
function threadHasComments(comments) {
    return !!comments && comments.length > 0;
}
let CommentThreadHeader = 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);
        const collapseClass = threadHasComments(this._commentThread.comments) ? COLLAPSE_ACTION_CLASS : DELETE_ACTION_CLASS;
        this._collapseAction = ( new Action(CommentCommandId.Hide, ( localize(8607, "Collapse")), collapseClass, true, () => this._delegate.collapse()));
        if (!threadHasComments(this._commentThread.comments)) {
            const commentsChanged = this._register(( new MutableDisposable()));
            commentsChanged.value = this._commentThread.onDidChangeComments(() => {
                if (threadHasComments(this._commentThread.comments)) {
                    this._collapseAction.class = COLLAPSE_ACTION_CLASS;
                    commentsChanged.clear();
                }
            });
        }
        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 = ( localize(8608, "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);
        if (!actions.length) {
            return;
        }
        const event = ( new StandardMouseEvent(getWindow(this._headElement), e));
        if (!this._contextMenuActionRunner) {
            this._contextMenuActionRunner = this._register(( new ActionRunner()));
        }
        this._contextMenuService.showContextMenu({
            getAnchor: () => event,
            getActions: () => actions,
            actionRunner: this._contextMenuActionRunner,
            getActionsContext: () => {
                return {
                    commentControlHandle: this._commentThread.controllerHandle,
                    commentThreadHandle: this._commentThread.commentThreadHandle,
                    $mid: MarshalledId.CommentThread
                };
            }
        });
    }
};
CommentThreadHeader = ( __decorate([( __param(4, IContextKeyService)), ( __param(5, IInstantiationService)), ( __param(6, IContextMenuService))], CommentThreadHeader));

export { CommentThreadHeader };
