/**
 * Hook for validating Mermaid code completeness
 */

import { useCallback } from 'react';

export function useMermaidValidation() {
    const isMermaidCodeComplete = useCallback((code: string): boolean => {
        if (!code || code.trim().length === 0) return false;

        const trimmed = code.trim();

        // Check if code has basic structure
        const lines = trimmed.split('\n');
        if (lines.length < 2) return false; // Need at least diagram type + one element

        // Check for common incomplete patterns
        const lastLine = lines[lines.length - 1].trim();

        // Incomplete if last line ends with arrow without destination
        if (lastLine.match(/-->?\s*$/)) return false;
        if (lastLine.match(/-->\|[^|]*\|\s*$/)) return false;

        // Incomplete if last line ends with opening bracket/parenthesis
        if (lastLine.match(/[\[({]\s*$/)) return false;

        return true;
    }, []);

    return { isMermaidCodeComplete };
}
