export function recolorSvg(svg: string, color: string): string {
  if (color === "none") return svg
  let out = svg
  out = out.replace(/\bstroke=["'](?!none)([^"']+)["']/g, `stroke="${color}"`)
  out = out.replace(/\bfill=["'](?!none)([^"']+)["']/g, (match, _val) => {
    if (match.includes(`fill="none"`)) return match
    return `fill="${color}"`
  })
  return out
}

export function stripSvgWrapper(svg: string): string {
  const m = svg.match(/<svg[^>]*>([\s\S]*)<\/svg>/)
  return m ? m[1].trim() : svg
}

export function extractRootAttrs(svg: string): { stroke?: string; fill?: string; strokeWidth?: string } {
  const attrs: { stroke?: string; fill?: string; strokeWidth?: string } = {}
  const strokeMatch = svg.match(/<svg[^>]*\bstroke=["']([^"']+)["']/)
  if (strokeMatch) attrs.stroke = strokeMatch[1]
  const fillMatch = svg.match(/<svg[^>]*\bfill=["']([^"']+)["']/)
  if (fillMatch) attrs.fill = fillMatch[1]
  const swMatch = svg.match(/<svg[^>]*\bstroke-width=["']([^"']+)["']/)
  if (swMatch) attrs.strokeWidth = swMatch[1]
  return attrs
}