package xerr

import (
	"fmt"
	"strings"
)

// DeviceAlreadyRegisteredMessage 生成同设备注册达到上限时直接展示给 App 的错误文案。
// count 代表当前设备已注册账号数，displayUserIDs 按原注册顺序列出短号，便于用户识别原账号。
func DeviceAlreadyRegisteredMessage(count int, displayUserIDs []string) string {
	cleaned := make([]string, 0, len(displayUserIDs))
	seen := make(map[string]struct{}, len(displayUserIDs))
	for _, displayUserID := range displayUserIDs {
		displayUserID = strings.TrimSpace(displayUserID)
		if displayUserID == "" {
			continue
		}
		if _, ok := seen[displayUserID]; ok {
			continue
		}
		seen[displayUserID] = struct{}{}
		cleaned = append(cleaned, displayUserID)
	}
	if count < len(cleaned) {
		// 调用方如果只传了短号列表而没单独计数，展示数量必须至少覆盖实际列出的账号。
		count = len(cleaned)
	}
	if count < 0 {
		count = 0
	}
	if len(cleaned) == 0 {
		return fmt.Sprintf("you device already register %d account, login failed", count)
	}

	return fmt.Sprintf("you device already register %d account %s, login failed", count, strings.Join(cleaned, ","))
}
