export class EditorPluginHost {
    plugins = new Map();
    register(plugin) {
        const id = normalizeId(plugin.id, "plugin");
        if (this.plugins.has(id)) {
            throw new Error(`Editor plugin already registered: ${id}`);
        }
        this.assertUnique(plugin.panels ?? [], "panel", id);
        this.assertUnique(plugin.tools ?? [], "tool", id);
        this.assertUnique(plugin.importers ?? [], "importer", id);
        this.assertUnique(plugin.scriptingNodes ?? [], "scripting node", id);
        this.plugins.set(id, { ...plugin, id });
    }
    unregister(id) {
        return this.plugins.delete(normalizeId(id, "plugin"));
    }
    get(id) {
        return this.plugins.get(normalizeId(id, "plugin"));
    }
    list() {
        return [...this.plugins.values()];
    }
    snapshot() {
        const plugins = this.list();
        return {
            plugins,
            panels: sortByOrder(plugins.flatMap((plugin) => plugin.panels ?? [])),
            tools: sortByOrder(plugins.flatMap((plugin) => plugin.tools ?? [])),
            importers: plugins.flatMap((plugin) => plugin.importers ?? []),
            scriptingNodes: plugins.flatMap((plugin) => plugin.scriptingNodes ?? [])
        };
    }
    clear() {
        this.plugins.clear();
    }
    assertUnique(contributions, kind, pluginId) {
        const seen = new Set();
        for (const contribution of contributions) {
            const id = normalizeId(contribution.id, kind);
            if (seen.has(id)) {
                throw new Error(`Editor plugin ${pluginId} declares duplicate ${kind}: ${id}`);
            }
            seen.add(id);
        }
    }
}
function normalizeId(id, kind) {
    const normalized = id.trim();
    if (!/^[a-z0-9][a-z0-9.-]*$/i.test(normalized)) {
        throw new Error(`Editor ${kind} id must be non-empty and use letters, numbers, dots, or dashes.`);
    }
    return normalized;
}
function sortByOrder(items) {
    return [...items].sort((left, right) => (left.order ?? 0) - (right.order ?? 0) || left.id.localeCompare(right.id));
}
//# sourceMappingURL=EditorPluginHost.js.map