package a3;

import V4.C0763h0;
import V4.InterfaceC0765i0;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import m3.AbstractC2467b;
import okhttp3.OkHttpClient;
import w4.C3137A;

/* JADX INFO: loaded from: classes.dex */
public final class F extends C4.j implements J4.e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f10494a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public /* synthetic */ Object f10495b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final /* synthetic */ AtomicReference f10496c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final /* synthetic */ J4.a f10497d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final /* synthetic */ Set f10498e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final /* synthetic */ AtomicInteger f10499f;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final /* synthetic */ int f10500j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final /* synthetic */ N2.f f10501k;
    public final /* synthetic */ String l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final /* synthetic */ Integer f10502m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final /* synthetic */ Integer f10503n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final /* synthetic */ boolean f10504o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final /* synthetic */ Map f10505p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final /* synthetic */ N2.r f10506q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final /* synthetic */ OkHttpClient f10507r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final /* synthetic */ e5.e f10508s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final /* synthetic */ String f10509t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public final /* synthetic */ AtomicReference f10510u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public final /* synthetic */ kotlin.jvm.internal.C f10511v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final /* synthetic */ CopyOnWriteArrayList f10512w;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public F(AtomicReference atomicReference, J4.a aVar, Set set, AtomicInteger atomicInteger, int i6, N2.f fVar, String str, Integer num, Integer num2, boolean z6, Map map, N2.r rVar, OkHttpClient okHttpClient, e5.e eVar, String str2, AtomicReference atomicReference2, kotlin.jvm.internal.C c6, CopyOnWriteArrayList copyOnWriteArrayList, A4.d dVar) {
        super(2, dVar);
        this.f10496c = atomicReference;
        this.f10497d = aVar;
        this.f10498e = set;
        this.f10499f = atomicInteger;
        this.f10500j = i6;
        this.f10501k = fVar;
        this.l = str;
        this.f10502m = num;
        this.f10503n = num2;
        this.f10504o = z6;
        this.f10505p = map;
        this.f10506q = rVar;
        this.f10507r = okHttpClient;
        this.f10508s = eVar;
        this.f10509t = str2;
        this.f10510u = atomicReference2;
        this.f10511v = c6;
        this.f10512w = copyOnWriteArrayList;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        F f6 = new F(this.f10496c, this.f10497d, this.f10498e, this.f10499f, this.f10500j, this.f10501k, this.l, this.f10502m, this.f10503n, this.f10504o, this.f10505p, this.f10506q, this.f10507r, this.f10508s, this.f10509t, this.f10510u, this.f10511v, this.f10512w, dVar);
        f6.f10495b = obj;
        return f6;
    }

    @Override // J4.e
    public final Object invoke(Object obj, Object obj2) {
        return ((F) create((V4.C) obj, (A4.d) obj2)).invokeSuspend(C3137A.f25453a);
    }

    @Override // C4.a
    public final Object invokeSuspend(Object obj) throws Exception {
        boolean zAdd;
        int i6;
        boolean z6;
        F f6;
        V4.C c6;
        B4.a aVar = B4.a.f492a;
        int i7 = this.f10494a;
        if (i7 == 0) {
            AbstractC2467b.l(obj);
            V4.C c7 = (V4.C) this.f10495b;
            if (this.f10496c.get() != null) {
                return C3137A.f25453a;
            }
            if (!((Boolean) this.f10497d.invoke()).booleanValue()) {
                return C3137A.f25453a;
            }
            Set set = this.f10498e;
            String str = this.f10509t;
            synchronized (set) {
                zAdd = set.add(str);
            }
            if (!zAdd) {
                return C3137A.f25453a;
            }
            if (this.f10496c.get() != null) {
                Set set2 = this.f10498e;
                String str2 = this.f10509t;
                synchronized (set2) {
                    set2.remove(str2);
                }
                return C3137A.f25453a;
            }
            AtomicInteger atomicInteger = this.f10499f;
            int i8 = this.f10500j;
            if (atomicInteger != null) {
                do {
                    i6 = atomicInteger.get();
                    if (i6 >= i8) {
                        z6 = false;
                        break;
                    }
                } while (!atomicInteger.compareAndSet(i6, i6 + 1));
            }
            z6 = true;
            if (!z6) {
                Set set3 = this.f10498e;
                String str3 = this.f10509t;
                synchronized (set3) {
                    set3.remove(str3);
                }
                return C3137A.f25453a;
            }
            if (this.f10496c.get() != null) {
                return C3137A.f25453a;
            }
            N2.f fVar = this.f10501k;
            String str4 = this.l;
            Integer num = this.f10502m;
            Integer num2 = this.f10503n;
            boolean z7 = this.f10504o;
            Map map = this.f10505p;
            N2.r rVar = this.f10506q;
            OkHttpClient okHttpClient = this.f10507r;
            e5.e eVar = this.f10508s;
            this.f10495b = c7;
            this.f10494a = 1;
            f6 = this;
            Object objQ = W.a.Q(fVar, str4, num, num2, z7, map, rVar, okHttpClient, eVar, null, f6);
            if (objQ == aVar) {
                return aVar;
            }
            c6 = c7;
            obj = objQ;
            return C3137A.f25453a;
        }
        if (i7 != 1) {
            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
        }
        c6 = (V4.C) this.f10495b;
        AbstractC2467b.l(obj);
        f6 = this;
        P p6 = (P) obj;
        if (p6 != null) {
            AtomicReference atomicReference = f6.f10510u;
            AtomicReference atomicReference2 = f6.f10496c;
            kotlin.jvm.internal.C c8 = f6.f10511v;
            CopyOnWriteArrayList copyOnWriteArrayList = f6.f10512w;
            if (p6.f10579f) {
                while (!atomicReference.compareAndSet(null, p6) && atomicReference.get() == null) {
                }
            } else {
                String str5 = p6.f10574a;
                if (str5 != null && !S4.p.Q0(str5)) {
                    while (true) {
                        if (atomicReference2.compareAndSet(null, p6)) {
                            Object obj2 = c8.f20115a;
                            if (obj2 == null) {
                                kotlin.jvm.internal.o.n("primaryJob");
                                throw null;
                            }
                            ((InterfaceC0765i0) obj2).cancel(new CancellationException("torbox secondary won"));
                            InterfaceC0765i0 interfaceC0765i0 = (InterfaceC0765i0) c6.getCoroutineContext().get(C0763h0.f8306a);
                            Iterator it = copyOnWriteArrayList.iterator();
                            kotlin.jvm.internal.o.g(it, "iterator(...)");
                            while (it.hasNext()) {
                                InterfaceC0765i0 interfaceC0765i02 = (InterfaceC0765i0) it.next();
                                if (interfaceC0765i02 != interfaceC0765i0 && interfaceC0765i02.isActive()) {
                                    interfaceC0765i02.cancel(new CancellationException("torbox overlap winner"));
                                }
                            }
                        } else if (atomicReference2.get() != null) {
                            break;
                        }
                    }
                }
            }
        }
        return C3137A.f25453a;
    }
}
