package j1;

import androidx.collection.h0;
import com.google.android.gms.common.api.Api;
import j1.k;
import j1.n;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import rm.n0;
import y0.f4;
import y0.t3;

/* loaded from: classes.dex */
public abstract class p {

    /* renamed from: a */
    private static final dn.l f23517a = b.f23530p;

    /* renamed from: b */
    private static final t3 f23518b = new t3();

    /* renamed from: c */
    private static final Object f23519c = new Object();

    /* renamed from: d */
    private static n f23520d;

    /* renamed from: e */
    private static int f23521e;

    /* renamed from: f */
    private static final m f23522f;

    /* renamed from: g */
    private static final v f23523g;

    /* renamed from: h */
    private static List f23524h;

    /* renamed from: i */
    private static List f23525i;

    /* renamed from: j */
    private static final AtomicReference f23526j;

    /* renamed from: k */
    private static final k f23527k;

    /* renamed from: l */
    private static y0.g f23528l;

    /* loaded from: classes.dex */
    public static final class a extends kotlin.jvm.internal.v implements dn.l {

        /* renamed from: p */
        public static final a f23529p = new a();

        a() {
            super(1);
        }

        public final void a(n nVar) {
        }

        @Override // dn.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            a((n) obj);
            return n0.f34904a;
        }
    }

    /* loaded from: classes.dex */
    static final class b extends kotlin.jvm.internal.v implements dn.l {

        /* renamed from: p */
        public static final b f23530p = new b();

        b() {
            super(1);
        }

        public final void a(n nVar) {
        }

        @Override // dn.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            a((n) obj);
            return n0.f34904a;
        }
    }

    /* loaded from: classes.dex */
    public static final class c extends kotlin.jvm.internal.v implements dn.l {

        /* renamed from: p */
        final /* synthetic */ dn.l f23531p;

        /* renamed from: q */
        final /* synthetic */ dn.l f23532q;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        c(dn.l lVar, dn.l lVar2) {
            super(1);
            this.f23531p = lVar;
            this.f23532q = lVar2;
        }

        public final void a(Object obj) {
            this.f23531p.invoke(obj);
            this.f23532q.invoke(obj);
        }

        @Override // dn.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            a(obj);
            return n0.f34904a;
        }
    }

    /* loaded from: classes.dex */
    public static final class d extends kotlin.jvm.internal.v implements dn.l {

        /* renamed from: p */
        final /* synthetic */ dn.l f23533p;

        /* renamed from: q */
        final /* synthetic */ dn.l f23534q;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        d(dn.l lVar, dn.l lVar2) {
            super(1);
            this.f23533p = lVar;
            this.f23534q = lVar2;
        }

        public final void a(Object obj) {
            this.f23533p.invoke(obj);
            this.f23534q.invoke(obj);
        }

        @Override // dn.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            a(obj);
            return n0.f34904a;
        }
    }

    /* loaded from: classes.dex */
    public static final class e extends kotlin.jvm.internal.v implements dn.l {

        /* renamed from: p */
        final /* synthetic */ dn.l f23535p;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        e(dn.l lVar) {
            super(1);
            this.f23535p = lVar;
        }

        @Override // dn.l
        /* renamed from: a */
        public final k invoke(n nVar) {
            k kVar = (k) this.f23535p.invoke(nVar);
            synchronized (p.I()) {
                p.f23520d = p.f23520d.E(kVar.f());
                n0 n0Var = n0.f34904a;
            }
            return kVar;
        }
    }

    static {
        List k10;
        List k11;
        n.a aVar = n.f23505t;
        f23520d = aVar.a();
        f23521e = 2;
        f23522f = new m();
        f23523g = new v();
        k10 = sm.u.k();
        f23524h = k10;
        k11 = sm.u.k();
        f23525i = k11;
        int i10 = f23521e;
        f23521e = i10 + 1;
        j1.a aVar2 = new j1.a(i10, aVar.a());
        f23520d = f23520d.E(aVar2.f());
        AtomicReference atomicReference = new AtomicReference(aVar2);
        f23526j = atomicReference;
        f23527k = (k) atomicReference.get();
        f23528l = new y0.g(0);
    }

    public static final Object A(dn.l lVar) {
        Object obj;
        h0 E;
        Object a02;
        k kVar = f23527k;
        kotlin.jvm.internal.t.e(kVar, "null cannot be cast to non-null type androidx.compose.runtime.snapshots.GlobalSnapshot");
        synchronized (I()) {
            obj = f23526j.get();
            E = ((j1.a) obj).E();
            if (E != null) {
                f23528l.a(1);
            }
            a02 = a0((k) obj, lVar);
        }
        if (E != null) {
            try {
                List list = f23524h;
                int size = list.size();
                for (int i10 = 0; i10 < size; i10++) {
                    ((dn.p) list.get(i10)).invoke(a1.e.a(E), obj);
                }
            } finally {
                f23528l.a(-1);
            }
        }
        synchronized (I()) {
            C();
            if (E != null) {
                Object[] objArr = E.f1380b;
                long[] jArr = E.f1379a;
                int length = jArr.length - 2;
                if (length >= 0) {
                    int i11 = 0;
                    while (true) {
                        long j10 = jArr[i11];
                        if ((((~j10) << 7) & j10 & (-9187201950435737472L)) != -9187201950435737472L) {
                            int i12 = 8 - ((~(i11 - length)) >>> 31);
                            for (int i13 = 0; i13 < i12; i13++) {
                                if ((255 & j10) < 128) {
                                    U((x) objArr[(i11 << 3) + i13]);
                                }
                                j10 >>= 8;
                            }
                            if (i12 != 8) {
                                break;
                            }
                        }
                        if (i11 == length) {
                            break;
                        }
                        i11++;
                    }
                }
                n0 n0Var = n0.f34904a;
            }
        }
        return a02;
    }

    public static final void B() {
        A(a.f23529p);
    }

    public static final void C() {
        v vVar = f23523g;
        int e10 = vVar.e();
        int i10 = 0;
        int i11 = 0;
        while (true) {
            if (i10 >= e10) {
                break;
            }
            f4 f4Var = vVar.f()[i10];
            if ((f4Var != null ? f4Var.get() : null) != null && !(!T((x) r5))) {
                if (i11 != i10) {
                    vVar.f()[i11] = f4Var;
                    vVar.d()[i11] = vVar.d()[i10];
                }
                i11++;
            }
            i10++;
        }
        for (int i12 = i11; i12 < e10; i12++) {
            vVar.f()[i12] = null;
            vVar.d()[i12] = 0;
        }
        if (i11 != e10) {
            vVar.g(i11);
        }
    }

    public static final k D(k kVar, dn.l lVar, boolean z10) {
        boolean z11 = kVar instanceof j1.c;
        if (z11 || kVar == null) {
            return new b0(z11 ? (j1.c) kVar : null, lVar, null, false, z10);
        }
        return new c0(kVar, lVar, false, z10);
    }

    public static /* synthetic */ k E(k kVar, dn.l lVar, boolean z10, int i10, Object obj) {
        if ((i10 & 2) != 0) {
            lVar = null;
        }
        if ((i10 & 4) != 0) {
            z10 = false;
        }
        return D(kVar, lVar, z10);
    }

    public static final z F(z zVar) {
        z W;
        k.a aVar = k.f23492e;
        k c10 = aVar.c();
        z W2 = W(zVar, c10.f(), c10.g());
        if (W2 != null) {
            return W2;
        }
        synchronized (I()) {
            k c11 = aVar.c();
            W = W(zVar, c11.f(), c11.g());
        }
        if (W != null) {
            return W;
        }
        V();
        throw new rm.k();
    }

    public static final z G(z zVar, k kVar) {
        z W = W(zVar, kVar.f(), kVar.g());
        if (W != null) {
            return W;
        }
        V();
        throw new rm.k();
    }

    public static final k H() {
        k kVar = (k) f23518b.a();
        return kVar == null ? (k) f23526j.get() : kVar;
    }

    public static final Object I() {
        return f23519c;
    }

    public static final k J() {
        return f23527k;
    }

    public static final dn.l K(dn.l lVar, dn.l lVar2, boolean z10) {
        if (!z10) {
            lVar2 = null;
        }
        return (lVar == null || lVar2 == null || lVar == lVar2) ? lVar == null ? lVar2 : lVar : new c(lVar, lVar2);
    }

    public static /* synthetic */ dn.l L(dn.l lVar, dn.l lVar2, boolean z10, int i10, Object obj) {
        if ((i10 & 4) != 0) {
            z10 = true;
        }
        return K(lVar, lVar2, z10);
    }

    public static final dn.l M(dn.l lVar, dn.l lVar2) {
        return (lVar == null || lVar2 == null || lVar == lVar2) ? lVar == null ? lVar2 : lVar : new d(lVar, lVar2);
    }

    public static final z N(z zVar, x xVar) {
        z d02 = d0(xVar);
        if (d02 != null) {
            d02.h(Api.BaseClientBuilder.API_PRIORITY_OTHER);
            return d02;
        }
        z d10 = zVar.d();
        d10.h(Api.BaseClientBuilder.API_PRIORITY_OTHER);
        d10.g(xVar.g());
        kotlin.jvm.internal.t.e(d10, "null cannot be cast to non-null type T of androidx.compose.runtime.snapshots.SnapshotKt.newOverwritableRecordLocked$lambda$16");
        xVar.r(d10);
        kotlin.jvm.internal.t.e(d10, "null cannot be cast to non-null type T of androidx.compose.runtime.snapshots.SnapshotKt.newOverwritableRecordLocked");
        return d10;
    }

    public static final z O(z zVar, x xVar, k kVar) {
        z P;
        synchronized (I()) {
            P = P(zVar, xVar, kVar);
        }
        return P;
    }

    private static final z P(z zVar, x xVar, k kVar) {
        z N = N(zVar, xVar);
        N.c(zVar);
        N.h(kVar.f());
        return N;
    }

    public static final void Q(k kVar, x xVar) {
        kVar.w(kVar.j() + 1);
        dn.l k10 = kVar.k();
        if (k10 != null) {
            k10.invoke(xVar);
        }
    }

    public static final Map R(j1.c cVar, j1.c cVar2, n nVar) {
        long[] jArr;
        int i10;
        HashMap hashMap;
        long[] jArr2;
        int i11;
        HashMap hashMap2;
        int i12;
        z W;
        h0 E = cVar2.E();
        int f10 = cVar.f();
        HashMap hashMap3 = null;
        if (E == null) {
            return null;
        }
        n D = cVar2.g().E(cVar2.f()).D(cVar2.F());
        Object[] objArr = E.f1380b;
        long[] jArr3 = E.f1379a;
        int length = jArr3.length - 2;
        if (length >= 0) {
            HashMap hashMap4 = null;
            int i13 = 0;
            while (true) {
                long j10 = jArr3[i13];
                if ((((~j10) << 7) & j10 & (-9187201950435737472L)) != -9187201950435737472L) {
                    int i14 = 8;
                    int i15 = 8 - ((~(i13 - length)) >>> 31);
                    int i16 = 0;
                    while (i16 < i15) {
                        if ((255 & j10) < 128) {
                            x xVar = (x) objArr[(i13 << 3) + i16];
                            z g10 = xVar.g();
                            z W2 = W(g10, f10, nVar);
                            if (W2 == null || (W = W(g10, f10, D)) == null || kotlin.jvm.internal.t.b(W2, W)) {
                                jArr2 = jArr3;
                                i11 = f10;
                            } else {
                                jArr2 = jArr3;
                                i11 = f10;
                                z W3 = W(g10, cVar2.f(), cVar2.g());
                                if (W3 == null) {
                                    V();
                                    throw new rm.k();
                                }
                                z m10 = xVar.m(W, W2, W3);
                                if (m10 == null) {
                                    return null;
                                }
                                if (hashMap4 == null) {
                                    hashMap4 = new HashMap();
                                }
                                hashMap4.put(W2, m10);
                                hashMap4 = hashMap4;
                            }
                            hashMap2 = null;
                            i12 = 8;
                        } else {
                            jArr2 = jArr3;
                            i11 = f10;
                            hashMap2 = hashMap3;
                            i12 = i14;
                        }
                        j10 >>= i12;
                        i16++;
                        hashMap3 = hashMap2;
                        i14 = i12;
                        jArr3 = jArr2;
                        f10 = i11;
                    }
                    jArr = jArr3;
                    i10 = f10;
                    hashMap = hashMap3;
                    if (i15 != i14) {
                        return hashMap4;
                    }
                } else {
                    jArr = jArr3;
                    i10 = f10;
                    hashMap = hashMap3;
                }
                if (i13 == length) {
                    hashMap3 = hashMap4;
                    break;
                }
                i13++;
                hashMap3 = hashMap;
                jArr3 = jArr;
                f10 = i10;
            }
        }
        return hashMap3;
    }

    public static final z S(z zVar, x xVar, k kVar, z zVar2) {
        z N;
        if (kVar.i()) {
            kVar.p(xVar);
        }
        int f10 = kVar.f();
        if (zVar2.f() == f10) {
            return zVar2;
        }
        synchronized (I()) {
            N = N(zVar, xVar);
        }
        N.h(f10);
        if (zVar2.f() != 1) {
            kVar.p(xVar);
        }
        return N;
    }

    private static final boolean T(x xVar) {
        z zVar;
        int e10 = f23522f.e(f23521e);
        z zVar2 = null;
        z zVar3 = null;
        int i10 = 0;
        for (z g10 = xVar.g(); g10 != null; g10 = g10.e()) {
            int f10 = g10.f();
            if (f10 != 0) {
                if (f10 >= e10) {
                    i10++;
                } else if (zVar2 == null) {
                    i10++;
                    zVar2 = g10;
                } else {
                    if (g10.f() < zVar2.f()) {
                        zVar = zVar2;
                        zVar2 = g10;
                    } else {
                        zVar = g10;
                    }
                    if (zVar3 == null) {
                        zVar3 = xVar.g();
                        z zVar4 = zVar3;
                        while (true) {
                            if (zVar3 == null) {
                                zVar3 = zVar4;
                                break;
                            }
                            if (zVar3.f() >= e10) {
                                break;
                            }
                            if (zVar4.f() < zVar3.f()) {
                                zVar4 = zVar3;
                            }
                            zVar3 = zVar3.e();
                        }
                    }
                    zVar2.h(0);
                    zVar2.c(zVar3);
                    zVar2 = zVar;
                }
            }
        }
        return i10 > 1;
    }

    public static final void U(x xVar) {
        if (T(xVar)) {
            f23523g.a(xVar);
        }
    }

    public static final Void V() {
        throw new IllegalStateException("Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied".toString());
    }

    public static final z W(z zVar, int i10, n nVar) {
        z zVar2 = null;
        while (zVar != null) {
            if (f0(zVar, i10, nVar) && (zVar2 == null || zVar2.f() < zVar.f())) {
                zVar2 = zVar;
            }
            zVar = zVar.e();
        }
        if (zVar2 != null) {
            return zVar2;
        }
        return null;
    }

    public static final z X(z zVar, x xVar) {
        z W;
        k.a aVar = k.f23492e;
        k c10 = aVar.c();
        dn.l h10 = c10.h();
        if (h10 != null) {
            h10.invoke(xVar);
        }
        z W2 = W(zVar, c10.f(), c10.g());
        if (W2 != null) {
            return W2;
        }
        synchronized (I()) {
            k c11 = aVar.c();
            z g10 = xVar.g();
            kotlin.jvm.internal.t.e(g10, "null cannot be cast to non-null type T of androidx.compose.runtime.snapshots.SnapshotKt.readable$lambda$9");
            W = W(g10, c11.f(), c11.g());
            if (W == null) {
                V();
                throw new rm.k();
            }
        }
        return W;
    }

    public static final void Y(int i10) {
        f23522f.f(i10);
    }

    public static final Void Z() {
        throw new IllegalStateException("Cannot modify a state object in a read-only snapshot".toString());
    }

    public static final Object a0(k kVar, dn.l lVar) {
        Object invoke = lVar.invoke(f23520d.z(kVar.f()));
        synchronized (I()) {
            int i10 = f23521e;
            f23521e = i10 + 1;
            f23520d = f23520d.z(kVar.f());
            f23526j.set(new j1.a(i10, f23520d));
            kVar.d();
            f23520d = f23520d.E(i10);
            n0 n0Var = n0.f34904a;
        }
        return invoke;
    }

    public static final k b0(dn.l lVar) {
        return (k) A(new e(lVar));
    }

    public static final int c0(int i10, n nVar) {
        int a10;
        int C = nVar.C(i10);
        synchronized (I()) {
            a10 = f23522f.a(C);
        }
        return a10;
    }

    private static final z d0(x xVar) {
        int e10 = f23522f.e(f23521e) - 1;
        n a10 = n.f23505t.a();
        z zVar = null;
        for (z g10 = xVar.g(); g10 != null; g10 = g10.e()) {
            if (g10.f() == 0) {
                return g10;
            }
            if (f0(g10, e10, a10)) {
                if (zVar != null) {
                    return g10.f() < zVar.f() ? g10 : zVar;
                }
                zVar = g10;
            }
        }
        return null;
    }

    private static final boolean e0(int i10, int i11, n nVar) {
        return (i11 == 0 || i11 > i10 || nVar.B(i11)) ? false : true;
    }

    private static final boolean f0(z zVar, int i10, n nVar) {
        return e0(i10, zVar.f(), nVar);
    }

    public static final void g0(k kVar) {
        int e10;
        if (f23520d.B(kVar.f())) {
            return;
        }
        StringBuilder sb2 = new StringBuilder();
        sb2.append("Snapshot is not open: id=");
        sb2.append(kVar.f());
        sb2.append(", disposed=");
        sb2.append(kVar.e());
        sb2.append(", applied=");
        j1.c cVar = kVar instanceof j1.c ? (j1.c) kVar : null;
        sb2.append(cVar != null ? Boolean.valueOf(cVar.D()) : "read-only");
        sb2.append(", lowestPin=");
        synchronized (I()) {
            e10 = f23522f.e(-1);
        }
        sb2.append(e10);
        throw new IllegalStateException(sb2.toString().toString());
    }

    public static final z h0(z zVar, x xVar, k kVar) {
        z W;
        if (kVar.i()) {
            kVar.p(xVar);
        }
        int f10 = kVar.f();
        z W2 = W(zVar, f10, kVar.g());
        if (W2 == null) {
            V();
            throw new rm.k();
        }
        if (W2.f() == kVar.f()) {
            return W2;
        }
        synchronized (I()) {
            W = W(xVar.g(), f10, kVar.g());
            if (W == null) {
                V();
                throw new rm.k();
            }
            if (W.f() != f10) {
                W = P(W, xVar, kVar);
            }
        }
        kotlin.jvm.internal.t.e(W, "null cannot be cast to non-null type T of androidx.compose.runtime.snapshots.SnapshotKt.writableRecord");
        if (W2.f() != 1) {
            kVar.p(xVar);
        }
        return W;
    }

    public static final n z(n nVar, int i10, int i11) {
        while (i10 < i11) {
            nVar = nVar.E(i10);
            i10++;
        }
        return nVar;
    }
}
