import { __decorate, __param } from '../../../../../../../../../external/tslib/tslib.es6.js';
import { coalesce } from '../../../../../../base/common/arrays.js';
import { DisposableStore, MutableDisposable } from '../../../../../../base/common/lifecycle.js';
import { EDITOR_FONT_DEFAULTS } from '../../../../../../editor/common/config/editorOptions.js';
import { IConfigurationService } from '../../../../../../platform/configuration/common/configuration.service.js';
import { IContextKeyService } from '../../../../../../platform/contextkey/common/contextkey.service.js';
import { IInstantiationService } from '../../../../../../platform/instantiation/common/instantiation.js';
import { IThemeService } from '../../../../../../platform/theme/common/themeService.service.js';
import { ICommentService } from '../../../../comments/browser/commentService.service.js';
import { CommentThreadWidget } from '../../../../comments/browser/commentThreadWidget.js';
import { CellContentPart } from '../cellPart.js';
import { CellKind } from '../../../common/notebookCommon.js';
let CellComments = class CellComments extends CellContentPart {
    constructor(notebookEditor, container, contextKeyService, themeService, commentService, configurationService, instantiationService) {
        super();
        this.notebookEditor = notebookEditor;
        this.container = container;
        this.contextKeyService = contextKeyService;
        this.themeService = themeService;
        this.commentService = commentService;
        this.configurationService = configurationService;
        this.instantiationService = instantiationService;
        this._commentThreadDisposables = this._register(( new DisposableStore()));
        this.container.classList.add('review-widget');
        this._register(this._commentThreadWidget = ( new MutableDisposable()));
        this._register(this.themeService.onDidColorThemeChange(this._applyTheme, this));
        this._applyTheme();
    }
    async initialize(element) {
        if (this.currentElement === element) {
            return;
        }
        this.currentElement = element;
        await this._updateThread();
    }
    async _createCommentTheadWidget(owner, commentThread) {
        this._commentThreadDisposables.clear();
        this._commentThreadWidget.value = this.instantiationService.createInstance(CommentThreadWidget, this.container, this.notebookEditor, owner, this.notebookEditor.textModel.uri, this.contextKeyService, this.instantiationService, commentThread, undefined, undefined, {
            codeBlockFontFamily: this.configurationService.getValue('editor').fontFamily || EDITOR_FONT_DEFAULTS.fontFamily
        }, undefined, {
            actionRunner: () => {
            },
            collapse: () => { }
        });
        const layoutInfo = this.notebookEditor.getLayoutInfo();
        await this._commentThreadWidget.value.display(layoutInfo.fontInfo.lineHeight, true);
        this._applyTheme();
        this._commentThreadDisposables.add(this._commentThreadWidget.value.onDidResize(() => {
            if (this.currentElement?.cellKind === CellKind.Code && this._commentThreadWidget.value) {
                this.currentElement.commentHeight = this._calculateCommentThreadHeight(this._commentThreadWidget.value.getDimensions().height);
            }
        }));
    }
    _bindListeners() {
        this.cellDisposables.add(this.commentService.onDidUpdateCommentThreads(async () => this._updateThread()));
    }
    async _updateThread() {
        if (!this.currentElement) {
            return;
        }
        const info = await this._getCommentThreadForCell(this.currentElement);
        if (!this._commentThreadWidget.value && info) {
            await this._createCommentTheadWidget(info.owner, info.thread);
            const layoutInfo = this.currentElement.layoutInfo;
            this.container.style.top = `${layoutInfo.outputContainerOffset + layoutInfo.outputTotalHeight}px`;
            this.currentElement.commentHeight = this._calculateCommentThreadHeight(this._commentThreadWidget.value.getDimensions().height);
            return;
        }
        if (this._commentThreadWidget.value) {
            if (!info) {
                this._commentThreadDisposables.clear();
                this._commentThreadWidget.value = undefined;
                this.currentElement.commentHeight = 0;
                return;
            }
            if (this._commentThreadWidget.value.commentThread === info.thread) {
                this.currentElement.commentHeight = this._calculateCommentThreadHeight(this._commentThreadWidget.value.getDimensions().height);
                return;
            }
            await this._commentThreadWidget.value.updateCommentThread(info.thread);
            this.currentElement.commentHeight = this._calculateCommentThreadHeight(this._commentThreadWidget.value.getDimensions().height);
        }
    }
    _calculateCommentThreadHeight(bodyHeight) {
        const layoutInfo = this.notebookEditor.getLayoutInfo();
        const headHeight = Math.ceil(layoutInfo.fontInfo.lineHeight * 1.2);
        const lineHeight = layoutInfo.fontInfo.lineHeight;
        const arrowHeight = Math.round(lineHeight / 3);
        const frameThickness = Math.round(lineHeight / 9) * 2;
        const computedHeight = headHeight + bodyHeight + arrowHeight + frameThickness + 8 ;
        return computedHeight;
    }
    async _getCommentThreadForCell(element) {
        if (this.notebookEditor.hasModel()) {
            const commentInfos = coalesce(await this.commentService.getNotebookComments(element.uri));
            if (commentInfos.length && commentInfos[0].threads.length) {
                return { owner: commentInfos[0].uniqueOwner, thread: commentInfos[0].threads[0] };
            }
        }
        return null;
    }
    _applyTheme() {
        const theme = this.themeService.getColorTheme();
        const fontInfo = this.notebookEditor.getLayoutInfo().fontInfo;
        this._commentThreadWidget.value?.applyTheme(theme, fontInfo);
    }
    didRenderCell(element) {
        if (element.cellKind === CellKind.Code) {
            this.initialize(element);
            this._bindListeners();
        }
    }
    prepareLayout() {
        if (this.currentElement?.cellKind === CellKind.Code && this._commentThreadWidget.value) {
            this.currentElement.commentHeight = this._calculateCommentThreadHeight(this._commentThreadWidget.value.getDimensions().height);
        }
    }
    updateInternalLayoutNow(element) {
        if (this.currentElement?.cellKind === CellKind.Code && this._commentThreadWidget.value) {
            const layoutInfo = element.layoutInfo;
            this.container.style.top = `${layoutInfo.outputContainerOffset + layoutInfo.outputTotalHeight}px`;
        }
    }
};
CellComments = ( __decorate([
    ( __param(2, IContextKeyService)),
    ( __param(3, IThemeService)),
    ( __param(4, ICommentService)),
    ( __param(5, IConfigurationService)),
    ( __param(6, IInstantiationService))
], CellComments));
export { CellComments };
