/**
 * @dyna-grid/core - Advanced Data Grid / Data Table supporting Javascript / Web Components
 * @version v1.0.5
 * @link 
 * @license 
 */
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var gridOptionsWrapper_1 = require("./gridOptionsWrapper");
var selectionController_1 = require("./selectionController");
var columnApi_1 = require("./columnController/columnApi");
var columnController_1 = require("./columnController/columnController");
var rowRenderer_1 = require("./rendering/rowRenderer");
var headerRootComp_1 = require("./headerRendering/headerRootComp");
var filterManager_1 = require("./filter/filterManager");
var valueService_1 = require("./valueService/valueService");
var eventService_1 = require("./eventService");
var gridPanel_1 = require("./gridPanel/gridPanel");
var gridApi_1 = require("./gridApi");
var columnFactory_1 = require("./columnController/columnFactory");
var displayedGroupCreator_1 = require("./columnController/displayedGroupCreator");
var expressionService_1 = require("./valueService/expressionService");
var templateService_1 = require("./templateService");
var popupService_1 = require("./widgets/popupService");
var logger_1 = require("./logger");
var columnUtils_1 = require("./columnController/columnUtils");
var autoWidthCalculator_1 = require("./rendering/autoWidthCalculator");
var horizontalResizeService_1 = require("./headerRendering/horizontalResizeService");
var context_1 = require("./context/context");
var gridCore_1 = require("./gridCore");
var standardMenu_1 = require("./headerRendering/standardMenu");
var dragAndDropService_1 = require("./dragAndDrop/dragAndDropService");
var dragService_1 = require("./dragAndDrop/dragService");
var sortController_1 = require("./sortController");
var focusController_1 = require("./focusController");
var mouseEventService_1 = require("./gridPanel/mouseEventService");
var cellNavigationService_1 = require("./cellNavigationService");
var events_1 = require("./events");
var valueFormatterService_1 = require("./rendering/valueFormatterService");
var dyCheckbox_1 = require("./widgets/dyCheckbox");
var dyRadioButton_1 = require("./widgets/dyRadioButton");
var vanillaFrameworkOverrides_1 = require("./vanillaFrameworkOverrides");
var scrollVisibleService_1 = require("./gridPanel/scrollVisibleService");
var stylingService_1 = require("./styling/stylingService");
var columnHoverService_1 = require("./rendering/columnHoverService");
var columnAnimationService_1 = require("./rendering/columnAnimationService");
var autoGroupColService_1 = require("./columnController/autoGroupColService");
var paginationProxy_1 = require("./pagination/paginationProxy");
var paginationAutoPageSizeService_1 = require("./pagination/paginationAutoPageSizeService");
var constants_1 = require("./constants/constants");
var valueCache_1 = require("./valueService/valueCache");
var changeDetectionService_1 = require("./valueService/changeDetectionService");
var alignedGridsService_1 = require("./alignedGridsService");
var userComponentFactory_1 = require("./components/framework/userComponentFactory");
var userComponentRegistry_1 = require("./components/framework/userComponentRegistry");
var dyComponentUtils_1 = require("./components/framework/dyComponentUtils");
var componentMetadataProvider_1 = require("./components/framework/componentMetadataProvider");
var beans_1 = require("./rendering/beans");
var environment_1 = require("./environment");
var animationFrameService_1 = require("./misc/animationFrameService");
var navigationService_1 = require("./gridPanel/navigationService");
var maxDivHeightScaler_1 = require("./rendering/maxDivHeightScaler");
var selectableService_1 = require("./rowNodes/selectableService");
var autoHeightCalculator_1 = require("./rendering/row/autoHeightCalculator");
var paginationComp_1 = require("./pagination/paginationComp");
var resizeObserverService_1 = require("./misc/resizeObserverService");
var overlayWrapperComponent_1 = require("./rendering/overlays/overlayWrapperComponent");
var dyGroupComponent_1 = require("./widgets/dyGroupComponent");
var dyDialog_1 = require("./widgets/dyDialog");
var dyPanel_1 = require("./widgets/dyPanel");
var dyInputTextField_1 = require("./widgets/dyInputTextField");
var dyInputTextArea_1 = require("./widgets/dyInputTextArea");
var dySlider_1 = require("./widgets/dySlider");
var dyColorPicker_1 = require("./widgets/dyColorPicker");
var dyInputNumberField_1 = require("./widgets/dyInputNumberField");
var dyInputRange_1 = require("./widgets/dyInputRange");
var dySelect_1 = require("./widgets/dySelect");
var dyAngleSelect_1 = require("./widgets/dyAngleSelect");
var dyToggleButton_1 = require("./widgets/dyToggleButton");
var detailRowCompCache_1 = require("./rendering/row/detailRowCompCache");
var rowPosition_1 = require("./entities/rowPosition");
var cellPosition_1 = require("./entities/cellPosition");
var pinnedRowModel_1 = require("./pinnedRowModel/pinnedRowModel");
var moduleRegistry_1 = require("./modules/moduleRegistry");
var moduleNames_1 = require("./modules/moduleNames");
var undoRedoService_1 = require("./undoRedo/undoRedoService");
var dyStackComponentsRegistry_1 = require("./components/dyStackComponentsRegistry");
var headerPosition_1 = require("./headerRendering/header/headerPosition");
var headerNavigationService_1 = require("./headerRendering/header/headerNavigationService");
var generic_1 = require("./utils/generic");
var object_1 = require("./utils/object");
var columnDefFactory_1 = require("./columnController/columnDefFactory");
var rowCssClassCalculator_1 = require("./rendering/row/rowCssClassCalculator");
var rowNodeBlockLoader_1 = require("./rowNodeCache/rowNodeBlockLoader");
var rowNodeSorter_1 = require("./rowNodes/rowNodeSorter");
var Grid = /** @class */ (function () {
    function Grid(eGridDiv, gridOptions, params) {
        if (!eGridDiv) {
            console.error('DYNA Grid: no div element provided to the grid');
            return;
        }
        if (!gridOptions) {
            console.error('DYNA Grid: no gridOptions provided to the grid');
            return;
        }
        var debug = !!gridOptions.debug;
        this.gridOptions = gridOptions;
        var registeredModules = this.getRegisteredModules(params);
        var beanClasses = this.createBeansList(registeredModules);
        var providedBeanInstances = this.createProvidedBeans(eGridDiv, params);
        if (!beanClasses) {
            return;
        } // happens when no row model found
        var contextParams = {
            providedBeanInstances: providedBeanInstances,
            beanClasses: beanClasses,
            debug: debug
        };
        this.logger = new logger_1.Logger('DYNA Grid', function () { return gridOptions.debug; });
        var contextLogger = new logger_1.Logger('Context', function () { return contextParams.debug; });
        this.context = new context_1.Context(contextParams, contextLogger);
        this.registerModuleUserComponents(registeredModules);
        this.registerStackComponents(registeredModules);
        var gridCoreClass = (params && params.rootComponent) || gridCore_1.GridCore;
        var gridCore = new gridCoreClass();
        this.context.createBean(gridCore);
        this.setColumnsAndData();
        this.dispatchGridReadyEvent(gridOptions);
        var isEnterprise = moduleRegistry_1.ModuleRegistry.isRegistered(moduleNames_1.ModuleNames.ProCoreModule);
        this.logger.log("initialised successfully, enterprise = " + isEnterprise);
    }
    Grid.prototype.registerStackComponents = function (registeredModules) {
        var dyStackComponents = this.createDyStackComponentsList(registeredModules);
        var dyStackComponentsRegistry = this.context.getBean('dyStackComponentsRegistry');
        dyStackComponentsRegistry.setupComponents(dyStackComponents);
    };
    Grid.prototype.getRegisteredModules = function (params) {
        var passedViaConstructor = params ? params.modules : null;
        var registered = moduleRegistry_1.ModuleRegistry.getRegisteredModules();
        var allModules = [];
        var mapNames = {};
        // adds to list and removes duplicates
        function addModule(moduleBased, mod) {
            function addIndividualModule(currentModule) {
                if (!mapNames[currentModule.moduleName]) {
                    mapNames[currentModule.moduleName] = true;
                    allModules.push(currentModule);
                    moduleRegistry_1.ModuleRegistry.register(currentModule, moduleBased);
                }
            }
            addIndividualModule(mod);
            if (mod.dependantModules) {
                mod.dependantModules.forEach(addModule.bind(null, moduleBased));
            }
        }
        if (passedViaConstructor) {
            passedViaConstructor.forEach(addModule.bind(null, true));
        }
        if (registered) {
            registered.forEach(addModule.bind(null, !moduleRegistry_1.ModuleRegistry.isPackageBased()));
        }
        return allModules;
    };
    Grid.prototype.registerModuleUserComponents = function (registeredModules) {
        var userComponentRegistry = this.context.getBean('userComponentRegistry');
        var moduleUserComps = this.extractModuleEntity(registeredModules, function (module) { return module.userComponents ? module.userComponents : []; });
        moduleUserComps.forEach(function (compMeta) {
            userComponentRegistry.registerDefaultComponent(compMeta.componentName, compMeta.componentClass);
        });
    };
    Grid.prototype.createProvidedBeans = function (eGridDiv, params) {
        var frameworkOverrides = params ? params.frameworkOverrides : null;
        if (generic_1.missing(frameworkOverrides)) {
            frameworkOverrides = new vanillaFrameworkOverrides_1.VanillaFrameworkOverrides();
        }
        var seed = {
            gridOptions: this.gridOptions,
            eGridDiv: eGridDiv,
            $scope: params ? params.$scope : null,
            $compile: params ? params.$compile : null,
            quickFilterOnScope: params ? params.quickFilterOnScope : null,
            globalEventListener: params ? params.globalEventListener : null,
            frameworkOverrides: frameworkOverrides
        };
        if (params && params.providedBeanInstances) {
            object_1.assign(seed, params.providedBeanInstances);
        }
        return seed;
    };
    Grid.prototype.createDyStackComponentsList = function (registeredModules) {
        var components = [
            { componentName: 'DyCheckbox', componentClass: dyCheckbox_1.DyCheckbox },
            { componentName: 'DyRadioButton', componentClass: dyRadioButton_1.DyRadioButton },
            { componentName: 'DyToggleButton', componentClass: dyToggleButton_1.DyToggleButton },
            { componentName: 'DyInputTextField', componentClass: dyInputTextField_1.DyInputTextField },
            { componentName: 'DyInputTextArea', componentClass: dyInputTextArea_1.DyInputTextArea },
            { componentName: 'DyInputNumberField', componentClass: dyInputNumberField_1.DyInputNumberField },
            { componentName: 'DyInputRange', componentClass: dyInputRange_1.DyInputRange },
            { componentName: 'DySelect', componentClass: dySelect_1.DySelect },
            { componentName: 'DySlider', componentClass: dySlider_1.DySlider },
            { componentName: 'DyAngleSelect', componentClass: dyAngleSelect_1.DyAngleSelect },
            { componentName: 'DyColorPicker', componentClass: dyColorPicker_1.DyColorPicker },
            { componentName: 'DyGridComp', componentClass: gridPanel_1.GridPanel },
            { componentName: 'DyHeaderRoot', componentClass: headerRootComp_1.HeaderRootComp },
            { componentName: 'DyPagination', componentClass: paginationComp_1.PaginationComp },
            { componentName: 'DyOverlayWrapper', componentClass: overlayWrapperComponent_1.OverlayWrapperComponent },
            { componentName: 'DyGroupComponent', componentClass: dyGroupComponent_1.DyGroupComponent },
            { componentName: 'DyPanel', componentClass: dyPanel_1.DyPanel },
            { componentName: 'DyDialog', componentClass: dyDialog_1.DyDialog }
        ];
        var moduleDyStackComps = this.extractModuleEntity(registeredModules, function (module) { return module.dyStackComponents ? module.dyStackComponents : []; });
        components = components.concat(moduleDyStackComps);
        return components;
    };
    Grid.prototype.createBeansList = function (registeredModules) {
        var rowModelClass = this.getRowModelClass(registeredModules);
        if (!rowModelClass) {
            return;
        }
        // beans should only contain SERVICES, it should NEVER contain COMPONENTS
        var beans = [
            rowModelClass, beans_1.Beans, rowPosition_1.RowPositionUtils, cellPosition_1.CellPositionUtils, headerPosition_1.HeaderPositionUtils,
            paginationAutoPageSizeService_1.PaginationAutoPageSizeService, gridApi_1.GridApi, userComponentRegistry_1.UserComponentRegistry, dyComponentUtils_1.DyComponentUtils,
            componentMetadataProvider_1.ComponentMetadataProvider, resizeObserverService_1.ResizeObserverService, userComponentFactory_1.UserComponentFactory,
            maxDivHeightScaler_1.MaxDivHeightScaler, autoHeightCalculator_1.AutoHeightCalculator, horizontalResizeService_1.HorizontalResizeService,
            pinnedRowModel_1.PinnedRowModel, dragService_1.DragService, displayedGroupCreator_1.DisplayedGroupCreator, eventService_1.EventService, gridOptionsWrapper_1.GridOptionsWrapper,
            popupService_1.PopupService, selectionController_1.SelectionController, filterManager_1.FilterManager, columnController_1.ColumnController, headerNavigationService_1.HeaderNavigationService,
            paginationProxy_1.PaginationProxy, rowRenderer_1.RowRenderer, expressionService_1.ExpressionService, columnFactory_1.ColumnFactory, templateService_1.TemplateService,
            alignedGridsService_1.AlignedGridsService, navigationService_1.NavigationService, valueCache_1.ValueCache, valueService_1.ValueService, logger_1.LoggerFactory,
            columnUtils_1.ColumnUtils, autoWidthCalculator_1.AutoWidthCalculator, standardMenu_1.StandardMenuFactory, dragAndDropService_1.DragAndDropService, columnApi_1.ColumnApi,
            focusController_1.FocusController, mouseEventService_1.MouseEventService, environment_1.Environment, cellNavigationService_1.CellNavigationService, valueFormatterService_1.ValueFormatterService,
            stylingService_1.StylingService, scrollVisibleService_1.ScrollVisibleService, sortController_1.SortController, columnHoverService_1.ColumnHoverService, columnAnimationService_1.ColumnAnimationService,
            selectableService_1.SelectableService, autoGroupColService_1.AutoGroupColService, changeDetectionService_1.ChangeDetectionService, animationFrameService_1.AnimationFrameService,
            detailRowCompCache_1.DetailRowCompCache, undoRedoService_1.UndoRedoService, dyStackComponentsRegistry_1.DyStackComponentsRegistry, columnDefFactory_1.ColumnDefFactory,
            rowCssClassCalculator_1.RowCssClassCalculator, rowNodeBlockLoader_1.RowNodeBlockLoader, rowNodeSorter_1.RowNodeSorter
        ];
        var moduleBeans = this.extractModuleEntity(registeredModules, function (module) { return module.beans ? module.beans : []; });
        beans.push.apply(beans, moduleBeans);
        // check for duplicates, as different modules could include the same beans that
        // they depend on, eg ClientSideRowModel in enterprise, and ClientSideRowModel in community
        var beansNoDuplicates = [];
        beans.forEach(function (bean) {
            if (beansNoDuplicates.indexOf(bean) < 0) {
                beansNoDuplicates.push(bean);
            }
        });
        return beansNoDuplicates;
    };
    Grid.prototype.extractModuleEntity = function (moduleEntities, extractor) {
        return [].concat.apply([], moduleEntities.map(extractor));
    };
    Grid.prototype.setColumnsAndData = function () {
        var gridOptionsWrapper = this.context.getBean('gridOptionsWrapper');
        var columnController = this.context.getBean('columnController');
        var columnDefs = gridOptionsWrapper.getColumnDefs();
        columnController.setColumnDefs(columnDefs || [], "gridInitializing");
        var rowModel = this.context.getBean('rowModel');
        rowModel.start();
    };
    Grid.prototype.dispatchGridReadyEvent = function (gridOptions) {
        var eventService = this.context.getBean('eventService');
        var readyEvent = {
            type: events_1.Events.EVENT_GRID_READY,
            api: gridOptions.api,
            columnApi: gridOptions.columnApi
        };
        eventService.dispatchEvent(readyEvent);
    };
    Grid.prototype.getRowModelClass = function (registeredModules) {
        var rowModelType = this.gridOptions.rowModelType;
        // default to client side
        if (!rowModelType) {
            rowModelType = constants_1.Constants.ROW_MODEL_TYPE_CLIENT_SIDE;
        }
        var rowModelClasses = {};
        registeredModules.forEach(function (module) {
            object_1.iterateObject(module.rowModels, function (key, value) {
                rowModelClasses[key] = value;
            });
        });
        var rowModelClass = rowModelClasses[rowModelType];
        if (generic_1.exists(rowModelClass)) {
            return rowModelClass;
        }
        if (rowModelType === constants_1.Constants.ROW_MODEL_TYPE_INFINITE) {
            console.error("DYNA Grid: Row Model \"Infinite\" not found. Please ensure the " + moduleNames_1.ModuleNames.InfiniteRow + " is registered.';");
        }
        console.error('DYNA Grid: could not find matching row model for rowModelType ' + rowModelType);
        if (rowModelType === constants_1.Constants.ROW_MODEL_TYPE_VIEWPORT) {
            console.error("DYNA Grid: Row Model \"Viewport\" not found. Please ensure the DYNA Grid Enterprise Module " + moduleNames_1.ModuleNames.ViewportRow + " is registered.';");
        }
        if (rowModelType === constants_1.Constants.ROW_MODEL_TYPE_SERVER_SIDE) {
            console.error("DYNA Grid: Row Model \"Server Side\" not found. Please ensure the DYNA Grid Enterprise Module " + moduleNames_1.ModuleNames.ServerSideRow + " is registered.';");
        }
        if (rowModelType === constants_1.Constants.ROW_MODEL_TYPE_CLIENT_SIDE) {
            console.error("DYNA Grid: Row Model \"Client Side\" not found. Please ensure the " + moduleNames_1.ModuleNames.ClientSideRow + " is registered.';");
        }
    };
    Grid.prototype.destroy = function () {
        this.gridOptions.api.destroy();
    };
    return Grid;
}());
exports.Grid = Grid;

//# sourceMappingURL=grid.js.map
