package template

import (
	"fmt"
	link "net/url"
	"slices"
)

func url(text, url string) (string, error) {
	unsupported := []string{elvish, xonsh}
	if slices.Contains(unsupported, shell) {
		return text, nil
	}

	if url == "" {
		return text, nil
	}
	_, err := link.ParseRequestURI(url)
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("<LINK>%s<TEXT>%s</TEXT></LINK>", url, text), nil
}

func filePath(text, path string) (string, error) {
	unsupported := []string{elvish, xonsh}
	if slices.Contains(unsupported, shell) {
		return text, nil
	}

	encodedPath := (&link.URL{Path: path}).EscapedPath()

	return fmt.Sprintf("<LINK>file:%s<TEXT>%s</TEXT></LINK>", encodedPath, text), nil
}
