// 
// Decompiled by Procyon v0.6.0
// 

package io.sentry;

import io.sentry.util.TracingUtils;
import io.sentry.exception.InvalidSentryTraceHeaderException;
import java.util.List;
import java.util.Arrays;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import io.sentry.protocol.SentryId;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class PropagationContext
{
    @NotNull
    private SentryId traceId;
    @NotNull
    private SpanId spanId;
    @Nullable
    private SpanId parentSpanId;
    @Nullable
    private Boolean sampled;
    @NotNull
    private final Baggage baggage;
    
    public static PropagationContext fromHeaders(@NotNull final ILogger logger, @Nullable final String sentryTraceHeader, @Nullable final String baggageHeader) {
        return fromHeaders(logger, sentryTraceHeader, Arrays.asList(baggageHeader));
    }
    
    @NotNull
    public static PropagationContext fromHeaders(@NotNull final ILogger logger, @Nullable final String sentryTraceHeaderString, @Nullable final List<String> baggageHeaderStrings) {
        if (sentryTraceHeaderString == null) {
            return new PropagationContext();
        }
        try {
            final SentryTraceHeader traceHeader = new SentryTraceHeader(sentryTraceHeaderString);
            final Baggage baggage = Baggage.fromHeader(baggageHeaderStrings, logger);
            return fromHeaders(traceHeader, baggage, null);
        }
        catch (final InvalidSentryTraceHeaderException e) {
            logger.log(SentryLevel.DEBUG, e, "Failed to parse Sentry trace header: %s", e.getMessage());
            return new PropagationContext();
        }
    }
    
    @NotNull
    public static PropagationContext fromHeaders(@NotNull final SentryTraceHeader sentryTraceHeader, @Nullable final Baggage baggage, @Nullable final SpanId spanId) {
        final SpanId spanIdToUse = (spanId == null) ? new SpanId() : spanId;
        return new PropagationContext(sentryTraceHeader.getTraceId(), spanIdToUse, sentryTraceHeader.getSpanId(), baggage, sentryTraceHeader.isSampled());
    }
    
    @NotNull
    public static PropagationContext fromExistingTrace(@NotNull final String traceId, @NotNull final String spanId, @Nullable final Double decisionSampleRate, @Nullable final Double decisionSampleRand) {
        return new PropagationContext(new SentryId(traceId), new SpanId(), new SpanId(spanId), TracingUtils.ensureBaggage(null, null, decisionSampleRate, decisionSampleRand), null);
    }
    
    public PropagationContext() {
        this(new SentryId(), new SpanId(), null, null, null);
    }
    
    public PropagationContext(@NotNull final PropagationContext propagationContext) {
        this(propagationContext.getTraceId(), propagationContext.getSpanId(), propagationContext.getParentSpanId(), propagationContext.getBaggage(), propagationContext.isSampled());
    }
    
    public PropagationContext(@NotNull final SentryId traceId, @NotNull final SpanId spanId, @Nullable final SpanId parentSpanId, @Nullable final Baggage baggage, @Nullable final Boolean sampled) {
        this.traceId = traceId;
        this.spanId = spanId;
        this.parentSpanId = parentSpanId;
        this.baggage = TracingUtils.ensureBaggage(baggage, sampled, null, null);
        this.sampled = sampled;
    }
    
    @NotNull
    public SentryId getTraceId() {
        return this.traceId;
    }
    
    public void setTraceId(@NotNull final SentryId traceId) {
        this.traceId = traceId;
    }
    
    @NotNull
    public SpanId getSpanId() {
        return this.spanId;
    }
    
    public void setSpanId(@NotNull final SpanId spanId) {
        this.spanId = spanId;
    }
    
    @Nullable
    public SpanId getParentSpanId() {
        return this.parentSpanId;
    }
    
    public void setParentSpanId(@Nullable final SpanId parentSpanId) {
        this.parentSpanId = parentSpanId;
    }
    
    @NotNull
    public Baggage getBaggage() {
        return this.baggage;
    }
    
    @Nullable
    public Boolean isSampled() {
        return this.sampled;
    }
    
    public void setSampled(@Nullable final Boolean sampled) {
        this.sampled = sampled;
    }
    
    @Nullable
    public TraceContext traceContext() {
        return this.baggage.toTraceContext();
    }
    
    @NotNull
    public SpanContext toSpanContext() {
        final SpanContext spanContext = new SpanContext(this.traceId, this.spanId, "default", null, null);
        spanContext.setOrigin("auto");
        return spanContext;
    }
    
    @NotNull
    public Double getSampleRand() {
        final Double sampleRand = this.baggage.getSampleRand();
        return (sampleRand == null) ? 0.0 : sampleRand;
    }
}
