package d2latex

import (
	
	
)

const (
	// MaxInputBytes bounds the work and output that a single LaTeX label can
	// cause in the embedded MathJax renderer.
	MaxInputBytes = 4 << 10

	// MaxGroupNestingDepth prevents deeply nested TeX groups from exhausting
	// the Go stack in mathjax-go's recursive group parser.
	MaxGroupNestingDepth = 128
)

// ValidateInput applies D2's resource limits to a LaTeX label. Render and
// Measure both call it so compiler, WASM, and raw render-target paths share
// the same admission policy.
func ( string) error {
	if len() > MaxInputBytes {
		return fmt.Errorf("latex input is %d bytes, exceeding limit %d", len(), MaxInputBytes)
	}
	if !utf8.ValidString() {
		return fmt.Errorf("latex input must be valid UTF-8")
	}

	 := 0
	 := 0
	 := false
	for  := 0;  < len(); ++ {
		 := []
		if  {
			if  == '\n' ||  == '\r' {
				 = false
			}
			continue
		}
		if  == '\\' {
			++
			continue
		}

		 := %2 != 0
		 = 0
		if  {
			continue
		}

		switch  {
		case '%':
			 = true
		case '{':
			++
			if  > MaxGroupNestingDepth {
				return fmt.Errorf("latex group nesting depth %d exceeds limit %d", , MaxGroupNestingDepth)
			}
		case '}':
			if  > 0 {
				--
			}
		}
	}
	return nil
}