"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdaptableNoCodeWizard = exports.Adaptable = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const DocumentationLinkConstants_1 = require("../Utilities/Constants/DocumentationLinkConstants");
const AdaptableLogger_1 = require("./AdaptableLogger");
const AdaptableAgGrid_1 = require("./AdaptableAgGrid");
const core_1 = require("ag-grid-community");
const DefaultAdaptableOptions_1 = require("../AdaptableOptions/DefaultAdaptableOptions");
const renderReactRoot_1 = require("../renderReactRoot");
const AdaptableWizardView_1 = tslib_1.__importDefault(require("../View/AdaptableWizardView"));
class Adaptable {
    /**
     * Initializer for Adaptable
     *
     * @param adaptableOptions the AdaptableOptions
     * @param agGridConfig AG Grid
     * @returns a Promise containing the AdaptableAPI object in order to enable run-time access to Adaptable's properties and functions
     */
    static init(adaptableOptions, agGridConfig) {
        var _a;
        if (!((_a = agGridConfig === null || agGridConfig === void 0 ? void 0 : agGridConfig.modules) === null || _a === void 0 ? void 0 : _a.length)) {
            AdaptableLogger_1.AdaptableLogger.consoleErrorBase(`No AG Grid modules were provided, thus limiting AdapTables features significantly!\n\nRead further info here:${DocumentationLinkConstants_1.AgGridModulesDocsLink}`);
        }
        return AdaptableAgGrid_1.AdaptableAgGrid._initInternal(Object.assign(Object.assign({}, agGridConfig), { adaptableOptions, variant: 'vanilla' }));
    }
}
exports.Adaptable = Adaptable;
class AdaptableNoCodeWizard {
    /**
     * @param adaptableOptions
     */
    constructor(adaptableOptions, extraOptions = {}, agGridOptons) {
        const defaultInit = async ({ gridOptions, adaptableOptions, agGridModules, }) => {
            return await Adaptable.init(adaptableOptions, { gridOptions, modules: agGridModules });
        };
        this.adaptableOptions = adaptableOptions;
        this.init = extraOptions.onInit || defaultInit;
        this.extraOptions = extraOptions;
        this.agGridModules = core_1.ModuleRegistry.__getRegisteredModules(agGridOptons === null || agGridOptons === void 0 ? void 0 : agGridOptons.gridOptions.gridId);
        this.render();
    }
    render(container) {
        var _a;
        let id = (0, DefaultAdaptableOptions_1.getDefaultAdaptableOptions)().containerOptions.adaptableContainer || 'adaptable';
        if (!container) {
            let containerOptionsContainer = (_a = this.adaptableOptions.containerOptions) === null || _a === void 0 ? void 0 : _a.adaptableContainer;
            if (containerOptionsContainer) {
                if (typeof containerOptionsContainer === 'string') {
                    id = containerOptionsContainer !== null && containerOptionsContainer !== void 0 ? containerOptionsContainer : id;
                }
                else {
                    container = containerOptionsContainer;
                }
            }
        }
        container = container || document.getElementById(id);
        if (!container) {
            throw new Error('Cannot find container in which to render Adaptable No Code Wizard');
        }
        // this allows people to customize the wizard dimensions & styling
        // when it's visible
        container.classList.add('adaptable--in-wizard');
        const unmount = (0, renderReactRoot_1.renderReactRoot)(React.createElement(AdaptableWizardView_1.default, Object.assign(Object.assign({ adaptableOptions: this.adaptableOptions }, this.extraOptions), { onInit: (adaptableOptions, gridOptions) => {
                container.classList.remove('adaptable--in-wizard');
                unmount();
                this.init({
                    adaptableOptions,
                    gridOptions: gridOptions,
                    agGridModules: [...this.agGridModules],
                });
            } })), container);
    }
}
exports.AdaptableNoCodeWizard = AdaptableNoCodeWizard;
