import { AdaptableLogger } from '.';
import { AllEnterpriseModule } from 'ag-grid-enterprise';
export const AG_GRID_VERSION = AllEnterpriseModule.version;
export class AgGridModulesAdapter {
    constructor(adaptableInstance) {
        this.adaptableInstance = adaptableInstance;
        this.allAgGridModules = new Set();
        this.allAgGridModules = this.extractAgGridModuleNames(AllEnterpriseModule);
    }
    destroy() {
        this.adaptableInstance = null;
        this.allAgGridModules.clear();
        this.allAgGridModules = null;
    }
    get agGridApi() {
        return this.adaptableInstance.agGridAdapter.getAgGridApi();
    }
    isAgGridModuleRegistered(moduleName) {
        return this.agGridApi.isModuleRegistered(moduleName);
    }
    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':
                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;
    }
}
