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

package io.sentry;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import io.sentry.util.Objects;
import java.util.Collections;
import java.util.WeakHashMap;
import org.jetbrains.annotations.NotNull;
import java.util.Map;

public final class DuplicateEventDetectionEventProcessor implements EventProcessor
{
    @NotNull
    private final Map<Throwable, Object> capturedObjects;
    @NotNull
    private final SentryOptions options;
    
    public DuplicateEventDetectionEventProcessor(@NotNull final SentryOptions options) {
        this.capturedObjects = Collections.synchronizedMap(new WeakHashMap<Throwable, Object>());
        this.options = Objects.requireNonNull(options, "options are required");
    }
    
    @Nullable
    @Override
    public SentryEvent process(@NotNull final SentryEvent event, @NotNull final Hint hint) {
        if (this.options.isEnableDeduplication()) {
            final Throwable throwable = event.getThrowable();
            if (throwable != null) {
                if (this.capturedObjects.containsKey(throwable) || containsAnyKey(this.capturedObjects, allCauses(throwable))) {
                    this.options.getLogger().log(SentryLevel.DEBUG, "Duplicate Exception detected. Event %s will be discarded.", event.getEventId());
                    return null;
                }
                this.capturedObjects.put(throwable, null);
            }
        }
        else {
            this.options.getLogger().log(SentryLevel.DEBUG, "Event deduplication is disabled.", new Object[0]);
        }
        return event;
    }
    
    private static <T> boolean containsAnyKey(@NotNull final Map<T, Object> map, @NotNull final List<T> list) {
        for (final T entry : list) {
            if (map.containsKey(entry)) {
                return true;
            }
        }
        return false;
    }
    
    @NotNull
    private static List<Throwable> allCauses(@NotNull final Throwable throwable) {
        final List<Throwable> causes = new ArrayList<Throwable>();
        for (Throwable ex = throwable; ex.getCause() != null; ex = ex.getCause()) {
            causes.add(ex.getCause());
        }
        return causes;
    }
    
    @Nullable
    @Override
    public Long getOrder() {
        return 1000L;
    }
}
