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

package io.sentry;

import io.sentry.vendor.gson.stream.JsonToken;
import java.io.IOException;
import java.util.Iterator;
import org.jetbrains.annotations.TestOnly;
import io.sentry.util.CollectionUtils;
import org.jetbrains.annotations.ApiStatus;
import io.sentry.util.thread.IThreadChecker;
import io.sentry.util.Objects;
import io.sentry.featureflags.SpanFeatureFlagBuffer;
import java.util.concurrent.ConcurrentHashMap;
import io.sentry.featureflags.IFeatureFlagBuffer;
import java.util.Map;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import io.sentry.protocol.SentryId;

public class SpanContext implements JsonUnknown, JsonSerializable
{
    public static final String TYPE = "trace";
    public static final String DEFAULT_ORIGIN = "manual";
    @NotNull
    private final SentryId traceId;
    @NotNull
    private final SpanId spanId;
    @Nullable
    private SpanId parentSpanId;
    @Nullable
    private transient TracesSamplingDecision samplingDecision;
    @NotNull
    protected String op;
    @Nullable
    protected String description;
    @Nullable
    protected SpanStatus status;
    @NotNull
    protected Map<String, String> tags;
    @Nullable
    protected String origin;
    @NotNull
    protected Map<String, Object> data;
    @Nullable
    private Map<String, Object> unknown;
    @NotNull
    private Instrumenter instrumenter;
    @Nullable
    protected Baggage baggage;
    @NotNull
    protected IFeatureFlagBuffer featureFlags;
    @NotNull
    private SentryId profilerId;
    
    public SpanContext(@NotNull final String operation, @Nullable final TracesSamplingDecision samplingDecision) {
        this(new SentryId(), new SpanId(), operation, null, samplingDecision);
    }
    
    public SpanContext(@NotNull final String operation) {
        this(new SentryId(), new SpanId(), operation, null, null);
    }
    
    public SpanContext(@NotNull final SentryId traceId, @NotNull final SpanId spanId, @NotNull final String operation, @Nullable final SpanId parentSpanId, @Nullable final TracesSamplingDecision samplingDecision) {
        this(traceId, spanId, parentSpanId, operation, null, samplingDecision, null, "manual");
    }
    
    @ApiStatus.Internal
    public SpanContext(@NotNull final SentryId traceId, @NotNull final SpanId spanId, @Nullable final SpanId parentSpanId, @NotNull final String operation, @Nullable final String description, @Nullable final TracesSamplingDecision samplingDecision, @Nullable final SpanStatus status, @Nullable final String origin) {
        this.tags = new ConcurrentHashMap<String, String>();
        this.origin = "manual";
        this.data = new ConcurrentHashMap<String, Object>();
        this.instrumenter = Instrumenter.SENTRY;
        this.featureFlags = SpanFeatureFlagBuffer.create();
        this.profilerId = SentryId.EMPTY_ID;
        this.traceId = Objects.requireNonNull(traceId, "traceId is required");
        this.spanId = Objects.requireNonNull(spanId, "spanId is required");
        this.op = Objects.requireNonNull(operation, "operation is required");
        this.parentSpanId = parentSpanId;
        this.description = description;
        this.status = status;
        this.origin = origin;
        this.setSamplingDecision(samplingDecision);
        final IThreadChecker threadChecker = ScopesAdapter.getInstance().getOptions().getThreadChecker();
        this.data.put("thread.id", String.valueOf(threadChecker.currentThreadSystemId()));
        this.data.put("thread.name", threadChecker.getCurrentThreadName());
    }
    
    public SpanContext(@NotNull final SpanContext spanContext) {
        this.tags = new ConcurrentHashMap<String, String>();
        this.origin = "manual";
        this.data = new ConcurrentHashMap<String, Object>();
        this.instrumenter = Instrumenter.SENTRY;
        this.featureFlags = SpanFeatureFlagBuffer.create();
        this.profilerId = SentryId.EMPTY_ID;
        this.traceId = spanContext.traceId;
        this.spanId = spanContext.spanId;
        this.parentSpanId = spanContext.parentSpanId;
        this.setSamplingDecision(spanContext.samplingDecision);
        this.op = spanContext.op;
        this.description = spanContext.description;
        this.status = spanContext.status;
        final Map<String, String> copiedTags = CollectionUtils.newConcurrentHashMap(spanContext.tags);
        if (copiedTags != null) {
            this.tags = copiedTags;
        }
        final Map<String, Object> copiedUnknown = CollectionUtils.newConcurrentHashMap(spanContext.unknown);
        if (copiedUnknown != null) {
            this.unknown = copiedUnknown;
        }
        this.baggage = spanContext.baggage;
        final Map<String, Object> copiedData = CollectionUtils.newConcurrentHashMap(spanContext.data);
        if (copiedData != null) {
            this.data = copiedData;
        }
    }
    
