package handlers

import (
	"encoding/json"
	"html/template"
	"strings"

	"git.soup.land/soup/lookbook/internal/static"
)

var TemplateFuncs = template.FuncMap{
	"staticURL": static.VersionedPath,
	"json":      jsonTemplate,
}

func jsonTemplate(v any) template.JS {
	payload, err := json.Marshal(v)
	if err != nil {
		return template.JS("null")
	}
	safe := strings.ReplaceAll(string(payload), "</", "<\\/")
	return template.JS(safe)
}
