package V2;

import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.ktx.FirestoreKt;
import com.google.firebase.ktx.Firebase;
import java.util.ArrayList;
import java.util.List;
import m3.AbstractC2467b;
import w4.C3137A;
import x4.AbstractC3229F;
import x4.AbstractC3251q;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public DocumentReference f7322b;

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

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

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

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

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public F3(String str, String str2, A4.d dVar) {
        super(2, dVar);
        this.f7325e = str;
        this.f7326f = str2;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        F3 f32 = new F3(this.f7325e, this.f7326f, dVar);
        f32.f7324d = obj;
        return f32;
    }

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

    @Override // C4.a
    public final Object invokeSuspend(Object obj) {
        Object objD;
        String string;
        DocumentReference documentReference;
        ArrayList arrayList;
        ArrayList arrayList2;
        List list;
        B4.a aVar = B4.a.f492a;
        int i6 = this.f7323c;
        try {
        } catch (Throwable th) {
            objD = AbstractC2467b.d(th);
        }
        if (i6 == 0) {
            AbstractC2467b.l(obj);
            String str = this.f7325e;
            String str2 = this.f7326f;
            if (S4.p.Q0(str)) {
                throw new IllegalArgumentException("Not signed in");
            }
            string = S4.p.o1(str2).toString();
            L3 l32 = L3.f7428a;
            AbstractC2467b.l(L3.u(string));
            w4.k kVarR = L3.r(L3.m(string));
            List list2 = (List) kVarR.f25469a;
            List list3 = (List) kVarR.f25470b;
            L3.d(list2, list3);
            if (!list3.isEmpty() && L3.i().isEmpty()) {
                throw new IllegalStateException("This add-on must be registered with a debrid service. Enable a debrid provider and add your API key in Cloud Manager first.");
            }
            ArrayList arrayListK = L3.k(list2, list3);
            if (arrayListK.isEmpty()) {
                throw new IllegalArgumentException("The URL does not contain valid DebridStream add-ons");
            }
            DocumentReference documentReferenceDocument = FirestoreKt.getFirestore(Firebase.INSTANCE).collection("addOns").document(str);
            kotlin.jvm.internal.o.g(documentReferenceDocument, "document(...)");
            Task<DocumentSnapshot> task = documentReferenceDocument.get();
            kotlin.jvm.internal.o.g(task, "get(...)");
            this.f7324d = string;
            this.f7321a = arrayListK;
            this.f7322b = documentReferenceDocument;
            this.f7323c = 1;
            Object objH = W0.e.h(task, this);
            if (objH == aVar) {
                return aVar;
            }
            documentReference = documentReferenceDocument;
            obj = objH;
            arrayList = arrayListK;
        } else {
            if (i6 != 1) {
                if (i6 != 2) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                arrayList2 = this.f7321a;
                list = (List) this.f7324d;
                AbstractC2467b.l(obj);
                L3.c(arrayList2, list);
                objD = C3137A.f25453a;
                return new w4.n(objD);
            }
            documentReference = this.f7322b;
            arrayList = this.f7321a;
            string = (String) this.f7324d;
            AbstractC2467b.l(obj);
        }
        DocumentSnapshot documentSnapshot = (DocumentSnapshot) obj;
        L3 l33 = L3.f7428a;
        kotlin.jvm.internal.o.e(documentSnapshot);
        ArrayList arrayListE0 = AbstractC3251q.E0(L3.b(documentSnapshot));
        if (arrayListE0.size() >= 1 && !arrayListE0.contains(string)) {
            throw new IllegalStateException("Only one third-party add-on is allowed. Remove the existing one first.");
        }
        if (!arrayListE0.contains(string)) {
            arrayListE0.add(string);
        }
        Task<Void> task2 = documentReference.set(AbstractC3229F.v(new w4.k("addOns", arrayListE0)));
        kotlin.jvm.internal.o.g(task2, "set(...)");
        this.f7324d = arrayList;
        this.f7321a = arrayListE0;
        this.f7322b = null;
        this.f7323c = 2;
        if (W0.e.h(task2, this) != aVar) {
            arrayList2 = arrayListE0;
            list = arrayList;
            L3.c(arrayList2, list);
            objD = C3137A.f25453a;
            return new w4.n(objD);
        }
        return aVar;
    }
}
