// (C) 2021-2026 GoodData Corporation
import { isCustomWidget } from "../../model/types/layoutTypes.js";
class WidgetCustomizerState {
    logger;
    customWidgetMap = new Map();
    constructor(logger) {
        this.logger = logger;
    }
    addDefinition = (definition) => {
        if (this.customWidgetMap.has(definition.widgetType)) {
            this.logger.warn(`Redefining custom widget type ${definition.widgetType}. Previous definition will be discarded.`);
        }
        this.customWidgetMap.set(definition.widgetType, definition);
    };
    getCustomWidgetMap = () => {
        return this.customWidgetMap;
    };
}
class SealedCustomizerState {
    state;
    logger;
    constructor(state, logger) {
        this.state = state;
        this.logger = logger;
    }
    addDefinition = (_definition) => {
        this.logger.warn(`Attempting to add custom widgets outside of plugin registration. Ignoring.`);
    };
    getCustomWidgetMap = () => {
        return this.state.getCustomWidgetMap();
    };
}
export class DefaultWidgetCustomizer {
    logger;
    state;
    constructor(logger) {
        this.logger = logger;
        this.state = new WidgetCustomizerState(this.logger);
    }
    addCustomWidget = (widgetType, Component) => {
        this.state.addDefinition({
            widgetType,
            DefaultComponent: Component,
        });
        return this;
    };
    sealCustomizer = () => {
        this.state = new SealedCustomizerState(this.state, this.logger);
        return this;
    };
    getWidgetComponentProvider = () => {
        const customWidgetMap = this.state.getCustomWidgetMap();
        return (widget) => {
            if (!isCustomWidget(widget)) {
                return undefined;
            }
            const definition = customWidgetMap.get(widget.customType);
            if (!definition) {
                return undefined;
            }
            return definition.DefaultComponent;
        };
    };
}
//# sourceMappingURL=widgetCustomizer.js.map