// Code generated by gtrace. DO NOT EDIT.

package gtrace

import (
	"context"

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

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

// ScriptingOption specified Scripting compose option
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
type ScriptingComposeOption func(o *scriptingComposeOptions)

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

// Compose returns a new Scripting 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.Scripting, rhs *trace.Scripting, opts ...ScriptingComposeOption) *trace.Scripting {
	if lhs == nil {
		return rhs
	}
	var ret trace.Scripting
	options := scriptingComposeOptions{}
	for _, opt := range opts {
		if opt != nil {
			opt(&options)
		}
	}
	{
		h1 := lhs.OnExecute
		h2 := rhs.OnExecute
		ret.OnExecute = func(s trace.ScriptingExecuteStartInfo) func(trace.ScriptingExecuteDoneInfo) {
			if options.panicCallback != nil {
				defer func() {
					if e := recover(); e != nil {
						options.panicCallback(e)
					}
				}()
			}
			var r, r1 func(trace.ScriptingExecuteDoneInfo)
			if h1 != nil {
				r = h1(s)
			}
			if h2 != nil {
				r1 = h2(s)
			}
			return func(s trace.ScriptingExecuteDoneInfo) {
				if options.panicCallback != nil {
					defer func() {
						if e := recover(); e != nil {
							options.panicCallback(e)
						}
					}()
				}
				if r != nil {
					r(s)
				}
				if r1 != nil {
					r1(s)
				}
			}
		}
	}
	{
		h1 := lhs.OnStreamExecute
		h2 := rhs.OnStreamExecute
		ret.OnStreamExecute = func(s trace.ScriptingStreamExecuteStartInfo) func(trace.ScriptingStreamExecuteIntermediateInfo) func(trace.ScriptingStreamExecuteDoneInfo) {
			if options.panicCallback != nil {
				defer func() {
					if e := recover(); e != nil {
						options.panicCallback(e)
					}
				}()
			}
			var r, r1 func(trace.ScriptingStreamExecuteIntermediateInfo) func(trace.ScriptingStreamExecuteDoneInfo)
			if h1 != nil {
				r = h1(s)
			}
			if h2 != nil {
				r1 = h2(s)
			}
			return func(s trace.ScriptingStreamExecuteIntermediateInfo) func(trace.ScriptingStreamExecuteDoneInfo) {
				if options.panicCallback != nil {
					defer func() {
						if e := recover(); e != nil {
							options.panicCallback(e)
						}
					}()
				}
				var r2, r3 func(trace.ScriptingStreamExecuteDoneInfo)
				if r != nil {
					r2 = r(s)
				}
				if r1 != nil {
					r3 = r1(s)
				}
				return func(s trace.ScriptingStreamExecuteDoneInfo) {
					if options.panicCallback != nil {
						defer func() {
							if e := recover(); e != nil {
								options.panicCallback(e)
							}
						}()
					}
					if r2 != nil {
						r2(s)
					}
					if r3 != nil {
						r3(s)
					}
				}
			}
		}
	}
	{
		h1 := lhs.OnExplain
		h2 := rhs.OnExplain
		ret.OnExplain = func(s trace.ScriptingExplainStartInfo) func(trace.ScriptingExplainDoneInfo) {
			if options.panicCallback != nil {
				defer func() {
					if e := recover(); e != nil {
						options.panicCallback(e)
					}
				}()
			}
			var r, r1 func(trace.ScriptingExplainDoneInfo)
			if h1 != nil {
				r = h1(s)
			}
			if h2 != nil {
				r1 = h2(s)
			}
			return func(s trace.ScriptingExplainDoneInfo) {
				if options.panicCallback != nil {
					defer func() {
						if e := recover(); e != nil {
							options.panicCallback(e)
						}
					}()
				}
				if r != nil {
					r(s)
				}
				if r1 != nil {
					r1(s)
				}
			}
		}
	}
	{
		h1 := lhs.OnClose
		h2 := rhs.OnClose
		ret.OnClose = func(s trace.ScriptingCloseStartInfo) func(trace.ScriptingCloseDoneInfo) {
			if options.panicCallback != nil {
				defer func() {
					if e := recover(); e != nil {
						options.panicCallback(e)
					}
				}()
			}
			var r, r1 func(trace.ScriptingCloseDoneInfo)
			if h1 != nil {
				r = h1(s)
			}
			if h2 != nil {
				r1 = h2(s)
			}
			return func(s trace.ScriptingCloseDoneInfo) {
				if options.panicCallback != nil {
					defer func() {
						if e := recover(); e != nil {
							options.panicCallback(e)
						}
					}()
				}
				if r != nil {
					r(s)
				}
				if r1 != nil {
					r1(s)
				}
			}
		}
	}
	return &ret
}
func onExecute(t *trace.Scripting, s trace.ScriptingExecuteStartInfo) func(trace.ScriptingExecuteDoneInfo) {
	fn := t.OnExecute
	if fn == nil {
		return func(trace.ScriptingExecuteDoneInfo) {
			return
		}
	}
	res := fn(s)
	if res == nil {
		return func(trace.ScriptingExecuteDoneInfo) {
			return
		}
	}
	return res
}
func onStreamExecute(t *trace.Scripting, s trace.ScriptingStreamExecuteStartInfo) func(trace.ScriptingStreamExecuteIntermediateInfo) func(trace.ScriptingStreamExecuteDoneInfo) {
	fn := t.OnStreamExecute
	if fn == nil {
		return func(trace.ScriptingStreamExecuteIntermediateInfo) func(trace.ScriptingStreamExecuteDoneInfo) {
			return func(trace.ScriptingStreamExecuteDoneInfo) {
				return
			}
		}
	}
	res := fn(s)
	if res == nil {
		return func(trace.ScriptingStreamExecuteIntermediateInfo) func(trace.ScriptingStreamExecuteDoneInfo) {
			return func(trace.ScriptingStreamExecuteDoneInfo) {
				return
			}
		}
	}
	return func(s trace.ScriptingStreamExecuteIntermediateInfo) func(trace.ScriptingStreamExecuteDoneInfo) {
		res := res(s)
		if res == nil {
			return func(trace.ScriptingStreamExecuteDoneInfo) {
				return
			}
		}
		return res
	}
}
func onExplain(t *trace.Scripting, s trace.ScriptingExplainStartInfo) func(trace.ScriptingExplainDoneInfo) {
	fn := t.OnExplain
	if fn == nil {
		return func(trace.ScriptingExplainDoneInfo) {
			return
		}
	}
	res := fn(s)
	if res == nil {
		return func(trace.ScriptingExplainDoneInfo) {
			return
		}
	}
	return res
}
func onClose(t *trace.Scripting, s trace.ScriptingCloseStartInfo) func(trace.ScriptingCloseDoneInfo) {
	fn := t.OnClose
	if fn == nil {
		return func(trace.ScriptingCloseDoneInfo) {
			return
		}
	}
	res := fn(s)
	if res == nil {
		return func(trace.ScriptingCloseDoneInfo) {
			return
		}
	}
	return res
}
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
func ScriptingOnExecute(t *trace.Scripting, c *context.Context, c1 trace.Call, query string, parameters trace.ScriptingQueryParameters) func(result trace.ScriptingResult, _ error) {
	var p trace.ScriptingExecuteStartInfo
	p.Context = c
	p.Call = c1
	p.Query = query
	p.Parameters = parameters
	res := onExecute(t, p)
	return func(result trace.ScriptingResult, e error) {
		var p trace.ScriptingExecuteDoneInfo
		p.Result = result
		p.Error = e
		res(p)
	}
}
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
func ScriptingOnStreamExecute(t *trace.Scripting, c *context.Context, c1 trace.Call, query string, parameters trace.ScriptingQueryParameters) func(error) func(error) {
	var p trace.ScriptingStreamExecuteStartInfo
	p.Context = c
	p.Call = c1
	p.Query = query
	p.Parameters = parameters
	res := onStreamExecute(t, p)
	return func(e error) func(error) {
		var p trace.ScriptingStreamExecuteIntermediateInfo
		p.Error = e
		res := res(p)
		return func(e error) {
			var p trace.ScriptingStreamExecuteDoneInfo
			p.Error = e
			res(p)
		}
	}
}
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
func ScriptingOnExplain(t *trace.Scripting, c *context.Context, c1 trace.Call, query string) func(plan string, _ error) {
	var p trace.ScriptingExplainStartInfo
	p.Context = c
	p.Call = c1
	p.Query = query
	res := onExplain(t, p)
	return func(plan string, e error) {
		var p trace.ScriptingExplainDoneInfo
		p.Plan = plan
		p.Error = e
		res(p)
	}
}
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
func ScriptingOnClose(t *trace.Scripting, c *context.Context, c1 trace.Call) func(error) {
	var p trace.ScriptingCloseStartInfo
	p.Context = c
	p.Call = c1
	res := onClose(t, p)
	return func(e error) {
		var p trace.ScriptingCloseDoneInfo
		p.Error = e
		res(p)
	}
}
