// Low-level SVG string generation helpers.

///|
/// Escape XML special characters in text content.
pub fn xmlEscape(s : String) -> String {
  s
  .replace(old="&", new="&")
  .replace(old="<", new="<")
  .replace(old=">", new=">")
  .replace(old="\"", new=""")
  .replace(old="'", new="'")
}

///|
/// Generate an SVG rect element.
pub fn rect(
  x~ : Double,
  y~ : Double,
  w~ : Double,
  h~ : Double,
  fill~ : String,
  stroke~ : String,
  strokeWidth~ : Double,
) -> String {
  ""
}

///|
/// Generate an SVG line element.
pub fn line(
  x1~ : Double,
  y1~ : Double,
  x2~ : Double,
  y2~ : Double,
  stroke~ : String,
  strokeWidth~ : Double,
) -> String {
  ""
}

///|
/// Generate an SVG circle element.
pub fn circle(
  cx~ : Double,
  cy~ : Double,
  r~ : Double,
  fill~ : String,
  stroke~ : String,
  strokeWidth~ : Double,
) -> String {
  ""
}

///|
/// Generate an SVG text element.
pub fn text(
  x~ : Double,
  y~ : Double,
  content~ : String,
  font~ : Font,
  textAnchor~ : String,
) -> String {
  let escaped = xmlEscape(content)
  "\{escaped}"
}

///|
/// Generate a rotated SVG text element (for Y axis labels).
pub fn rotatedText(
  x~ : Double,
  y~ : Double,
  content~ : String,
  font~ : Font,
) -> String {
  let escaped = xmlEscape(content)
  "\{escaped}"
}

///|
/// Generate the SVG document opening.
pub fn svgOpen(width~ : Double, height~ : Double, bg~ : String) -> String {
  "\n  "
}

///|
/// Generate the SVG document closing.
pub fn svgClose() -> String {
  ""
}

///|
/// Generate an SVG polyline element for line charts.
pub fn polyline(
  points~ : String,
  stroke~ : String,
  strokeWidth~ : Double,
  fill~ : String,
) -> String {
  ""
}

///|
/// Format a sequence of (x, y) points into a polyline points string.
pub fn formatPoints(pts : Array[(Double, Double)]) -> String {
  let parts : Array[String] = []
  for i = 0; i < pts.length(); i = i + 1 {
    let (px, py) = pts[i]
    parts.push("\{px},\{py}")
  }
  let mut result = ""
  for i = 0; i < parts.length(); i = i + 1 {
    if i == 0 {
      result = parts[i]
    } else {
      result = result + " " + parts[i]
    }
  }
  result
}

///|
/// Save an SVG string to a file in the current directory.
/// Uses @fs.write_string_to_file from moonbitlang/x.
/// Returns Ok(()) on success, Err(message) on failure.
pub fn saveSvg(filename : String, svg : String) -> Result[Unit, String] {
  try {
    @fs.write_string_to_file(filename, svg)
    Ok(())
  } catch {
    _ => Err("Failed to write file: \{filename}")
  }
}