"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgGridModulesAdapter = exports.AG_GRID_VERSION = void 0;
const _1 = require(".");
const ag_grid_enterprise_1 = require("ag-grid-enterprise");
exports.AG_GRID_VERSION = ag_grid_enterprise_1.AllEnterpriseModule.version;
class AgGridModulesAdapter {
    adaptableInstance;
    allAgGridModules = new Set();
    constructor(adaptableInstance) {
        this.adaptableInstance = adaptableInstance;
        this.allAgGridModules = this.extractAgGridModuleNames(ag_grid_enterprise_1.AllEnterpriseModule);
    }
    destroy() {
        this.adaptableInstance = null;
        this.allAgGridModules.clear();
        this.allAgGridModules = null;
    }
    get agGridApi() {
        return this.adaptableInstance.agGridAdapter.getAgGridApi();
    }
    isAgGridModuleRegistered(moduleName) {
        let isModuleRegistered = this.agGridApi.isModuleRegistered(moduleName);
        if (!isModuleRegistered) {
            // SSRM module comes with its own implementations of Pivot, MasterDetail and TreeData
            // #ssrm_pivot_module
            const serverSideDependencies = [
                'PivotModule',
                'MasterDetailModule',
                'TreeDataModule',
            ];
            if (serverSideDependencies.includes(moduleName)) {
                isModuleRegistered = this.agGridApi.isModuleRegistered('ServerSideRowModelModule');
            }
        }
        return isModuleRegistered;
    }
    getAgGridRegisteredModules() {
        const registeredModules = [];
        this.allAgGridModules.forEach((module) => {
            const agModuleName = `${module.moduleName}Module`;
            if (this.isAgGridModuleRegistered(agModuleName)) {
                registeredModules.push(module);
            }
        });
        return registeredModules;
    }
    getAgGridRegisteredModuleNames() {
        return this.getAgGridRegisteredModules().map((module) => {
            const agModuleName = `${module.moduleName}Module`;
            return agModuleName;
        });
    }
    getMandatoryAgGridModuleNames() {
        const mandatoryModules = [];
        const rowModelType = this.agGridApi.getGridOption('rowModelType');
        switch (rowModelType) {
            case 'serverSide':
                mandatoryModules.push('ServerSideRowModelModule');
                mandatoryModules.push('ServerSideRowModelApiModule');
                break;
            case 'infinite':
                _1.AdaptableLogger.consoleErrorBase("AdapTable does not support AG Grid's Infinite Row Model");
                break;
            case 'viewport':
                mandatoryModules.push('ViewportRowModelModule');
                break;
            default:
                mandatoryModules.push('ClientSideRowModelModule');
                mandatoryModules.push('ClientSideRowModelApiModule');
        }
        mandatoryModules.push('CellApiModule', 'CellStyleModule', 'ColumnApiModule', 'ColumnMenuModule', 'ContextMenuModule', 'CustomFilterModule', 'EventApiModule', 'ExternalFilterModule', 'GridStateModule', 'GroupFilterModule', 'RenderApiModule', 'RowApiModule', 'RowGroupingModule', 'RowStyleModule', 'ScrollApiModule');
        return mandatoryModules;
    }
    extractAgGridModuleNames(module, collectedModules = new Set()) {
        if (!module || !module.moduleName) {
            return collectedModules;
        }
        collectedModules.add(module);
        if (module.dependsOn) {
            module.dependsOn.forEach((dep) => {
                this.extractAgGridModuleNames(dep, collectedModules);
            });
        }
        return collectedModules;
    }
}
exports.AgGridModulesAdapter = AgGridModulesAdapter;
