package rpcsrv

import (
	"git.marketally.com/tutus-one/tutus-chain/pkg/tutusrpc"
)

// abstractResult is an interface which represents either single JSON-RPC 2.0 response
// or batch JSON-RPC 2.0 response.
type abstractResult interface {
	RunForErrors(f func(jsonErr *tutusrpc.Error))
}

// abstract represents abstract JSON-RPC 2.0 response. It is used as a server-side response
// representation.
type abstract struct {
	tutusrpc.Header
	Error  *tutusrpc.Error `json:"error,omitzero"`
	Result any           `json:"result,omitzero"`
}

// RunForErrors implements abstractResult interface.
func (a abstract) RunForErrors(f func(jsonErr *tutusrpc.Error)) {
	if a.Error != nil {
		f(a.Error)
	}
}

// abstractBatch represents abstract JSON-RPC 2.0 batch-response.
type abstractBatch []abstract

// RunForErrors implements abstractResult interface.
func (ab abstractBatch) RunForErrors(f func(jsonErr *tutusrpc.Error)) {
	for _, a := range ab {
		a.RunForErrors(f)
	}
}
