// Code generated by gtrace. DO NOT EDIT.

package gtrace

import (
	"context"

	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)

// discoveryComposeOptions is a holder of options
type discoveryComposeOptions struct {
	panicCallback func(e interface{})
}

// DiscoveryOption specified Discovery compose option
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
type DiscoveryComposeOption func(o *discoveryComposeOptions)

// WithDiscoveryPanicCallback specified behavior on panic
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
func WithDiscoveryPanicCallback(cb func(e interface{})) DiscoveryComposeOption {
	return func(o *discoveryComposeOptions) {
		o.panicCallback = cb
	}
}

// Compose returns a new Discovery which has functional fields composed both from lhs and rhs.
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
func Compose(lhs *trace.Discovery, rhs *trace.Discovery, opts ...DiscoveryComposeOption) *trace.Discovery {
	if lhs == nil {
		return rhs
	}
	var ret trace.Discovery
	options := discoveryComposeOptions{}
	for _, opt := range opts {
		if opt != nil {
			opt(&options)
		}
	}
	{
		h1 := lhs.OnDiscover
		h2 := rhs.OnDiscover
		ret.OnDiscover = func(d trace.DiscoveryDiscoverStartInfo) func(trace.DiscoveryDiscoverDoneInfo) {
			if options.panicCallback != nil {
				defer func() {
					if e := recover(); e != nil {
						options.panicCallback(e)
					}
				}()
			}
			var r, r1 func(trace.DiscoveryDiscoverDoneInfo)
			if h1 != nil {
				r = h1(d)
			}
			if h2 != nil {
				r1 = h2(d)
			}
			return func(d trace.DiscoveryDiscoverDoneInfo) {
				if options.panicCallback != nil {
					defer func() {
						if e := recover(); e != nil {
							options.panicCallback(e)
						}
					}()
				}
				if r != nil {
					r(d)
				}
				if r1 != nil {
					r1(d)
				}
			}
		}
	}
	{
		h1 := lhs.OnWhoAmI
		h2 := rhs.OnWhoAmI
		ret.OnWhoAmI = func(d trace.DiscoveryWhoAmIStartInfo) func(trace.DiscoveryWhoAmIDoneInfo) {
			if options.panicCallback != nil {
				defer func() {
					if e := recover(); e != nil {
						options.panicCallback(e)
					}
				}()
			}
			var r, r1 func(trace.DiscoveryWhoAmIDoneInfo)
			if h1 != nil {
				r = h1(d)
			}
			if h2 != nil {
				r1 = h2(d)
			}
			return func(d trace.DiscoveryWhoAmIDoneInfo) {
				if options.panicCallback != nil {
					defer func() {
						if e := recover(); e != nil {
							options.panicCallback(e)
						}
					}()
				}
				if r != nil {
					r(d)
				}
				if r1 != nil {
					r1(d)
				}
			}
		}
	}
	return &ret
}
func onDiscover(t *trace.Discovery, d trace.DiscoveryDiscoverStartInfo) func(trace.DiscoveryDiscoverDoneInfo) {
	fn := t.OnDiscover
	if fn == nil {
		return func(trace.DiscoveryDiscoverDoneInfo) {
			return
		}
	}
	res := fn(d)
	if res == nil {
		return func(trace.DiscoveryDiscoverDoneInfo) {
			return
		}
	}
	return res
}
func onWhoAmI(t *trace.Discovery, d trace.DiscoveryWhoAmIStartInfo) func(trace.DiscoveryWhoAmIDoneInfo) {
	fn := t.OnWhoAmI
	if fn == nil {
		return func(trace.DiscoveryWhoAmIDoneInfo) {
			return
		}
	}
	res := fn(d)
	if res == nil {
		return func(trace.DiscoveryWhoAmIDoneInfo) {
			return
		}
	}
	return res
}
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
func DiscoveryOnDiscover(t *trace.Discovery, c *context.Context, c1 trace.Call, address string, database string) func(location string, endpoints []trace.EndpointInfo, _ error) {
	var p trace.DiscoveryDiscoverStartInfo
	p.Context = c
	p.Call = c1
	p.Address = address
	p.Database = database
	res := onDiscover(t, p)
	return func(location string, endpoints []trace.EndpointInfo, e error) {
		var p trace.DiscoveryDiscoverDoneInfo
		p.Location = location
		p.Endpoints = endpoints
		p.Error = e
		res(p)
	}
}
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
func DiscoveryOnWhoAmI(t *trace.Discovery, c *context.Context, c1 trace.Call) func(user string, groups []string, _ error) {
	var p trace.DiscoveryWhoAmIStartInfo
	p.Context = c
	p.Call = c1
	res := onWhoAmI(t, p)
	return func(user string, groups []string, e error) {
		var p trace.DiscoveryWhoAmIDoneInfo
		p.User = user
		p.Groups = groups
		p.Error = e
		res(p)
	}
}
