package qg;

import com.adobe.marketing.mobile.SharedStateResult;
import com.adobe.marketing.mobile.SharedStateStatus;
import com.google.android.gms.common.api.Api;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import kotlin.jvm.internal.t;

/* loaded from: classes4.dex */
public final class l {

    /* renamed from: d, reason: collision with root package name */
    public static final a f33784d = new a(null);

    /* renamed from: a, reason: collision with root package name */
    private final String f33785a;

    /* renamed from: b, reason: collision with root package name */
    private final TreeMap f33786b;

    /* renamed from: c, reason: collision with root package name */
    private final String f33787c;

    /* loaded from: classes4.dex */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(kotlin.jvm.internal.k kVar) {
            this();
        }
    }

    public l(String name) {
        t.g(name, "name");
        this.f33787c = name;
        this.f33785a = "SharedStateManager(" + name + ')';
        this.f33786b = new TreeMap();
    }

    private final boolean d(int i10, k kVar) {
        if (this.f33786b.ceilingEntry(Integer.valueOf(i10)) == null) {
            this.f33786b.put(Integer.valueOf(i10), kVar);
            return true;
        }
        wg.t.e("MobileCore", this.f33785a, "Cannot create " + this.f33787c + " shared state at version " + i10 + ". More recent state exists.", new Object[0]);
        return false;
    }

    public final synchronized boolean a() {
        return this.f33786b.size() == 0;
    }

    public final synchronized SharedStateResult b(int i10) {
        SharedStateResult sharedStateResult;
        k kVar;
        Map.Entry floorEntry = this.f33786b.floorEntry(Integer.valueOf(i10));
        k kVar2 = floorEntry != null ? (k) floorEntry.getValue() : null;
        if (kVar2 != null) {
            return kVar2.a();
        }
        Map.Entry firstEntry = this.f33786b.firstEntry();
        if (firstEntry == null || (kVar = (k) firstEntry.getValue()) == null || (sharedStateResult = kVar.a()) == null) {
            sharedStateResult = new SharedStateResult(SharedStateStatus.NONE, null);
        }
        return sharedStateResult;
    }

    public final synchronized SharedStateResult c(int i10) {
        SortedMap tailMap = this.f33786b.descendingMap().tailMap(Integer.valueOf(i10));
        t.f(tailMap, "states.descendingMap().tailMap(version)");
        Iterator it = tailMap.entrySet().iterator();
        while (it.hasNext()) {
            k kVar = (k) ((Map.Entry) it.next()).getValue();
            if (kVar.b() != SharedStateStatus.PENDING) {
                return kVar.a();
            }
        }
        Map.Entry firstEntry = this.f33786b.firstEntry();
        k kVar2 = firstEntry != null ? (k) firstEntry.getValue() : null;
        return (kVar2 != null ? kVar2.b() : null) == SharedStateStatus.SET ? kVar2.a() : new SharedStateResult(SharedStateStatus.NONE, null);
    }

    public final synchronized boolean e(int i10) {
        return d(i10, new k(i10, SharedStateStatus.PENDING, b(Api.BaseClientBuilder.API_PRIORITY_OTHER).b()));
    }

    public final synchronized boolean f(int i10, Map map) {
        return d(i10, new k(i10, SharedStateStatus.SET, map));
    }

    public final synchronized boolean g(int i10, Map map) {
        k kVar = (k) this.f33786b.get(Integer.valueOf(i10));
        if (kVar == null) {
            return false;
        }
        t.f(kVar, "states[version] ?: return false");
        if (kVar.b() != SharedStateStatus.PENDING) {
            return false;
        }
        this.f33786b.put(Integer.valueOf(i10), new k(i10, SharedStateStatus.SET, map));
        return true;
    }
}