    public void setOperation(@NotNull final String operation) {
        this.op = Objects.requireNonNull(operation, "operation is required");
    }
    
    public void setTag(@Nullable final String name, @Nullable final String value) {
        if (name == null) {
            return;
        }
        if (value == null) {
            this.tags.remove(name);
        }
        else {
            this.tags.put(name, value);
        }
    }
    
    public void setDescription(@Nullable final String description) {
        this.description = description;
    }
    
    public void setStatus(@Nullable final SpanStatus status) {
        this.status = status;
    }
    
    @NotNull
    public SentryId getTraceId() {
        return this.traceId;
    }
    
    @NotNull
    public SpanId getSpanId() {
        return this.spanId;
    }
    
    @Nullable
    @TestOnly
    public SpanId getParentSpanId() {
        return this.parentSpanId;
    }
    
    @NotNull
    public String getOperation() {
        return this.op;
    }
    
    @Nullable
    public String getDescription() {
        return this.description;
    }
    
    @Nullable
    public SpanStatus getStatus() {
        return this.status;
    }
    
    @NotNull
    public Map<String, String> getTags() {
        return this.tags;
    }
    
    @Nullable
    public TracesSamplingDecision getSamplingDecision() {
        return this.samplingDecision;
    }
    
    @Nullable
    public Boolean getSampled() {
        if (this.samplingDecision == null) {
            return null;
        }
        return this.samplingDecision.getSampled();
    }
    
    @Nullable
    public Boolean getProfileSampled() {
        if (this.samplingDecision == null) {
            return null;
        }
        return this.samplingDecision.getProfileSampled();
    }
    
    @ApiStatus.Internal
    public void setSampled(@Nullable final Boolean sampled) {
        if (sampled == null) {
            this.setSamplingDecision(null);
        }
        else {
            this.setSamplingDecision(new TracesSamplingDecision(sampled));
        }
    }
    
    @ApiStatus.Internal
    public void setSampled(@Nullable final Boolean sampled, @Nullable final Boolean profileSampled) {
        if (sampled == null) {
            this.setSamplingDecision(null);
        }
        else if (profileSampled == null) {
            this.setSamplingDecision(new TracesSamplingDecision(sampled));
        }
        else {
            this.setSamplingDecision(new TracesSamplingDecision(sampled, null, profileSampled, null));
        }
    }
    
    @ApiStatus.Internal
    public void setSamplingDecision(@Nullable final TracesSamplingDecision samplingDecision) {
        this.samplingDecision = samplingDecision;
        if (this.baggage != null) {
            this.baggage.setValuesFromSamplingDecision(this.samplingDecision);
        }
    }
    
    @Nullable
    public String getOrigin() {
        return this.origin;
    }
    
    public void setOrigin(@Nullable final String origin) {
        this.origin = origin;
    }
    
    @NotNull
    public Instrumenter getInstrumenter() {
        return this.instrumenter;
    }
    
    public void setInstrumenter(@NotNull final Instrumenter instrumenter) {
        this.instrumenter = instrumenter;
    }
    
    @Nullable
    public Baggage getBaggage() {
        return this.baggage;
    }
    
    @NotNull
    public Map<String, Object> getData() {
        return this.data;
    }
    
    public void setData(@Nullable final String key, @Nullable final Object value) {
        if (key == null) {
            return;
        }
        if (value == null) {
            this.data.remove(key);
        }
        else {
            this.data.put(key, value);
        }
    }
    
