package util

import (
	"fmt"
	"strings"
)

func LinkInText(text, urlformat string, args ...any) string {
	return fmt.Sprintf("[%s](%s)", text, fmt.Sprintf(urlformat, args...))
}

func AtUserInText(id string) string {
	return fmt.Sprintf(`<at user_id="%s"></at>`, id)
}

func AtUserInCard(id string) string {
	return fmt.Sprintf(`<at id="%s"></at>`, id)
}

// FormatItems formats a list of items
//
// Example:
// FormatItems([]string{"a", "b", "c"}, "  ", "- ")
//   - a
//   - b
//   - c
func FormatItems(lines []string, indent string, prefix string) string {
	if len(lines) == 0 {
		return ""
	}

	b := &strings.Builder{}
	for i := 0; i < len(lines)-1; i++ {
		b.WriteString(indent)
		b.WriteString(prefix)
		b.WriteString(lines[i])
		b.WriteString("\n")
	}

	b.WriteString(indent)
	b.WriteString(prefix)
	b.WriteString(lines[len(lines)-1])

	return b.String()
}

// GenChatLink returns the link to open a chat in Lark
func GenChatLink(chatID string) string {
	return fmt.Sprintf("https://applink.feishu.cn/client/chat/open?openChatId=%s", chatID)
}
