package k8sresource

import (
	"strings"

	regionalpb "github.com/tidbcloud/infra-provider/api/tidbcloud/regional-server/v1"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

func namespacedName(sel *regionalpb.Selector) (namespace, name string, byName bool) {
	if sel == nil {
		return "", "", false
	}
	name = strings.TrimSpace(sel.Name)
	if name != "" {
		return strings.TrimSpace(sel.Namespace), name, true
	}
	return strings.TrimSpace(sel.Namespace), "", false
}

func buildListOptions(sel *regionalpb.Selector, limit int64) []client.ListOption {
	opts := []client.ListOption{client.Limit(limit)}
	if sel == nil {
		return opts
	}
	if ns := strings.TrimSpace(sel.Namespace); ns != "" {
		opts = append(opts, client.InNamespace(ns))
	}
	if len(sel.Label) > 0 {
		opts = append(opts, client.MatchingLabels(sel.Label))
	}
	return opts
}
