{{define "minWireVersion" -}}
{{- if $.MinWireVersion -}}
 && (desc.WireVersion != nil && desc.WireVersion.Includes({{$.MinWireVersion}}))
{{- end}}{{end}}

{{define "minWireVersionRequired"}}
{{- if $.MinWireVersionRequired}}
if desc.WireVersion == nil || !desc.WireVersion.Includes({{$.MinWireVersionRequired}}) {
	return nil, errors.New("the '{{$.ParameterName}}' command parameter requires a minimum server wire version of {{$.MinWireVersionRequired}}")
}
{{end}}{{end}}

{{define "commandParamCollectionDatabase" -}}
header := bsoncore.Value{Type: bsontype.String, Data: bsoncore.AppendString(nil, {{$.ShortName}}.collection)}
if {{$.ShortName}}.collection == "" {
	header = bsoncore.Value{Type: bsontype.Int32, Data: []byte{0x01, 0x00, 0x00, 0x00}}
}
dst = bsoncore.AppendValueElement(dst, "{{$.Command.Name}}", header)
{{end}}

{{define "commandParamDatabase"}}
dst = bsoncore.AppendInt32Element(dst, "{{$.Command.Name}}", 1)
{{end}}

{{define "commandParamCollection" -}}
dst = bsoncore.AppendStringElement(dst, "{{$.Command.Name}}", {{$.ShortName}}.collection)
{{end}}

{{define "commandParamDocument" -}}
if {{$.ShortName}}.{{$.Name}} != nil {{template "minWireVersion" $}} {
    {{- template "minWireVersionRequired" $ -}}
	dst = bsoncore.AppendDocumentElement(dst, "{{$.ParameterName}}", {{$.ShortName}}.{{$.Name}})
}
{{end}}

{{define "commandParamArray" -}}
if {{$.ShortName}}.{{$.Name}} != nil {{template "minWireVersion" $}} {
    {{- template "minWireVersionRequired" $ -}}
	dst = bsoncore.AppendArrayElement(dst, "{{$.ParameterName}}", {{$.ShortName}}.{{$.Name}})
}
{{end}}

{{define "commandParamValue" -}}
if {{$.ShortName}}.{{$.Name}}.Type != bsontype.Type(0) {{template "minWireVersion" $}} {
    {{- template "minWireVersionRequired" $ -}}
	dst = bsoncore.AppendValueElement(dst, "{{$.ParameterName}}", {{$.ShortName}}.{{$.Name}})
}
{{end}}

{{define "commandParamInt32" -}}
if {{$.ShortName}}.{{$.Name}} != nil {{template "minWireVersion" $}} {
    {{- template "minWireVersionRequired" $ -}}
	dst = bsoncore.AppendInt32Element(dst, "{{$.ParameterName}}", *{{$.ShortName}}.{{$.Name}})
}
{{end}}

{{define "commandParamInt64" -}}
if {{$.ShortName}}.{{$.Name}} != nil {{template "minWireVersion" $}} {
    {{- template "minWireVersionRequired" $ -}}
	dst = bsoncore.AppendInt64Element(dst, "{{$.ParameterName}}", *{{$.ShortName}}.{{$.Name}})
}
{{end}}

{{define "commandParamDouble" -}}
if {{$.ShortName}}.{{$.Name}} != nil {{template "minWireVersion" $}} {
    {{- template "minWireVersionRequired" $ -}}
	dst = bsoncore.AppendDoubleElement(dst, "{{$.ParameterName}}", *{{$.ShortName}}.{{$.Name}})
}
{{end}}

{{define "commandParamBoolean" -}}
if {{$.ShortName}}.{{$.Name}} != nil {{template "minWireVersion" $}} {
    {{- template "minWireVersionRequired" $ -}}
	dst = bsoncore.AppendBooleanElement(dst, "{{$.ParameterName}}", *{{$.ShortName}}.{{$.Name}})
}
{{end}}

{{define "commandParamString" -}}
if {{$.ShortName}}.{{$.Name}} != nil {{template "minWireVersion" $}} {
    {{- template "minWireVersionRequired" $ -}}
	dst = bsoncore.AppendStringElement(dst, "{{$.ParameterName}}", *{{$.ShortName}}.{{$.Name}})
}
{{end}}
