package com.debridstream.tv;

import V2.AbstractC0742y1;
import V2.S0;
import V2.W0;
import V4.E;
import Y2.o;
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 com.debridstream.tv.data.home.HomeMetaItem;
import e5.a;
import e5.d;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import x4.AbstractC3251q;
import x4.AbstractC3252r;
import x4.AbstractC3253s;
import x4.y;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class CollectionHubViewModel extends b0 {
    public static final int $stable = 8;
    private final H _uiState;
    private final String categoryJsonUrl;
    private List<? extends AbstractC0742y1> configs;
    private final Set<String> loaded;
    private final Set<String> loading;
    private final o repository;
    private final a sectionLoadMutex;
    private final a0 uiState;

    public CollectionHubViewModel(String categoryJsonUrl) {
        kotlin.jvm.internal.o.h(categoryJsonUrl, "categoryJsonUrl");
        this.categoryJsonUrl = categoryJsonUrl;
        this.repository = new o();
        this.configs = y.f26065a;
        this.loaded = new LinkedHashSet();
        this.loading = new LinkedHashSet();
        this.sectionLoadMutex = d.a();
        c0 c0VarC = O.c(new HomeUiState(null, true, null, 5, null));
        this._uiState = c0VarC;
        this.uiState = new J(c0VarC);
        E.y(X.h(this), null, null, new S0(this, null), 3);
    }

    public static /* synthetic */ void requestSectionsAround$default(CollectionHubViewModel collectionHubViewModel, int i6, int i7, int i8, Object obj) {
        if ((i8 & 2) != 0) {
            i7 = 2;
        }
        collectionHubViewModel.requestSectionsAround(i6, i7);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final HomeUiState shellState(List<? extends AbstractC0742y1> list) {
        ArrayList arrayList = new ArrayList(AbstractC3253s.G(list, 10));
        for (AbstractC0742y1 abstractC0742y1 : list) {
            arrayList.add(new HomeSection(abstractC0742y1.a(), abstractC0742y1.c(), y.f26065a));
        }
        return new HomeUiState(arrayList, false, null);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void updateSection(String str, String str2, List<HomeMetaItem> list) {
        c0 c0Var;
        Object value;
        HomeUiState homeUiState;
        ArrayList arrayListE0;
        H h5 = this._uiState;
        do {
            c0Var = (c0) h5;
            value = c0Var.getValue();
            homeUiState = (HomeUiState) value;
            arrayListE0 = AbstractC3251q.E0(homeUiState.getSections());
            Iterator it = arrayListE0.iterator();
            int i6 = 0;
            while (true) {
                if (!it.hasNext()) {
                    i6 = -1;
                    break;
                } else if (kotlin.jvm.internal.o.c(((HomeSection) it.next()).getId(), str)) {
                    break;
                } else {
                    i6++;
                }
            }
            HomeSection homeSection = new HomeSection(str, str2, W.a.a0(list));
            if (i6 >= 0) {
                arrayListE0.set(i6, homeSection);
            } else {
                arrayListE0.add(homeSection);
            }
        } while (!c0Var.i(value, HomeUiState.copy$default(homeUiState, arrayListE0, false, null, 6, null)));
    }

    public final AbstractC0742y1 findCatalogEntry(String sectionId) {
        Object next;
        kotlin.jvm.internal.o.h(sectionId, "sectionId");
        Iterator<T> it = this.configs.iterator();
        while (true) {
            if (!it.hasNext()) {
                next = null;
                break;
            }
            next = it.next();
            if (kotlin.jvm.internal.o.c(((AbstractC0742y1) next).a(), sectionId)) {
                break;
            }
        }
        return (AbstractC0742y1) next;
    }

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

    public final void requestSectionsAround(int i6, int i7) {
        if (this.configs.isEmpty()) {
            return;
        }
        int i8 = i6 - i7;
        if (i8 < 0) {
            i8 = 0;
        }
        int i9 = i6 + i7;
        int iA = AbstractC3252r.A(this.configs);
        if (i9 > iA) {
            i9 = iA;
        }
        if (i8 > i9) {
            return;
        }
        E.y(X.h(this), null, null, new W0(this, i9, i8, null), 3);
    }
}
