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

import type { CodeBlockHandler, ParsedCodeBlock } from './md-types.js';

export class MarkdownHandlerRegistry {
    private readonly handlers: CodeBlockHandler[] = [];

    register(handler: CodeBlockHandler): void {
        if (this.handlers.some((h) => h.id === handler.id)) {
            throw new Error(`Duplicate markdown handler id: ${handler.id}`);
        }
        this.handlers.push(handler);
    }

    match(block: ParsedCodeBlock): CodeBlockHandler | undefined {
        return this.handlers.find((handler) => handler.supports(block));
    }

    list(): ReadonlyArray<CodeBlockHandler> {
        return this.handlers;
    }
}
