// Filename: internal/utils/parser.go
package utils

import (
	"encoding/json"
	"regexp"
	"strings"
)

// ParseKeysFromText 是一个共享的工具函数，用于从各种格式的文本中解析出密钥列表。
// 因为它是共享的，所以函数名首字母大写以导出。
func ParseKeysFromText(text string) []string {
	var keys []string
	if json.Unmarshal([]byte(text), &keys) == nil && len(keys) > 0 {
		var cleanedKeys []string
		for _, key := range keys {
			trimmed := strings.TrimSpace(key)
			if trimmed != "" {
				cleanedKeys = append(cleanedKeys, trimmed)
			}
		}
		return cleanedKeys
	}
	keys = []string{}
	delimiters := regexp.MustCompile(`[\s,;|\n\r\t]+`)
	splitKeys := delimiters.Split(strings.TrimSpace(text), -1)
	for _, key := range splitKeys {
		trimmed := strings.TrimSpace(key)
		if trimmed != "" {
			keys = append(keys, trimmed)
		}
	}
	return keys
}
