package com.debridstream.tv.screens;

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 h3.C2099d2;
import h3.C2105e2;
import h3.C2111f2;
import h3.C2117g2;
import h3.C2123h2;
import h3.EnumC2085b2;
import java.util.Iterator;
import java.util.List;
import w4.k;
import x4.AbstractC3251q;
import x4.y;

/* JADX INFO: loaded from: classes.dex */
@StabilityInferred(parameters = 0)
public final class SearchRecommendationsViewModel extends b0 {
    public static final int $stable = 8;
    private static final C2111f2 Companion = new C2111f2();

    @Deprecated
    public static final int MAX_TMDB_PAGES = 8;
    private final H _ui;
    private boolean kidsCatalogFriendlyOnly;
    private Integer pendingPosterRestoreGridIndex;
    private String pendingPosterRestoreKey;
    private final o repository = new o();
    private final a0 uiState;

    public SearchRecommendationsViewModel() {
        y yVar = y.f26065a;
        c0 c0VarC = O.c(new C2105e2(yVar, null, yVar, false, false, null, false, 2));
        this._ui = c0VarC;
        this.uiState = new J(c0VarC);
    }

    public final void consumePosterRestorePending() {
        this.pendingPosterRestoreKey = null;
        this.pendingPosterRestoreGridIndex = null;
    }

    public final void ensureDefaultSelection() {
        List list = ((C2105e2) ((c0) this._ui).getValue()).f17891a;
        if (list.isEmpty()) {
            return;
        }
        C2099d2 c2099d2 = ((C2105e2) ((c0) this._ui).getValue()).f17892b;
        if (c2099d2 != null && !list.isEmpty()) {
            Iterator it = list.iterator();
            while (it.hasNext()) {
                if (kotlin.jvm.internal.o.c(((C2099d2) it.next()).f17848a, c2099d2.f17848a)) {
                    if (!((C2105e2) ((c0) this._ui).getValue()).f17893c.isEmpty() || ((C2105e2) ((c0) this._ui).getValue()).f17894d) {
                        return;
                    }
                    selectCatalog(c2099d2);
                    return;
                }
            }
        }
        selectCatalog((C2099d2) AbstractC3251q.e0(list));
    }

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

    public final boolean hasPendingPosterRestore() {
        return this.pendingPosterRestoreKey != null;
    }

    public final void loadMore() {
        c0 c0Var;
        Object value;
        C2099d2 c2099d2 = ((C2105e2) ((c0) this._ui).getValue()).f17892b;
        if (c2099d2 == null || !((C2105e2) ((c0) this._ui).getValue()).f17897g || ((C2105e2) ((c0) this._ui).getValue()).f17895e || ((C2105e2) ((c0) this._ui).getValue()).f17894d) {
            return;
        }
        int i6 = ((C2105e2) ((c0) this._ui).getValue()).f17898h;
        if (c2099d2.f17851d == EnumC2085b2.f17747a || i6 <= 8) {
            E.y(X.h(this), null, null, new C2117g2(this, c2099d2, i6, null), 3);
            return;
        }
        H h5 = this._ui;
        do {
            c0Var = (c0) h5;
            value = c0Var.getValue();
        } while (!c0Var.i(value, C2105e2.a((C2105e2) value, null, null, null, false, false, null, false, 0, 191)));
    }

    public final k peekPendingPosterRestore() {
        Integer num;
        String str = this.pendingPosterRestoreKey;
        if (str == null || (num = this.pendingPosterRestoreGridIndex) == null) {
            return null;
        }
        return new k(str, num);
    }

    public final void preparePosterRestoreForNavigation(HomeMetaItem item, int i6) {
        kotlin.jvm.internal.o.h(item, "item");
        this.pendingPosterRestoreKey = item.getType() + "-" + item.getTmdbId();
        this.pendingPosterRestoreGridIndex = Integer.valueOf(i6);
    }

    public final void selectCatalog(C2099d2 entry) {
        kotlin.jvm.internal.o.h(entry, "entry");
        E.y(X.h(this), null, null, new C2123h2(this, entry, null), 3);
    }

    public final void setCatalogList(List<C2099d2> catalogs) {
        kotlin.jvm.internal.o.h(catalogs, "catalogs");
        H h5 = this._ui;
        while (true) {
            c0 c0Var = (c0) h5;
            Object value = c0Var.getValue();
            List<C2099d2> list = catalogs;
            if (c0Var.i(value, C2105e2.a((C2105e2) value, list, null, null, false, false, null, false, 0, 254))) {
                return;
            } else {
                catalogs = list;
            }
        }
    }

    public final void setKidsCatalogFriendlyOnly(boolean z6) {
        c0 c0Var;
        Object value;
        if (this.kidsCatalogFriendlyOnly == z6) {
            return;
        }
        this.kidsCatalogFriendlyOnly = z6;
        H h5 = this._ui;
        do {
            c0Var = (c0) h5;
            value = c0Var.getValue();
        } while (!c0Var.i(value, C2105e2.a((C2105e2) value, null, null, y.f26065a, false, false, null, false, 2, 3)));
    }
}
