// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package trace // import "go.opentelemetry.io/otel/trace"

import (
	
)

const (
	// FlagsSampled is a bitmask with the sampled bit set. A SpanContext
	// with the sampling bit set means the span is sampled.
	FlagsSampled = TraceFlags(0x01)

	errInvalidHexID errorConst = "trace-id and span-id can only contain [0-9a-f] characters, all lowercase"

	errInvalidTraceIDLength errorConst = "hex encoded trace-id must have length equals to 32"
	errNilTraceID           errorConst = "trace-id can't be all zero"

	errInvalidSpanIDLength errorConst = "hex encoded span-id must have length equals to 16"
	errNilSpanID           errorConst = "span-id can't be all zero"
)

type errorConst string

func ( errorConst) () string {
	return string()
}

// TraceID is a unique identity of a trace.
// nolint:revive // revive complains about stutter of `trace.TraceID`.
type TraceID [16]byte

var (
	nilTraceID TraceID
	_          json.Marshaler = nilTraceID
)

// IsValid reports whether the trace TraceID is valid. A valid trace ID does
// not consist of zeros only.
func ( TraceID) () bool {
	return  != nilTraceID
}

// MarshalJSON implements a custom marshal function to encode TraceID
// as a hex string.
func ( TraceID) () ([]byte, error) {
	 := [32 + 2]byte{0: '"', 33: '"'}
	 := .hexBytes()
	copy([1:], [:])
	return [:], nil
}

// String returns the hex string representation form of a TraceID.
func ( TraceID) () string {
	 := .hexBytes()
	return string([:])
}

// hexBytes returns the hex string representation form of a TraceID.
func ( TraceID) () [32]byte {
	return [32]byte{
		hexLU[[0x0]>>4], hexLU[[0x0]&0xf],
		hexLU[[0x1]>>4], hexLU[[0x1]&0xf],
		hexLU[[0x2]>>4], hexLU[[0x2]&0xf],
		hexLU[[0x3]>>4], hexLU[[0x3]&0xf],
		hexLU[[0x4]>>4], hexLU[[0x4]&0xf],
		hexLU[[0x5]>>4], hexLU[[0x5]&0xf],
		hexLU[[0x6]>>4], hexLU[[0x6]&0xf],
		hexLU[[0x7]>>4], hexLU[[0x7]&0xf],
		hexLU[[0x8]>>4], hexLU[[0x8]&0xf],
		hexLU[[0x9]>>4], hexLU[[0x9]&0xf],
		hexLU[[0xa]>>4], hexLU[[0xa]&0xf],
		hexLU[[0xb]>>4], hexLU[[0xb]&0xf],
		hexLU[[0xc]>>4], hexLU[[0xc]&0xf],
		hexLU[[0xd]>>4], hexLU[[0xd]&0xf],
		hexLU[[0xe]>>4], hexLU[[0xe]&0xf],
		hexLU[[0xf]>>4], hexLU[[0xf]&0xf],
	}
}

// SpanID is a unique identity of a span in a trace.
type SpanID [8]byte

var (
	nilSpanID SpanID
	_         json.Marshaler = nilSpanID
)

// IsValid reports whether the SpanID is valid. A valid SpanID does not consist
// of zeros only.
func ( SpanID) () bool {
	return  != nilSpanID
}

// MarshalJSON implements a custom marshal function to encode SpanID
// as a hex string.
func ( SpanID) () ([]byte, error) {
	 := [16 + 2]byte{0: '"', 17: '"'}
	 := .hexBytes()
	copy([1:], [:])
	return [:], nil
}

// String returns the hex string representation form of a SpanID.
func ( SpanID) () string {
	 := .hexBytes()
	return string([:])
}

