package pkg

import (
	"io"

	"github.com/antchfx/xmlquery"
)

func Fill(svg io.Reader, data map[string]xmlquery.Node) (filledSvg string, err error) {
	doc, err := xmlquery.Parse(svg)
	defer func() { filledSvg = doc.OutputXML(true) }()
	if err != nil {
		return
	}

	for key, value := range data {
		nodes := xmlquery.Find(doc, "//*/g[@inkscape:label='fill']/*[@inkscape:label='"+key+"']")
		if err != nil {
			return filledSvg, err
		}

		for _, node := range nodes {
			node.FirstChild = &value
		}
	}

	return
}
