package rpc

import (
	"encoding/json"
)

// DecodeResponse decodes a JSON-RPC response payload into the expected Go type.
// If result is nil, it returns the zero value of T.
// If result is a json.RawMessage, it unmarshals it into T.
func DecodeResponse[T any](result any) (T, error) {
	var zero T
	if result == nil {
		return zero, nil
	}
	raw, ok := result.(json.RawMessage)
	if !ok {
		return zero, nil
	}
	var resp T
	if err := json.Unmarshal(raw, &resp); err != nil {
		return zero, err
	}
	return resp, nil
}
