"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NoteModule = void 0;
const tslib_1 = require("tslib");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
class NoteModule extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.NoteModuleId, ModuleConstants.NoteFriendlyName, 'note', 'NotePopup', 'Notes', api);
    }
    isModuleAvailable() {
        // Note module doesn't support autogenerated primary keys
        return super.isModuleAvailable() && !this.api.optionsApi.isAutogeneratePrimaryKey();
    }
    getModuleAdaptableObjects() {
        return this.api.noteApi.getAllNotes();
    }
    createContextMenuItems(menuContext) {
        if (!this.isModuleAvailable()) {
            return;
        }
        if (!this.api.noteApi.internalApi.areNotesSupported()) {
            return;
        }
        if (menuContext.isRowGroupColumn || menuContext.isGroupedNode) {
            return;
        }
        const isReadOnly = this.api.entitlementApi.getEntitlementAccessLevelForModule(this.moduleInfo.ModuleName) ===
            'ReadOnly';
        if (isReadOnly) {
            return undefined;
        }
        const isCellNotable = typeof this.api.optionsApi?.getNoteOptions()?.isCellNotable === 'function'
            ? this.api.optionsApi.getNoteOptions().isCellNotable({
                gridCell: menuContext.gridCell,
                ...this.api.internalApi.buildBaseContext(),
            })
            : true;
        if (!isCellNotable) {
            return;
        }
        const note = this.api.noteApi.getNoteForCell({
            PrimaryKeyValue: menuContext.primaryKeyValue,
            ColumnId: menuContext.adaptableColumn.columnId,
        });
        if (note) {
            return [
                this.createMenuItemClickFunction('note-remove', 'Remove Note', this.moduleInfo.Glyph, () => {
                    this.api.noteApi.deleteNote(note);
                }),
            ];
        }
        else {
            return [
                this.createMenuItemClickFunction('note-add', 'Add Note', this.moduleInfo.Glyph, () => {
                    // add an empty one
                    this.api.noteApi.addNote('', menuContext.primaryKeyValue, menuContext.adaptableColumn.columnId);
                    this.api.internalApi.getAnnotationsService().showPopup({
                        PrimaryKeyValue: menuContext.primaryKeyValue,
                        ColumnId: menuContext.adaptableColumn.columnId,
                    }, true);
                }),
            ];
        }
    }
}
exports.NoteModule = NoteModule;
