package com.debridstream.tv.screens;

import A0.a;
import A4.d;
import I0.C0307p;
import J4.c;
import N2.g;
import N2.q;
import N2.r;
import S4.p;
import V2.C0656e3;
import V4.E;
import W0.e;
import Y4.H;
import Y4.J;
import Y4.O;
import Y4.a0;
import Y4.c0;
import androidx.compose.runtime.internal.StabilityInferred;
import androidx.lifecycle.X;
import androidx.lifecycle.b0;
import androidx.media3.exoplayer.analytics.AnalyticsListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.SetOptions;
import com.google.firebase.firestore.ktx.FirestoreKt;
import com.google.firebase.ktx.Firebase;
import e3.AbstractC1964a;
import h3.C2076a0;
import h3.C2083b0;
import h3.T;
import h3.U;
import h3.W;
import h3.Y;
import h3.Z;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import kotlin.jvm.internal.o;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import w4.C3137A;
import w4.k;
import x4.AbstractC3229F;
import x4.AbstractC3251q;
import x4.y;
import x4.z;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class CloudManagerViewModel extends b0 {
    public static final int $stable = 8;
    private final H _uiState;
    private final r httpClient;
    private final OkHttpClient okHttpClient;
    private final a0 uiState;

    public CloudManagerViewModel() {
        String str = ((C2083b0) AbstractC3251q.e0(T.f17339i)).f17739a;
        z zVar = z.f26066a;
        c0 c0VarC = O.c(new U(str, false, false, "", null, false, false, null, zVar, zVar, zVar, zVar, zVar));
        this._uiState = c0VarC;
        this.uiState = new J(c0VarC);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        TimeUnit timeUnit = TimeUnit.SECONDS;
        builder.c(15L);
        builder.d(20L);
        builder.e(15L);
        TimeUnit timeUnit2 = TimeUnit.MINUTES;
        builder.f22095b = new ConnectionPool(16, 5L);
        AbstractC1964a.b(builder);
        this.okHttpClient = new OkHttpClient(builder);
        this.httpClient = new C0656e3(this, 2);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final List<String> extractLinks(Map<String, ? extends Object> map) {
        Object obj = map != null ? map.get("links") : null;
        List list = obj instanceof List ? (List) obj : null;
        if (list != null) {
            ArrayList arrayList = new ArrayList();
            Iterator it = list.iterator();
            while (it.hasNext()) {
                Object next = it.next();
                String string = next != null ? next.toString() : null;
                if (string != null) {
                    arrayList.add(string);
                }
            }
            if (!arrayList.isEmpty()) {
                return arrayList;
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final Object persistProviderKey(String str, String str2, String str3, d dVar) throws Exception {
        C2083b0 c2083b0;
        ListIterator listIterator = T.f17339i.listIterator(0);
        do {
            C0307p c0307p = (C0307p) listIterator;
            if (!c0307p.hasNext()) {
                throw new NoSuchElementException("Collection contains no element matching the predicate.");
            }
            c2083b0 = (C2083b0) c0307p.next();
        } while (!c2083b0.f17739a.equals(str2));
        Task<Void> task = FirestoreKt.getFirestore(Firebase.INSTANCE).collection("debridKeys").document(str).set(a.r(c2083b0.f17741c, str3), SetOptions.merge());
        o.g(task, "set(...)");
        Object objH = e.h(task, dVar);
        return objH == B4.a.f492a ? objH : C3137A.f25453a;
    }

    private final void refreshProvider(boolean z6) {
        U u6 = (U) ((c0) this._uiState).getValue();
        String str = u6.f17437a;
        String str2 = (String) u6.f17445i.get(str);
        if (str2 == null) {
            str2 = "";
        }
        if (u6.f17439c && !p.Q0(str2)) {
            E.y(X.h(this), null, null, new Y(this, str, str2, z6, null), 3);
            return;
        }
        H h5 = this._uiState;
        y yVar = y.f26065a;
        U uA = U.a(u6, null, false, false, null, null, false, false, null, null, null, AbstractC3229F.A(new k(str, yVar), u6.f17447k), AbstractC3229F.A(new k(str, yVar), u6.l), AbstractC3229F.A(new k(str, z.f26066a), u6.f17448m), AnalyticsListener.EVENT_VIDEO_FRAME_PROCESSING_OFFSET);
        c0 c0Var = (c0) h5;
        c0Var.getClass();
        c0Var.k(null, uA);
    }

    public static /* synthetic */ void refreshProvider$default(CloudManagerViewModel cloudManagerViewModel, boolean z6, int i6, Object obj) {
        if ((i6 & 1) != 0) {
            z6 = false;
        }
        cloudManagerViewModel.refreshProvider(z6);
    }

    public final a0 getUiState() {
        return this.uiState;
    }

    public final void goBackFolder() {
        String str = ((U) ((c0) this._uiState).getValue()).f17437a;
        List list = (List) ((U) ((c0) this._uiState).getValue()).l.get(str);
        if (list == null) {
            list = y.f26065a;
        }
        if (list.isEmpty()) {
            return;
        }
        c0 c0Var = (c0) this._uiState;
        U uA = U.a((U) c0Var.getValue(), null, false, false, null, null, false, false, null, null, null, null, AbstractC3229F.A(new k(str, AbstractC3251q.b0(list)), ((U) ((c0) this._uiState).getValue()).l), null, 6143);
        c0Var.getClass();
        c0Var.k(null, uA);
    }

    public final void initialize(String userId, boolean z6) {
        boolean z7;
        boolean z8;
        boolean z9;
        o.h(userId, "userId");
        U u6 = (U) ((c0) this._uiState).getValue();
        Map map = q.f5245b;
        if (u6.f17440d.equals(userId)) {
            z7 = z6;
            if (u6.f17439c == z7) {
                z8 = false;
            }
            H h5 = this._uiState;
            z9 = z8;
            U uA = U.a(u6, null, false, z7, userId, null, false, false, null, map, null, null, null, null, 7923);
            c0 c0Var = (c0) h5;
            c0Var.getClass();
            c0Var.k(null, uA);
            boolean zContainsKey = ((U) ((c0) this._uiState).getValue()).f17447k.containsKey(((U) ((c0) this._uiState).getValue()).f17437a);
            if (z9 && zContainsKey) {
                return;
            }
            refreshProvider(z9);
        }
        z7 = z6;
        z8 = true;
        H h52 = this._uiState;
        z9 = z8;
        U uA2 = U.a(u6, null, false, z7, userId, null, false, false, null, map, null, null, null, null, 7923);
        c0 c0Var2 = (c0) h52;
        c0Var2.getClass();
        c0Var2.k(null, uA2);
        boolean zContainsKey2 = ((U) ((c0) this._uiState).getValue()).f17447k.containsKey(((U) ((c0) this._uiState).getValue()).f17437a);
        if (z9) {
        }
        refreshProvider(z9);
    }

    public final void openFolder(g folder) {
        o.h(folder, "folder");
        U u6 = (U) ((c0) this._uiState).getValue();
        String str = u6.f17437a;
        String str2 = folder.f5170c;
        if (p.Q0(str2)) {
            return;
        }
        Map map = u6.f17448m;
        Map map2 = (Map) map.get(str);
        if (map2 == null) {
            map2 = z.f26066a;
        }
        boolean zContainsKey = map2.containsKey(str2);
        List list = y.f26065a;
        Map map3 = u6.l;
        if (zContainsKey) {
            List list2 = (List) map3.get(str);
            if (list2 != null) {
                list = list2;
            }
            ArrayList arrayListT0 = AbstractC3251q.t0(list, folder);
            H h5 = this._uiState;
            U uA = U.a(u6, null, false, false, null, null, false, false, null, null, null, null, AbstractC3229F.A(new k(str, arrayListT0), map3), null, 6143);
            c0 c0Var = (c0) h5;
            c0Var.getClass();
            c0Var.k(null, uA);
            return;
        }
        List list3 = folder.f5175h;
        if (list3 == null || list3.isEmpty()) {
            list3 = null;
        }
        if (list3 == null) {
            String str3 = (String) u6.f17445i.get(str);
            if (str3 == null) {
                str3 = "";
            }
            String str4 = str3;
            if (p.Q0(str4)) {
                return;
            }
            E.y(X.h(this), null, null, new W(this, str, str4, folder, str2, null), 3);
            return;
        }
        List list4 = (List) map3.get(str);
        if (list4 != null) {
            list = list4;
        }
        ArrayList arrayListT02 = AbstractC3251q.t0(list, folder);
        Map mapA = AbstractC3229F.A(new k(str2, list3), map2);
        H h6 = this._uiState;
        U uA2 = U.a(u6, null, false, false, null, null, false, false, null, null, null, null, AbstractC3229F.A(new k(str, arrayListT02), map3), AbstractC3229F.A(new k(str, mapA), map), 2047);
        c0 c0Var2 = (c0) h6;
        c0Var2.getClass();
        c0Var2.k(null, uA2);
    }

    public final void playFile(g file, c onResolved) {
        o.h(file, "file");
        o.h(onResolved, "onResolved");
        U u6 = (U) ((c0) this._uiState).getValue();
        String str = u6.f17437a;
        String str2 = (String) u6.f17445i.get(str);
        if (str2 == null) {
            str2 = "";
        }
        if (!p.Q0(str2)) {
            E.y(X.h(this), null, null, new h3.X(this, file, str, str2, onResolved, null), 3);
            return;
        }
        H h5 = this._uiState;
        U uA = U.a(u6, null, false, false, null, "Add API key before playback.", false, false, null, null, null, null, null, null, 8175);
        c0 c0Var = (c0) h5;
        c0Var.getClass();
        c0Var.k(null, uA);
    }

    public final void refreshCloudFiles() {
        refreshProvider$default(this, false, 1, null);
    }

    public final void removeApiKey() {
        U u6 = (U) ((c0) this._uiState).getValue();
        String str = u6.f17437a;
        String str2 = u6.f17440d;
        if (p.Q0(str2)) {
            return;
        }
        E.y(X.h(this), null, null, new Z(this, str2, str, null), 3);
    }

    public final void saveApiKey(String rawApiKey) {
        o.h(rawApiKey, "rawApiKey");
        String string = p.o1(rawApiKey).toString();
        if (p.Q0(string)) {
            c0 c0Var = (c0) this._uiState;
            U uA = U.a((U) c0Var.getValue(), null, false, false, null, "Please enter a valid API key.", false, false, null, null, null, null, null, null, 8175);
            c0Var.getClass();
            c0Var.k(null, uA);
            return;
        }
        U u6 = (U) ((c0) this._uiState).getValue();
        String str = u6.f17437a;
        String str2 = u6.f17440d;
        if (!p.Q0(str2)) {
            E.y(X.h(this), null, null, new C2076a0(this, str, string, str2, null), 3);
            return;
        }
        c0 c0Var2 = (c0) this._uiState;
        U uA2 = U.a((U) c0Var2.getValue(), null, false, false, null, "No signed-in user.", false, false, null, null, null, null, null, null, 8175);
        c0Var2.getClass();
        c0Var2.k(null, uA2);
    }

    public final void selectProvider(String providerId) {
        o.h(providerId, "providerId");
        c0 c0Var = (c0) this._uiState;
        U uA = U.a((U) c0Var.getValue(), providerId, false, false, null, null, false, false, null, null, null, null, null, null, 8174);
        c0Var.getClass();
        c0Var.k(null, uA);
        refreshProvider$default(this, false, 1, null);
    }
}
