"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Fdc3Module = void 0;
const tslib_1 = require("tslib");
const AdaptableModuleBase_1 = require("./AdaptableModuleBase");
const ModuleConstants = tslib_1.__importStar(require("../Utilities/Constants/ModuleConstants"));
class Fdc3Module extends AdaptableModuleBase_1.AdaptableModuleBase {
    constructor(api) {
        super(ModuleConstants.Fdc3ModuleId, ModuleConstants.Fdc3FriendlyName, 'fdc3', null, 'The FDC3 Module - handles the FDC3 integration', api);
    }
    createContextMenuItems(menuContext) {
        if (!this.isModuleAvailable()) {
            return;
        }
        if (menuContext.adaptableColumn && menuContext.rowNode) {
            const currentColumnId = menuContext.adaptableColumn.columnId;
            const currentRowNode = menuContext.rowNode;
            const contextMenuItems = [];
            this.getFdc3Api()
                .internalApi.getAllRaiseIntentConfigs()
                .filter((raiseIntentConfig) => raiseIntentConfig.contextMenu?.columnIds.includes(currentColumnId))
                .forEach((raiseIntentConfig) => {
                contextMenuItems.push(this.buildRaiseIntentMenuItem(raiseIntentConfig, currentRowNode));
            });
            this.getFdc3Api()
                .internalApi.getAllBroadcastContextConfigs()
                .filter((broadcastConfig) => broadcastConfig.contextMenu?.columnIds.includes(currentColumnId))
                .forEach((broadcastConfig) => {
                contextMenuItems.push(this.buildBroadcastMenuItem(broadcastConfig, currentRowNode));
            });
            contextMenuItems.sort((a, b) => a.label.localeCompare(b.label));
            return contextMenuItems;
        }
    }
    buildRaiseIntentMenuItem(intentConfig, rowNode) {
        const { intentType, contextType } = intentConfig;
        const menuItem = {
            name: 'fdc3-raise-intent',
            label: `Raise ${intentType}`,
            onClick: () => {
                this.getFdc3Api().raiseIntentFromRow(rowNode, intentType, contextType);
            },
            isVisible: true,
            category: this.moduleInfo.ModuleName,
        };
        if (intentConfig.contextMenu?.icon) {
            const menuItemIcon = intentConfig.contextMenu?.icon === '_defaultFdc3'
                ? this.getFdc3Api().internalApi.getDefaultIconForIntent(intentType)
                : intentConfig.contextMenu?.icon;
            menuItem.icon = menuItemIcon;
        }
        return menuItem;
    }
    buildBroadcastMenuItem(contextConfig, rowNode) {
        const { contextType } = contextConfig;
        const menuItem = {
            name: 'fdc3-broadcast',
            label: `Broadcast ${this.getFdc3Api().getContextLabel(contextType)}`,
            onClick: () => {
                this.getFdc3Api().broadcastFromRow(rowNode, contextType);
            },
            isVisible: true,
            category: this.moduleInfo.ModuleName,
        };
        if (contextConfig.contextMenu?.icon) {
            const menuItemIcon = contextConfig.contextMenu?.icon === '_defaultFdc3'
                ? this.getFdc3Api().internalApi.getDefaultIconForContext(contextType)
                : contextConfig.contextMenu?.icon;
            menuItem.icon = menuItemIcon;
        }
        return menuItem;
    }
    getFdc3Api() {
        return this.api.fdc3Api;
    }
}
exports.Fdc3Module = Fdc3Module;
