package message

import (
	"bytes"
	"text/template"
)

// Render executes a Go template string with the given data. Pure.
func Render(tmpl string, data any) (string, error) {
	t, err := template.New("").Parse(tmpl)
	if err != nil {
		return "", err
	}
	var buf bytes.Buffer
	if err := t.Execute(&buf, data); err != nil {
		return "", err
	}
	return buf.String(), nil
}

// MustRender is like Render but panics on error. Use only in tests.
func MustRender(tmpl string, data any) string {
	s, err := Render(tmpl, data)
	if err != nil {
		panic(err)
	}
	return s
}
