import { schema, denormalize as baseDenormalize } from 'normalizr';
import { WidgetTypes } from '../types/index.js';
import { getNextEntityName } from './string.js';
const widgetSchema = new schema.Entity('canvasWidgets', {}, { idAttribute: 'widgetId' });
widgetSchema.define({ children: [widgetSchema] });
const PAGE_WIDGET_ID = '0';
/**
 * Fixes invalid DSL from previous bugs on initial load.
 *  1. removes duplicate IDs
 *  2. de-duplicates identical Section and Canvas names because their names don't matter
 *  3. removes timers and stateVars from the page widget, as they are processed separately
 */
export function normalize(dsl) {
    if (!dsl)
        return { entities: { canvasWidgets: {} }, result: '' };
    const flattenedWidgets = {};
    const previouslySeenCanvasNames = new Set();
    function flattenWidget(widget) {
        if (flattenedWidgets[widget.widgetId])
            return;
        if (widget.children) {
            flattenedWidgets[widget.widgetId] = {
                ...widget,
                children: Array.from(new Set(widget.children?.map((child) => child.widgetId)))
            };
        }
        else {
            flattenedWidgets[widget.widgetId] = widget;
        }
        if (widget.widgetId === PAGE_WIDGET_ID) {
            // Extracted separately
            delete widget.stateVars;
            delete widget.timers;
            delete widget.events;
        }
        if ((widget.type === WidgetTypes.CANVAS_WIDGET || widget.type === WidgetTypes.SECTION_WIDGET) &&
            previouslySeenCanvasNames.has(widget.widgetName)) {
            const newName = getNextEntityName(widget.widgetName, Array.from(previouslySeenCanvasNames));
            flattenedWidgets[widget.widgetId].widgetName = newName;
            previouslySeenCanvasNames.add(newName);
        }
        else {
            previouslySeenCanvasNames.add(widget.widgetName);
        }
        if (widget.children) {
            widget.children.forEach(flattenWidget);
        }
    }
    flattenWidget(dsl);
    return {
        entities: { canvasWidgets: flattenedWidgets },
        result: dsl.widgetId
    };
}
export function denormalize(pageWidgetId, entities) {
    return baseDenormalize(pageWidgetId, widgetSchema, entities);
}
//# sourceMappingURL=normalize.js.map