import Helper from '../Helpers/Helper';
import { AgGridRequiredModulesDocsLink, HOST_URL_DOCS, } from '../Constants/DocumentationLinkConstants';
export class ModuleService {
    adaptableApi;
    constructor(adaptableApi) {
        this.adaptableApi = adaptableApi;
        this.adaptableApi = adaptableApi;
    }
    isAdapTableModulePresent(adaptableModule) {
        let module = this.getModuleCollection().get(adaptableModule);
        if (!module) {
            return false;
        }
        return module.isModuleVisible();
    }
    isModuleEditable(adaptableModule) {
        let module = this.getModuleCollection().get(adaptableModule);
        if (!module) {
            return false;
        }
        return module.isModuleEditable();
    }
    logMissingAgGridDepsInfos() {
        // log missing core (required) AG Grid dependencies
        const agGridModulesAdapter = this.adaptableApi.internalApi.getAgGridModulesAdapter();
        if (agGridModulesAdapter.isAgGridModuleRegistered('AllEnterpriseModule')) {
            // no need to check further if AllEnterprise is registered
            // it may even trigger false positives (e.g. when using SSRM)
            return;
        }
        const mandatoryAgGridModuleNames = agGridModulesAdapter.getMandatoryAgGridModuleNames();
        const registeredAgGridModuleNames = agGridModulesAdapter.getAgGridRegisteredModuleNames();
        const missingAgGridModuleNames = mandatoryAgGridModuleNames.filter((moduleName) => !registeredAgGridModuleNames.includes(moduleName));
        if (missingAgGridModuleNames.length) {
            this.adaptableApi.consoleError(`Adaptable requires these AG Grid modules: ${missingAgGridModuleNames.join(', ')}. Please register them. See: ${AgGridRequiredModulesDocsLink}`);
        }
        // log optional missing AG Grid dependencies for all modules
        this.getModuleCollection().forEach((adaptableModule) => {
            adaptableModule.logMissingAgGridDepsInfos();
        });
    }
    getModuleFriendlyName(adaptableModule) {
        return this.getModuleInfoByModule(adaptableModule)?.FriendlyName ?? adaptableModule;
    }
    createModuleUIItems() {
        this.createSettingsPanelItems();
        this.createModuleButtonsForDashboardAndToolPanel();
    }
    createSettingsPanelItems() {
        const settingsPanelItems = [];
        this.getModuleCollection().forEach((module) => {
            const settingsPanelMenuItem = module.createModuleMenuItem('ModuleMenu');
            if (Helper.objectExists(settingsPanelMenuItem)) {
                if (settingsPanelItems.findIndex((m) => m.category == settingsPanelMenuItem.category) == -1) {
                    settingsPanelItems.push(settingsPanelMenuItem);
                }
            }
        });
        // store Settings Panel items in Internal State as they never change and we often re-use
        this.adaptableApi.internalApi.setSettingsPanelItems(settingsPanelItems);
    }
    createModuleButtonsForDashboardAndToolPanel() {
        const moduleButtons = [];
        this.getModuleCollection().forEach((module) => {
            const moduleButton = module.createModuleMenuItem('ModuleButton');
            if (Helper.objectExists(moduleButton)) {
                if (moduleButtons.findIndex((m) => m.category == moduleButton.category) == -1) {
                    moduleButtons.push(moduleButton);
                }
            }
        });
        // store Dashboard Buttons in Internal State as they never change l
        this.adaptableApi.internalApi.setDashboardModuleButtons(moduleButtons);
    }
    getTeamSharingAction(adaptableModule) {
        let module = this.getModuleCollection().get(adaptableModule);
        if (!module) {
            return undefined;
        }
        return module.getTeamSharingAction();
    }
    getModuleById(adaptableModule) {
        // @ts-ignore TODO: make modules generic
        return this.getModuleCollection().get(adaptableModule);
    }
    getModuleInfoByModule(adaptableModule) {
        const module = this.getModuleById(adaptableModule);
        return module ? module.moduleInfo : undefined;
    }
    getModuleInfoByFriendlyName(friendlyName) {
        for (let module of this.getModuleCollection().values()) {
            let moduleInfo = module.moduleInfo;
            if (moduleInfo && moduleInfo.FriendlyName == friendlyName) {
                return moduleInfo;
            }
        }
        return undefined;
    }
    getPopupMaxWidth(adaptableModule) {
        return this.getModuleById(adaptableModule)?.getPopupMaxWidth();
    }
    getModuleCollection() {
        return this.adaptableApi.internalApi.getModules();
    }
    getModuleDocumentationPageByModule(adaptableModule) {
        let url = `${HOST_URL_DOCS}/`;
        switch (adaptableModule) {
            case 'Alert':
                return url + 'handbook-alerting';
            case 'BulkUpdate':
                return url + 'handbook-editing-bulk-update';
            case 'CalculatedColumn':
                return url + 'handbook-calculated-column';
            case 'CellSummary':
                return url + 'handbook-summarising-cells';
            case 'Charting':
                return url + 'handbook-charts';
            case 'ColumnFilter':
                return url + 'handbook-column-filter';
            case 'ColumnInfo':
                return url + 'dev-guide-columns-column-info';
            case 'Comment':
                return url + 'handbook-comments';
            case 'CustomSort':
                return url + 'handbook-custom-sorting';
            case 'Dashboard':
                return url + 'ui-dashboard';
            case 'DataChangeHistory':
                return url + 'handbook-monitoring-data-change-history';
            case 'DataImport':
                return url + 'handbook-importing';
            case 'DataSet':
                return url + 'handbook-data-sets';
            case 'Export':
                return url + 'handbook-exporting';
            case 'Fdc3':
                return url + 'handbook-fdc3';
            case 'FlashingCell':
                return url + 'handbook-flashing-cell';
            case 'FormatColumn':
                return url + 'handbook-column-formatting';
            case 'FreeTextColumn':
                return url + 'handbook-freetext-column';
            case 'GridFilter':
                return url + 'handbook-grid-filter';
            case 'GridInfo':
                return url + 'dev-guide-support-monitoring';
            case 'Layout':
                return url + 'handbook-layouts';
            case 'NamedQuery':
                return url + 'handbook-named-queries';
            case 'Note':
                return url + 'handbook-notes';
            case 'PlusMinus':
                return url + 'handbook-editing-plus-minus';
            case 'QuickSearch':
                return url + 'handbook-quick-search';
            case 'Schedule':
                return url + 'handbook-scheduling';
            case 'SettingsPanel':
                return url + 'ui-settings-panel';
            case 'Shortcut':
                return url + 'handbook-editing-shortcut';
            case 'SmartEdit':
                return url + 'handbook-editing-smart-edit';
            case 'StateManagement':
                return url + 'dev-guide-adaptable-state-management';
            case 'StatusBar':
                return url + 'ui-status-bar';
            case 'StyledColumn':
                return url + 'handbook-styled-column-overview';
            case 'SystemStatus':
                return url + 'handbook-system-status-message';
            case 'TeamSharing':
                return url + 'handbook-team-sharing';
            case 'Theme':
                return url + 'handbook-theming';
            case 'ToolPanel':
                return url + 'ui-tool-panel';
            case 'IPushPull':
                return url + 'integrations-ipushpull';
            case 'OpenFin':
                return url + 'integrations-openfin';
            default:
                return 'good';
        }
    }
    destroy() {
        // TO DO
    }
}
