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

package io.sentry;

import java.util.regex.Matcher;
import io.sentry.exception.InvalidSentryTraceHeaderException;
import java.util.regex.Pattern;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import io.sentry.protocol.SentryId;

public final class SentryTraceHeader
{
    public static final String SENTRY_TRACE_HEADER = "sentry-trace";
    @NotNull
    private final SentryId traceId;
    @NotNull
    private final SpanId spanId;
    @Nullable
    private final Boolean sampled;
    private static final Pattern SENTRY_TRACEPARENT_HEADER_REGEX;
    
    public SentryTraceHeader(@NotNull final SentryId traceId, @NotNull final SpanId spanId, @Nullable final Boolean sampled) {
        this.traceId = traceId;
        this.spanId = spanId;
        this.sampled = sampled;
    }
    
    public SentryTraceHeader(@NotNull final String value) throws InvalidSentryTraceHeaderException {
        final Matcher matcher = SentryTraceHeader.SENTRY_TRACEPARENT_HEADER_REGEX.matcher(value);
        final boolean matchesExist = matcher.matches();
        if (!matchesExist) {
            throw new InvalidSentryTraceHeaderException(value);
        }
        this.traceId = new SentryId(matcher.group(1));
        this.spanId = new SpanId(matcher.group(2));
        final String sampled = matcher.group(3);
        this.sampled = ((sampled == null) ? null : Boolean.valueOf("1".equals(sampled.substring(1))));
    }
    
    @NotNull
    public String getName() {
        return "sentry-trace";
    }
    
    @NotNull
    public String getValue() {
        if (this.sampled != null) {
            return String.format("%s-%s-%s", this.traceId, this.spanId, this.sampled ? "1" : "0");
        }
        return String.format("%s-%s", this.traceId, this.spanId);
    }
    
    @NotNull
    public SentryId getTraceId() {
        return this.traceId;
    }
    
    @NotNull
    public SpanId getSpanId() {
        return this.spanId;
    }
    
    @Nullable
    public Boolean isSampled() {
        return this.sampled;
    }
    
    static {
        SENTRY_TRACEPARENT_HEADER_REGEX = Pattern.compile("^[ \\t]*([0-9a-f]{32})-([0-9a-f]{16})(-[01])?[ \\t]*$", 2);
    }
}
