package traces

import (
	
	
	
)

// Encoder encodes a single attribute value into a backend-specific
// representation.
type Encoder interface {
	// String encodes a string value.
	String(v string)
	// Bool encodes a bool value.
	Bool(v bool)
	// Int64 encodes an int64 value.
	Int64(v int64)
	// Float64 encodes a float64 value.
	Float64(v float64)
	// Bytes encodes a byte slice value.
	Bytes(v []byte)
	// Array encodes a sequence of values by invoking f with an element
	// [Encoder]. Each method call on elem appends one element to the array.
	Array(f func(elem Encoder))
	// Map encodes a key-value map by invoking f with a field constructor.
	// Calling field(key) returns an [Encoder] for that entry’s value.
	Map(f func(field func(key string) Encoder))
}

// Marshaler encodes itself into an [Encoder]. It is invoked lazily by
// consumers (such as exporters or test recorders) and is never called for
// unsampled or discarded spans. Implementations must remain safe to read until
// export completes.
type Marshaler interface {
	// MarshalTrace encodes the value into e.
	MarshalTrace(e Encoder)
}

// MarshalerFunc is a function that implements the [Marshaler] interface.
type MarshalerFunc func(e Encoder)

// MarshalTrace implements the [Marshaler] interface.
func ( MarshalerFunc) ( Encoder) {
	()
}

type attrKind uint8

const (
	kindEmpty attrKind = iota
	kindString
	kindBool
	kindInt64
	kindFloat64
	kindBytes
	kindStrings
	kindArray
	kindMap
	kindStringer
	kindMarshaler
)

// Attr is a key-value attribute attached to a span or resource.
// Scalar and string values are stored inline without heap allocations.
type Attr struct {
	// Key is the attribute name.
	Key string

	kind attrKind
	num  uint64
	str  string
	val  any
}

// Encode writes the attribute’s value into e.
func ( Attr) ( Encoder) {
	switch .kind {
	case kindString:
		.String(.str)
	case kindBool:
		.Bool(.num != 0)
	case kindInt64:
		.Int64(int64(.num))
	case kindFloat64:
		.Float64(math.Float64frombits(.num))
	case kindBytes:
		.Bytes(.val.([]byte))
	case kindStrings:
		 := .val.([]string)
		.Array(func( Encoder) {
			for ,  := range  {
				.String()
			}
		})
	case kindArray:
		.Array(.val.(func(Encoder)))
	case kindMap:
		 := .val.([]Attr)
		.Map(func( func( string) Encoder) {
			for ,  := range  {
				.((.Key))
			}
		})
	case kindStringer:
		.String(.val.(fmt.Stringer).String())
	case kindMarshaler:
		.val.(Marshaler).MarshalTrace()
	}
}

// String returns a string [Attr].
func (,  string) Attr {
	return Attr{Key: , kind: kindString, str: }
}

// Bool returns a bool [Attr].
func ( string,  bool) Attr {
	var  uint64
	if  {
		 = 1
	}
	return Attr{Key: , kind: kindBool, num: }
}

// Int returns an int64 [Attr] from an int.
func ( string,  int) Attr {
	return Attr{Key: , kind: kindInt64, num: uint64(int64())}
}

// Int64 returns an int64 [Attr].
func ( string,  int64) Attr {
	return Attr{Key: , kind: kindInt64, num: uint64()}
}

// Float64 returns a float64 [Attr].
func ( string,  float64) Attr {
	return Attr{Key: , kind: kindFloat64, num: math.Float64bits()}
}

// Duration returns an int64 nanoseconds [Attr].
func ( string,  time.Duration) Attr {
	return Attr{Key: , kind: kindInt64, num: uint64(int64())}
}

// Bytes returns a byte slice [Attr]. The caller must not modify b afterwards.
func ( string,  []byte) Attr {
	return Attr{Key: , kind: kindBytes, val: }
}

// Strings returns a string array [Attr]. The caller must not modify vs
// afterwards.
func ( string,  ...string) Attr {
	return Attr{Key: , kind: kindStrings, val: }
}

// Array returns an array [Attr] whose elements are encoded lazily by f.
func ( string,  func( Encoder)) Attr {
	if  == nil {
		return Attr{Key: }
	}
	return Attr{Key: , kind: kindArray, val: }
}

// Map returns a nested key-value map [Attr]. The caller must not modify attrs
// afterwards.
func ( string,  ...Attr) Attr {
	return Attr{Key: , kind: kindMap, val: }
}

// Stringer returns a string [Attr] whose value is computed lazily via v.String
// during [Attr.Encode]. If v is nil, the attribute encodes nothing.
func ( string,  fmt.Stringer) Attr {
	if  == nil {
		return Attr{Key: }
	}
	return Attr{Key: , kind: kindStringer, val: }
}

// Marshal returns an [Attr] encoded lazily via m. If m is nil, the attribute
// encodes nothing.
func ( string,  Marshaler) Attr {
	if  == nil {
		return Attr{Key: }
	}
	return Attr{Key: , kind: kindMarshaler, val: }
}