"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentModule = void 0;
const tslib_1 = require("tslib");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
class CommentModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.CommentModuleId, ModuleConstants.CommentFriendlyName, 'comments', 'CommentsPopup', 'comments', api);
    }
    onAdaptableReady() {
        this.loadComments();
    }
    isModuleEnabled() {
        const options = this.api.optionsApi.getCommentOptions();
        if (!options || !options.persistCommentThreads || !options.loadCommentThreads) {
            return false;
        }
        if (this.api.optionsApi.isAutogeneratePrimaryKey()) {
            return false;
        }
        return super.isModuleEnabled();
    }
    async loadComments() {
        const commentThreads = await this.api.optionsApi
            .getCommentOptions()
            ?.loadCommentThreads?.(this.api.internalApi.buildBaseContext());
        this.api.commentApi.setComments(commentThreads);
    }
    createContextMenuItems(menuContext) {
        if (!this.isModuleVisible()) {
            return;
        }
        if (!this.api.commentApi.internalApi.areCommentsSupportedInLayout()) {
            return;
        }
        if (menuContext.isRowGroupColumn || menuContext.isGroupedNode) {
            return;
        }
        const items = [];
        const isCellCommentable = typeof this.api.optionsApi?.getCommentOptions()?.isCellCommentable === 'function'
            ? this.api.optionsApi?.getCommentOptions()?.isCellCommentable({
                gridCell: menuContext.gridCell,
                ...this.api.internalApi.buildBaseContext(),
            })
            : true;
        if (!isCellCommentable) {
            return;
        }
        const cellAddress = {
            PrimaryKeyValue: menuContext.primaryKeyValue,
            ColumnId: menuContext.adaptableColumn.columnId,
        };
        const cellComments = this.api.commentApi.getCommentThreadForCell(cellAddress)?.Comments;
        if (cellComments) {
            return [
                this.createMenuItemClickFunction('comment-remove', 'Remove Comment', this.moduleInfo.Glyph, () => {
                    this.api.commentApi.deleteCommentThread(cellAddress);
                }),
            ];
        }
        else {
            return [
                this.createMenuItemClickFunction('comment-add', 'Add Comment', this.moduleInfo.Glyph, () => {
                    // add an empty one
                    this.api.commentApi.addCommentThread({
                        ...cellAddress,
                        Comments: [],
                    });
                    requestAnimationFrame(() => {
                        this.api.internalApi.getAnnotationsService().showPopup({
                            PrimaryKeyValue: menuContext.primaryKeyValue,
                            ColumnId: menuContext.adaptableColumn.columnId,
                        }, true);
                        this.api.internalApi.getAnnotationsService().editFocusedEntity('Comment');
                    });
                }),
            ];
        }
    }
}
exports.CommentModule = CommentModule;
