package transfer_intent_hydrator

import (
	"strconv"
	"strings"

	"github.com/tech/sendico/pkg/merrors"
	"go.mongodb.org/mongo-driver/v2/bson"
)

func decodeMethodData(data []byte, dst any, field string) error {
	if len(data) == 0 {
		return merrors.InvalidArgument(field + ".data is required")
	}
	if err := bson.Unmarshal(data, dst); err != nil {
		return merrors.InvalidArgument(field + ".data is invalid")
	}
	return nil
}

func parseUint32(value string, field string) (uint32, error) {
	trimmed := strings.TrimSpace(value)
	if trimmed == "" {
		return 0, nil
	}
	num, err := strconv.ParseUint(trimmed, 10, 32)
	if err != nil {
		return 0, merrors.InvalidArgument(field + " must be numeric")
	}
	return uint32(num), nil
}

func inferKind(destination QuoteEndpoint) QuoteIntentKind {
	switch destination.Type {
	case QuoteEndpointTypeExternalChain, QuoteEndpointTypeCard:
		return QuoteIntentKindPayout
	default:
		return QuoteIntentKindInternalTransfer
	}
}
