package installation

import (
	"fmt"
	"strings"

	"github.com/tidbcloud/AgentTeam/internal/store/app/registry"
	"github.com/tidbcloud/AgentTeam/internal/store/model"
)

// MarshalJSONObject encodes an app-owned JSON object and preserves the parent
// app store's bad-request error semantics for invalid values.
func MarshalJSONObject(value map[string]any, fieldName string) ([]byte, error) {
	if value == nil {
		return []byte("{}"), nil
	}

	encoded, err := model.MarshalJSONObject("app "+fieldName, value)
	if err != nil {
		return nil, badRequest(fmt.Sprintf("Invalid %s object.", strings.TrimSpace(fieldName)))
	}
	return encoded, nil
}

func SortedUniqueScopes(scopes []string) []string {
	return registry.SortedUniqueScopes(scopes)
}

func UniqueTrimmedStrings(values []string) []string {
	return registry.UniqueTrimmedStrings(values)
}
