package traces

import (
	
	
	
)

// Tracer creates spans and delivers finished [Record] values to a [Handler].
//
// A Tracer is immutable and safe for concurrent use; With* methods return a
// shallow copy with the updated setting (or the receiver if unchanged).
// Like [log/slog.Logger], a nil Tracer is invalid and panics on use.
// Pass [DiscardHandler] to [New] to disable tracing explicitly.
type Tracer struct {
	handler Handler
	scope   string
	attrs   []Attr
	sampler Sampler
	now     func() time.Time
	ids     IDSource
}

// New returns a new [Tracer] instance with the given non-nil [Handler].
// It defaults to [ParentBased] with [AlwaysSample], [time.Now], and
// [RandomIDs].
func ( Handler) *Tracer {
	if  == nil {
		panic("traces: nil Handler")
	}
	return &Tracer{
		handler: ,
		sampler: ParentBased(AlwaysSample),
		now:     time.Now,
		ids:     RandomIDs(),
	}
}

// Handler returns the underlying [Handler].
func ( *Tracer) () Handler {
	return .handler
}

// WithScope returns a [Tracer] that attributes spans to the given
// instrumentation scope name. If name matches the current scope, it returns
// the receiver.
func ( *Tracer) ( string) *Tracer {
	if .scope ==  {
		return 
	}
	 := *
	.scope = 
	return &
}

// WithAttrs returns a [Tracer] that includes attrs on every span it starts.
// If attrs is empty, it returns the receiver.
func ( *Tracer) ( ...Attr) *Tracer {
	if len() == 0 {
		return 
	}
	 := *
	.attrs = append(slices.Clip(.attrs), ...)
	return &
}

// WithSampler returns a [Tracer] using s for sampling decisions. If s is nil,
// it restores the default [ParentBased] sampler with [AlwaysSample].
func ( *Tracer) ( Sampler) *Tracer {
	if  == nil {
		 = ParentBased(AlwaysSample)
	}
	 := *
	.sampler = 
	return &
}

// WithTime returns a [Tracer] using now as its time source. If now is nil, it
// restores [time.Now].
func ( *Tracer) ( func() time.Time) *Tracer {
	if  == nil {
		 = time.Now
	}
	 := *
	.now = 
	return &
}

// WithIDs returns a [Tracer] using ids to generate trace and span identifiers.
// If ids is nil, it restores a fresh [RandomIDs] generator.
func ( *Tracer) ( IDSource) *Tracer {
	if  == nil {
		 = RandomIDs()
	}
	 := *
	.ids = 
	return &
}

// Start begins a new span with the given explicit parent, kind, name, and
// start-time attributes.
//
// A zero parent starts a new trace; a valid parent continues that trace (for
// example from [FromContext] or [Span.Ref]). The context ctx is passed through
// to the [Sampler] and is never inspected for an implicit parent.
//
// Every returned [Span] should eventually be finished by calling [Span.End].
func ( *Tracer) (
	 context.Context,
	 SpanRef,
	 SpanKind,
	 string,
	 ...Attr,
) *Span {
	if .handler == DiscardHandler() {
		return &Span{ref: }
	}

	 := SpanRef{
		TraceID: .TraceID,
	}
	if !.IsValid() {
		.TraceID = .ids.NewTraceID()
	}
	.SpanID = .ids.NewSpanID()

	 := 
	if len(.attrs) > 0 {
		 = make([]Attr, 0, len(.attrs)+len())
		 = append(, .attrs...)
		 = append(, ...)
	}

	 := .sampler(, SamplingParams{
		Parent:  ,
		TraceID: .TraceID,
		Name:    ,
		Kind:    ,
		Attrs:   ,
		Scope:   .scope,
	})
	if ! {
		// Unsampled spans are non-recording but carry a fresh span ID with
		// Sampled == false so downstream peers honor the sampling decision.
		return &Span{ref: }
	}

	.Sampled = true
	 := newSpanState(Record{
		Ref:    ,
		Parent: ,
		Scope:  .scope,
		Name:   ,
		Kind:   ,
		Start:  .now(),
		Attrs:  ,
	})

	return &Span{
		tracer: ,
		ref:    ,
		state:  ,
	}
}