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

package io.sentry.protocol;

import io.sentry.ObjectReader;
import io.sentry.JsonDeserializer;
import java.io.IOException;
import io.sentry.ILogger;
import io.sentry.ObjectWriter;
import io.sentry.util.StringUtils;
import io.sentry.SentryUUID;
import io.sentry.util.UUIDStringUtils;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;
import io.sentry.util.LazyEvaluator;
import io.sentry.JsonSerializable;

public final class SentryId implements JsonSerializable
{
    public static final SentryId EMPTY_ID;
    @NotNull
    private final LazyEvaluator<String> lazyStringValue;
    
    public SentryId() {
        this((UUID)null);
    }
    
    public SentryId(@Nullable final UUID uuid) {
        if (uuid != null) {
            this.lazyStringValue = new LazyEvaluator<String>(() -> this.normalize(UUIDStringUtils.toSentryIdString(uuid)));
        }
        else {
            this.lazyStringValue = new LazyEvaluator<String>(SentryUUID::generateSentryId);
        }
    }
    
    public SentryId(@NotNull final String sentryIdString) {
        final String normalized = StringUtils.normalizeUUID(sentryIdString);
        if (normalized.length() != 32 && normalized.length() != 36) {
            throw new IllegalArgumentException("String representation of SentryId has either 32 (UUID no dashes) or 36 characters long (completed UUID). Received: " + sentryIdString);
        }
        if (normalized.length() == 36) {
            this.lazyStringValue = new LazyEvaluator<String>(() -> this.normalize(normalized));
        }
        else {
            this.lazyStringValue = new LazyEvaluator<String>(() -> normalized);
        }
    }
    
    @Override
    public String toString() {
        return this.lazyStringValue.getValue();
    }
    
    @Override
    public boolean equals(@Nullable final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        final SentryId sentryId = (SentryId)o;
        return this.lazyStringValue.getValue().equals(sentryId.lazyStringValue.getValue());
    }
    
    @Override
    public int hashCode() {
        return this.lazyStringValue.getValue().hashCode();
    }
    
    @NotNull
    private String normalize(@NotNull final String uuidString) {
        return StringUtils.normalizeUUID(uuidString).replace("-", "");
    }
    
    @Override
    public void serialize(@NotNull final ObjectWriter writer, @NotNull final ILogger logger) throws IOException {
        writer.value(this.toString());
    }
    
    static {
        EMPTY_ID = new SentryId("00000000-0000-0000-0000-000000000000".replace("-", ""));
    }
    
    public static final class Deserializer implements JsonDeserializer<SentryId>
    {
        @NotNull
        @Override
        public SentryId deserialize(@NotNull final ObjectReader reader, @NotNull final ILogger logger) throws Exception {
            return new SentryId(reader.nextString());
        }
    }
}