    @ApiStatus.Internal
    public SpanContext copyForChild(@NotNull final String operation, @Nullable final SpanId parentSpanId, @Nullable final SpanId spanId) {
        return new SpanContext(this.traceId, (spanId == null) ? new SpanId() : spanId, parentSpanId, operation, null, this.samplingDecision, null, "manual");
    }
    
    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof SpanContext)) {
            return false;
        }
        final SpanContext that = (SpanContext)o;
        return this.traceId.equals(that.traceId) && this.spanId.equals(that.spanId) && Objects.equals(this.parentSpanId, that.parentSpanId) && this.op.equals(that.op) && Objects.equals(this.description, that.description) && this.getStatus() == that.getStatus();
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.traceId, this.spanId, this.parentSpanId, this.op, this.description, this.getStatus());
    }
    
    @ApiStatus.Internal
    @NotNull
    public SentryId getProfilerId() {
        return this.profilerId;
    }
    
    @ApiStatus.Internal
    public void setProfilerId(@NotNull final SentryId profilerId) {
        this.profilerId = profilerId;
    }
    
    @ApiStatus.Internal
    public void addFeatureFlag(@Nullable final String flag, @Nullable final Boolean result) {
        this.featureFlags.add(flag, result);
    }
    
    @ApiStatus.Internal
    @NotNull
    public IFeatureFlagBuffer getFeatureFlagBuffer() {
        return this.featureFlags;
    }
    
    @Override
    public void serialize(@NotNull final ObjectWriter writer, @NotNull final ILogger logger) throws IOException {
        writer.beginObject();
        writer.name("trace_id");
        this.traceId.serialize(writer, logger);
        writer.name("span_id");
        this.spanId.serialize(writer, logger);
        if (this.parentSpanId != null) {
            writer.name("parent_span_id");
            this.parentSpanId.serialize(writer, logger);
        }
        writer.name("op").value(this.op);
        if (this.description != null) {
            writer.name("description").value(this.description);
        }
        if (this.getStatus() != null) {
            writer.name("status").value(logger, this.getStatus());
        }
        if (this.origin != null) {
            writer.name("origin").value(logger, this.origin);
        }
        if (!this.tags.isEmpty()) {
            writer.name("tags").value(logger, this.tags);
        }
        if (!this.data.isEmpty()) {
            writer.name("data").value(logger, this.data);
        }
        if (this.unknown != null) {
            for (final String key : this.unknown.keySet()) {
                final Object value = this.unknown.get(key);
                writer.name(key).value(logger, value);
            }
        }
        writer.endObject();
    }
    
    @Nullable
    @Override
    public Map<String, Object> getUnknown() {
        return this.unknown;
    }
    
    @Override
    public void setUnknown(@Nullable final Map<String, Object> unknown) {
        this.unknown = unknown;
    }
    
    public static final class JsonKeys
    {
        public static final String TRACE_ID = "trace_id";
        public static final String SPAN_ID = "span_id";
        public static final String PARENT_SPAN_ID = "parent_span_id";
        public static final String OP = "op";
        public static final String DESCRIPTION = "description";
        public static final String STATUS = "status";
        public static final String TAGS = "tags";
        public static final String ORIGIN = "origin";
        public static final String DATA = "data";
    }
    
    public static final class Deserializer implements JsonDeserializer<SpanContext>
    {
        @NotNull
        @Override
        public SpanContext deserialize(@NotNull final ObjectReader reader, @NotNull final ILogger logger) throws Exception {
            reader.beginObject();
            SentryId traceId = null;
            SpanId spanId = null;
            SpanId parentSpanId = null;
            String op = null;
            String description = null;
            SpanStatus status = null;
            String origin = null;
            Map<String, String> tags = null;
            Map<String, Object> data = null;
            Map<String, Object> unknown = null;
            while (reader.peek() == JsonToken.NAME) {
                final String nextName2;
                final String nextName = nextName2 = reader.nextName();
                switch (nextName2) {
                    case "trace_id": {
                        traceId = new SentryId.Deserializer().deserialize(reader, logger);
                        continue;
                    }
                    case "span_id": {
                        spanId = new SpanId.Deserializer().deserialize(reader, logger);
                        continue;
                    }
                    case "parent_span_id": {
                        parentSpanId = reader.nextOrNull(logger, (JsonDeserializer<SpanId>)new SpanId.Deserializer());
                        continue;
                    }
                    case "op": {
                        op = reader.nextString();
                        continue;
                    }
                    case "description": {
                        description = reader.nextString();
                        continue;
                    }
                    case "status": {
                        status = reader.nextOrNull(logger, (JsonDeserializer<SpanStatus>)new SpanStatus.Deserializer());
                        continue;
                    }
                    case "origin": {
                        origin = reader.nextString();
                        continue;
                    }
                    case "tags": {
                        tags = CollectionUtils.newConcurrentHashMap((Map<String, String>)reader.nextObjectOrNull());
                        continue;
                    }
                    case "data": {
                        data = (Map)reader.nextObjectOrNull();
                        continue;
                    }
                    default: {
                        if (unknown == null) {
                            unknown = new ConcurrentHashMap<String, Object>();
                        }
                        reader.nextUnknown(logger, unknown, nextName);
                        continue;
                    }
                }
            }
            if (traceId == null) {
                final String message = "Missing required field \"trace_id\"";
                final Exception exception = new IllegalStateException(message);
                logger.log(SentryLevel.ERROR, message, exception);
                throw exception;
            }
            if (spanId == null) {
                final String message = "Missing required field \"span_id\"";
                final Exception exception = new IllegalStateException(message);
                logger.log(SentryLevel.ERROR, message, exception);
                throw exception;
            }
            if (op == null) {
                op = "";
            }
            final SpanContext spanContext = new SpanContext(traceId, spanId, op, parentSpanId, null);
            spanContext.setDescription(description);
            spanContext.setStatus(status);
            spanContext.setOrigin(origin);
            if (tags != null) {
                spanContext.tags = tags;
            }
            if (data != null) {
                spanContext.data = data;
            }
            spanContext.setUnknown(unknown);
            reader.endObject();
            return spanContext;
        }
    }
}
