"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 DefaultAdaptableOptions_1 = require("../AdaptableOptions/DefaultAdaptableOptions");
const renderReactRoot_1 = require("../renderReactRoot");
const AdaptableWizardView_1 = tslib_1.__importDefault(require("../View/AdaptableWizardView"));
const resolveContainerElement_1 = require("../Utilities/resolveContainerElement");
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) {
        if (!agGridConfig?.modules?.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({ ...agGridConfig, adaptableOptions, variant: 'vanilla' });
    }
}
exports.Adaptable = Adaptable;
class AdaptableNoCodeWizard {
    init;
    adaptableOptions;
    extraOptions;
    agGridModules;
    /**
     * @param adaptableOptions
     */
    constructor(adaptableOptions, extraOptions = {}, agGridOptions) {
        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 = [];
        this.render();
    }
    render(container) {
        if (!container) {
            const initContext = {
                adaptableId: this.adaptableOptions?.adaptableId ?? '',
                adaptableStateKey: this.adaptableOptions?.adaptableStateKey ?? '',
                adaptableContext: this.adaptableOptions?.adaptableContext,
            };
            container = (0, resolveContainerElement_1.resolveContainerElement)(this.adaptableOptions.containerOptions?.adaptableContainer, initContext);
        }
        if (!container) {
            const defaultId = (0, DefaultAdaptableOptions_1.getDefaultAdaptableOptions)().containerOptions.adaptableContainer || 'adaptable';
            container = document.getElementById(defaultId);
        }
        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, {
            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;
