package otlptraces

import (
	commonpb 
	resourcepb 
	tracepb 

	
)

const (
	flagSampled     uint32 = 0x01
	flagHasIsRemote uint32 = uint32(tracepb.SpanFlags_SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK)
	flagIsRemote    uint32 = uint32(tracepb.SpanFlags_SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK)
)

func ( *Exporter) ( []traces.Record) *tracepb.TracesData {
	 := make(map[string]*tracepb.ScopeSpans)
	 := &tracepb.ResourceSpans{
		Resource: &resourcepb.Resource{
			Attributes: encodeAttrs(.Resource),
		},
		SchemaUrl: .SchemaURL,
	}
	for  := range  {
		 := &[]
		,  := [.Scope]
		if ! {
			 = &tracepb.ScopeSpans{
				Scope: &commonpb.InstrumentationScope{
					Name: .Scope,
				},
				SchemaUrl: .SchemaURL,
			}
			[.Scope] = 
			.ScopeSpans = append(.ScopeSpans, )
		}
		.Spans = append(.Spans, encodeSpan())
	}
	return &tracepb.TracesData{
		ResourceSpans: []*tracepb.ResourceSpans{},
	}
}

func ( *traces.Record) *tracepb.Span {
	var  uint32
	if .Ref.Sampled {
		 |= flagSampled
	}
	 := &tracepb.Span{
		TraceId:           .Ref.TraceID[:],
		SpanId:            .Ref.SpanID[:],
		Flags:             ,
		Name:              .Name,
		Kind:              tracepb.Span_SpanKind(.Kind),
		StartTimeUnixNano: uint64(.Start.UnixNano()),
		EndTimeUnixNano:   uint64(.End.UnixNano()),
		Attributes:        encodeAttrs(.Attrs),
	}
	if .Err != nil {
		.Status = &tracepb.Status{
			Code:    tracepb.Status_STATUS_CODE_ERROR,
			Message: .Err.Error(),
		}
	}
	if .Parent.IsValid() {
		.ParentSpanId = .Parent.SpanID[:]
		.Flags |= flagHasIsRemote
		if .Kind == traces.SpanKindServer || .Kind == traces.SpanKindConsumer {
			.Flags |= flagIsRemote
		}
	}
	return 
}

func ( []traces.Attr) []*commonpb.KeyValue {
	if len() == 0 {
		return nil
	}
	 := make([]*commonpb.KeyValue, len())
	for ,  := range  {
		 := &commonpb.KeyValue{Key: .Key}
		.Encode(otlpEncoder{
			set: func( *commonpb.AnyValue) {
				.Value = 
			},
		})
		if .Value == nil {
			.Value = &commonpb.AnyValue{}
		}
		[] = 
	}
	return 
}

type otlpEncoder struct {
	set func(*commonpb.AnyValue)
}

func ( otlpEncoder) ( string) {
	.set(&commonpb.AnyValue{
		Value: &commonpb.AnyValue_StringValue{StringValue: },
	})
}

func ( otlpEncoder) ( bool) {
	.set(&commonpb.AnyValue{
		Value: &commonpb.AnyValue_BoolValue{BoolValue: },
	})
}

func ( otlpEncoder) ( int64) {
	.set(&commonpb.AnyValue{
		Value: &commonpb.AnyValue_IntValue{IntValue: },
	})
}

func ( otlpEncoder) ( float64) {
	.set(&commonpb.AnyValue{
		Value: &commonpb.AnyValue_DoubleValue{DoubleValue: },
	})
}

func ( otlpEncoder) ( []byte) {
	.set(&commonpb.AnyValue{
		Value: &commonpb.AnyValue_BytesValue{BytesValue: },
	})
}

func ( otlpEncoder) ( func(traces.Encoder)) {
	var  []*commonpb.AnyValue
	(otlpEncoder{
		set: func( *commonpb.AnyValue) {
			 = append(, )
		},
	})
	.set(&commonpb.AnyValue{
		Value: &commonpb.AnyValue_ArrayValue{
			ArrayValue: &commonpb.ArrayValue{Values: },
		},
	})
}

func ( otlpEncoder) ( func(func(string) traces.Encoder)) {
	var  []*commonpb.KeyValue
	(func( string) traces.Encoder {
		 := &commonpb.KeyValue{Key: }
		 = append(, )
		return otlpEncoder{
			set: func( *commonpb.AnyValue) {
				.Value = 
			},
		}
	})
	.set(&commonpb.AnyValue{
		Value: &commonpb.AnyValue_KvlistValue{
			KvlistValue: &commonpb.KeyValueList{Values: },
		},
	})
}