// Copyright (c) 2026 Modelware. All rights reserved.

import { ChartMarkdownBlockRenderer } from './chart-renderer.js';
import { DiagramMarkdownBlockRenderer } from './diagram-renderer.js';
import { GraphMarkdownBlockRenderer } from './graph-renderer.js';
import { ListMarkdownBlockRenderer } from './list-renderer.js';
import { MatrixMarkdownBlockRenderer } from './matrix-renderer.js';
import { MessageMarkdownBlockRenderer } from './message-renderer.js';
import type { MarkdownBlockRenderer } from './renderer.js';
import { TableEditorMarkdownBlockRenderer } from './table-editor-renderer.js';
import { TableMarkdownBlockRenderer } from './table-renderer.js';
import { TextMarkdownBlockRenderer } from './text-renderer.js';
import { TreeMarkdownBlockRenderer } from './tree-renderer.js';
import type { MdBlockExecutionResult } from './types.js';

export class MarkdownRendererRegistry {
    constructor(private readonly renderers: ReadonlyArray<MarkdownBlockRenderer>) {}

    render(result: MdBlockExecutionResult): HTMLElement {
        const renderer = this.renderers.find((candidate) => candidate.canRender(result));
        if (!renderer) {
            throw new Error(`No renderer registered for block '${result.blockId}'.`);
        }
        return renderer.render(result);
    }
}

export function createCoreMarkdownRenderers(): MarkdownBlockRenderer[] {
    return [
        new MatrixMarkdownBlockRenderer(),
        new TreeMarkdownBlockRenderer(),
        new TableEditorMarkdownBlockRenderer(),
        new TableMarkdownBlockRenderer(),
        new ListMarkdownBlockRenderer(),
        new TextMarkdownBlockRenderer(),
        new GraphMarkdownBlockRenderer(),
        new DiagramMarkdownBlockRenderer(),
        new ChartMarkdownBlockRenderer(),
        new MessageMarkdownBlockRenderer(),
    ];
}

export function createMarkdownRendererRegistry(): MarkdownRendererRegistry {
    return new MarkdownRendererRegistry(createCoreMarkdownRenderers());
}
