/**
 * Helper utilities for Mermaid diagram rendering
 */

// Utility function to apply text colors to Mermaid SVG
export const applyMermaidTextColors = (container: HTMLElement, textColor: string) => {
    const svgElement = container.querySelector('svg');
    if (svgElement) {
        // SVG text elements use 'fill'
        svgElement.querySelectorAll('text').forEach((el) => {
            (el as SVGElement).style.fill = textColor;
        });

        // HTML elements inside foreignObject use 'color'
        svgElement.querySelectorAll('.nodeLabel, .edgeLabel').forEach((el) => {
            (el as HTMLElement).style.color = textColor;
        });
    }
};

// Detect if diagram is vertical (tall and narrow)
export const isVerticalDiagram = (svgElement: SVGSVGElement): boolean => {
    const viewBox = svgElement.getAttribute('viewBox');
    if (viewBox) {
        const [, , width, height] = viewBox.split(' ').map(Number);
        return height > width * 1.5;
    }
    const bbox = svgElement.getBBox?.();
    if (bbox) {
        return bbox.height > bbox.width * 1.5;
    }
    return false;
};
