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

package io.sentry.featureflags;

import java.util.Iterator;
import java.util.List;
import io.sentry.protocol.FeatureFlag;
import java.util.ArrayList;
import io.sentry.protocol.FeatureFlags;
import io.sentry.ISentryLifecycleToken;
import java.util.LinkedHashMap;
import org.jetbrains.annotations.NotNull;
import io.sentry.util.AutoClosableReentrantLock;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public final class SpanFeatureFlagBuffer implements IFeatureFlagBuffer
{
    private static final int MAX_SIZE = 10;
    @Nullable
    private Map<String, Boolean> flags;
    @NotNull
    private final AutoClosableReentrantLock lock;
    
    private SpanFeatureFlagBuffer() {
        this.flags = null;
        this.lock = new AutoClosableReentrantLock();
    }
    
    @Override
    public void add(@Nullable final String flag, @Nullable final Boolean result) {
        if (flag == null || result == null) {
            return;
        }
        try (final ISentryLifecycleToken ignored = this.lock.acquire()) {
            if (this.flags == null) {
                this.flags = new LinkedHashMap<String, Boolean>(10);
            }
            if (this.flags.size() < 10 || this.flags.containsKey(flag)) {
                this.flags.put(flag, result);
            }
        }
    }
    
    @Nullable
    @Override
    public FeatureFlags getFeatureFlags() {
        try (final ISentryLifecycleToken ignored = this.lock.acquire()) {
            if (this.flags == null || this.flags.isEmpty()) {
                try (final ISentryLifecycleToken ignored = null) {}
                return;
            }
            final List<FeatureFlag> featureFlags = new ArrayList<FeatureFlag>(this.flags.size());
            for (final Map.Entry<String, Boolean> entry : this.flags.entrySet()) {
                featureFlags.add(new FeatureFlag(entry.getKey(), entry.getValue()));
            }
            return new FeatureFlags(featureFlags);
        }
    }
    
    @NotNull
    @Override
    public IFeatureFlagBuffer clone() {
        return create();
    }
    
    @NotNull
    public static IFeatureFlagBuffer create() {
        return new SpanFeatureFlagBuffer();
    }
}
