package formatter_utils

import (
	"bufio"
	"strings"
)

func FormatText(text string) string {
	scanner := bufio.NewScanner(strings.NewReader(text))

	scanner.Split(bufio.ScanLines)

	lines := []string{}
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}

	var res strings.Builder
	for i, line := range lines {
		l := strings.TrimSpace(line)
		if strings.HasPrefix(l, "--") {
			l = strings.Replace(l, "--", "—", 1)
		}
		if i == 0 && strings.HasPrefix(l, "—") {
			res.WriteString("    ")
		}
		if i > 0 {
			res.WriteString("\n")
			if len(l) > 0 {
				res.WriteString("    ")
			}
		}
		res.WriteString(l)
	}
	return res.String()
}

func FormatString(text string) string {
	l := strings.TrimSpace(text)
	if strings.HasPrefix(l, "--") {
		l = strings.Replace(l, "--", "—", 1)
	}
	return l
}
