package cat

import (
	"fmt"
	"strings"
)

func (c Cater) ToTypst() string {
	var sb strings.Builder

	write := func(e entry, lvl int) {
		for range lvl {
			sb.WriteString("=")
		}
		sb.WriteString(fmt.Sprintf(" %s (`%s`)\n", e.name, e.fqname))

		if len(e.content) > 0 {
			prts := strings.Split(e.name, ".")
			sb.WriteString(
				fmt.Sprintf(
					"```%s\n%s\n```\n\n",

					prts[len(prts)-1],
					strings.ReplaceAll(
						e.content,
						"```",
						"\\`\\`\\`",
					),
				),
			)
		}
	}

	for _, e := range c {

		sb.WriteString("= Export\n")
		sb.WriteString("#outline()\n")
		e.traverse(1, write)
	}

	return sb.String()
}