func ( SpanID) () [16]byte {
	return [16]byte{
		hexLU[[0]>>4], hexLU[[0]&0xf],
		hexLU[[1]>>4], hexLU[[1]&0xf],
		hexLU[[2]>>4], hexLU[[2]&0xf],
		hexLU[[3]>>4], hexLU[[3]&0xf],
		hexLU[[4]>>4], hexLU[[4]&0xf],
		hexLU[[5]>>4], hexLU[[5]&0xf],
		hexLU[[6]>>4], hexLU[[6]&0xf],
		hexLU[[7]>>4], hexLU[[7]&0xf],
	}
}

// TraceIDFromHex returns a TraceID from a hex string if it is compliant with
// the W3C trace-context specification.  See more at
// https://www.w3.org/TR/trace-context/#trace-id
// nolint:revive // revive complains about stutter of `trace.TraceIDFromHex`.
func ( string) (TraceID, error) {
	if len() != 32 {
		return [16]byte{}, errInvalidTraceIDLength
	}
	var  [16]byte
	 := byte(0)
	for  := 0;  < len();  += 4 {
		[/2] = (hexRev[[]] << 4) | hexRev[[+1]]
		[/2+1] = (hexRev[[+2]] << 4) | hexRev[[+3]]
		 |= hexRev[[]] | hexRev[[+1]] | hexRev[[+2]] | hexRev[[+3]]
	}
	// If the upper 4 bits of any byte are not zero, there was an invalid hex
	// character since invalid hex characters are 0xff in hexRev.
	if &0xf0 != 0 {
		return [16]byte{}, errInvalidHexID
	}
	// If we didn't set any bits, then h was all zeros.
	if  == 0 {
		return [16]byte{}, errNilTraceID
	}
	return , nil
}

// SpanIDFromHex returns a SpanID from a hex string if it is compliant
// with the w3c trace-context specification.
// See more at https://www.w3.org/TR/trace-context/#parent-id
func ( string) (SpanID, error) {
	if len() != 16 {
		return [8]byte{}, errInvalidSpanIDLength
	}
	var  [8]byte
	 := byte(0)
	for  := 0;  < len();  += 4 {
		[/2] = (hexRev[[]] << 4) | hexRev[[+1]]
		[/2+1] = (hexRev[[+2]] << 4) | hexRev[[+3]]
		 |= hexRev[[]] | hexRev[[+1]] | hexRev[[+2]] | hexRev[[+3]]
	}
	// If the upper 4 bits of any byte are not zero, there was an invalid hex
	// character since invalid hex characters are 0xff in hexRev.
	if &0xf0 != 0 {
		return [8]byte{}, errInvalidHexID
	}
	// If we didn't set any bits, then h was all zeros.
	if  == 0 {
		return [8]byte{}, errNilSpanID
	}
	return , nil
}

// TraceFlags contains flags that can be set on a SpanContext.
type TraceFlags byte //nolint:revive // revive complains about stutter of `trace.TraceFlags`.

// IsSampled reports whether the sampling bit is set in the TraceFlags.
func ( TraceFlags) () bool {
	return &FlagsSampled == FlagsSampled
}

// WithSampled sets the sampling bit in a new copy of the TraceFlags.
func ( TraceFlags) ( bool) TraceFlags { // nolint:revive  // sampled is not a control flag.
	if  {
		return  | FlagsSampled
	}

	return  &^ FlagsSampled
}

// MarshalJSON implements a custom marshal function to encode TraceFlags
// as a hex string.
func ( TraceFlags) () ([]byte, error) {
	 := [2 + 2]byte{0: '"', 3: '"'}
	 := .hexBytes()
	copy([1:], [:])
	return [:], nil
}

// String returns the hex string representation form of TraceFlags.
func ( TraceFlags) () string {
	 := .hexBytes()
	return string([:])
}

func ( TraceFlags) () [2]byte {
	return [2]byte{hexLU[>>4], hexLU[&0xf]}
}

// SpanContextConfig contains mutable fields usable for constructing
// an immutable SpanContext.
type SpanContextConfig struct {
	TraceID    TraceID
	SpanID     SpanID
	TraceFlags TraceFlags
	TraceState TraceState
	Remote     bool
}

