package chat

import (
	"encoding/json"
	"gorm.io/datatypes"
	"strings"
)

func normalizePagination(page, pageSize int) (int, int) {
	if page < 1 {
		page = 1
	}
	if pageSize < 1 {
		pageSize = 20
	}
	if pageSize > 100 {
		pageSize = 100
	}
	return page, pageSize
}
func truncatePreview(content string) string {
	runes := []rune(content)
	if len(runes) <= 80 {
		return content
	}
	return string(runes[:80])
}
func messagePreview(content string, attachments []string) string {
	content = strings.TrimSpace(content)
	if content != "" {
		return truncatePreview(content)
	}
	if len(attachments) > 0 {
		return "[图片]"
	}
	return ""
}
func emptyJSONList() datatypes.JSON {
	raw, _ := json.Marshal([]string{})
	return datatypes.JSON(raw)
}
func encodeStringList(items []string) datatypes.JSON {
	raw, _ := json.Marshal(items)
	return datatypes.JSON(raw)
}
func decodeStringList(raw datatypes.JSON) []string {
	if len(raw) == 0 {
		return []string{}
	}
	var items []string
	if err := json.Unmarshal(raw, &items); err != nil {
		return []string{}
	}
	return items
}