// NewSpanContext constructs a SpanContext using values from the provided
// SpanContextConfig.
func ( SpanContextConfig) SpanContext {
	return SpanContext{
		traceID:    .TraceID,
		spanID:     .SpanID,
		traceFlags: .TraceFlags,
		traceState: .TraceState,
		remote:     .Remote,
	}
}

// SpanContext contains identifying trace information about a Span.
type SpanContext struct {
	traceID    TraceID
	spanID     SpanID
	traceFlags TraceFlags
	traceState TraceState
	remote     bool
}

var _ json.Marshaler = SpanContext{}

// IsValid reports whether the SpanContext is valid. A valid span context has a
// valid TraceID and SpanID.
func ( SpanContext) () bool {
	return .HasTraceID() && .HasSpanID()
}

// IsRemote reports whether the SpanContext represents a remotely-created Span.
func ( SpanContext) () bool {
	return .remote
}

// WithRemote returns a copy of sc with the Remote property set to remote.
func ( SpanContext) ( bool) SpanContext {
	return SpanContext{
		traceID:    .traceID,
		spanID:     .spanID,
		traceFlags: .traceFlags,
		traceState: .traceState,
		remote:     ,
	}
}

// TraceID returns the TraceID from the SpanContext.
func ( SpanContext) () TraceID {
	return .traceID
}

// HasTraceID reports whether the SpanContext has a valid TraceID.
func ( SpanContext) () bool {
	return .traceID.IsValid()
}

// WithTraceID returns a new SpanContext with the TraceID replaced.
func ( SpanContext) ( TraceID) SpanContext {
	return SpanContext{
		traceID:    ,
		spanID:     .spanID,
		traceFlags: .traceFlags,
		traceState: .traceState,
		remote:     .remote,
	}
}

// SpanID returns the SpanID from the SpanContext.
func ( SpanContext) () SpanID {
	return .spanID
}

// HasSpanID reports whether the SpanContext has a valid SpanID.
func ( SpanContext) () bool {
	return .spanID.IsValid()
}

// WithSpanID returns a new SpanContext with the SpanID replaced.
func ( SpanContext) ( SpanID) SpanContext {
	return SpanContext{
		traceID:    .traceID,
		spanID:     ,
		traceFlags: .traceFlags,
		traceState: .traceState,
		remote:     .remote,
	}
}

// TraceFlags returns the flags from the SpanContext.
func ( SpanContext) () TraceFlags {
	return .traceFlags
}

// IsSampled reports whether the sampling bit is set in the SpanContext's TraceFlags.
func ( SpanContext) () bool {
	return .traceFlags.IsSampled()
}

// WithTraceFlags returns a new SpanContext with the TraceFlags replaced.
func ( SpanContext) ( TraceFlags) SpanContext {
	return SpanContext{
		traceID:    .traceID,
		spanID:     .spanID,
		traceFlags: ,
		traceState: .traceState,
		remote:     .remote,
	}
}

// TraceState returns the TraceState from the SpanContext.
func ( SpanContext) () TraceState {
	return .traceState
}

// WithTraceState returns a new SpanContext with the TraceState replaced.
func ( SpanContext) ( TraceState) SpanContext {
	return SpanContext{
		traceID:    .traceID,
		spanID:     .spanID,
		traceFlags: .traceFlags,
		traceState: ,
		remote:     .remote,
	}
}

// Equal reports whether two SpanContext values are equal.
func ( SpanContext) ( SpanContext) bool {
	return .traceID == .traceID &&
		.spanID == .spanID &&
		.traceFlags == .traceFlags &&
		.traceState.String() == .traceState.String() &&
		.remote == .remote
}

// MarshalJSON implements a custom marshal function to encode a SpanContext.
func ( SpanContext) () ([]byte, error) {
	return json.Marshal(SpanContextConfig{
		TraceID:    .traceID,
		SpanID:     .spanID,
		TraceFlags: .traceFlags,
		TraceState: .traceState,
		Remote:     .remote,
	})
}