package androidx.lifecycle;

import android.app.Application;
import android.os.Bundle;
import java.lang.reflect.Constructor;
import java.util.LinkedHashMap;

/* JADX INFO: loaded from: classes.dex */
public final class o0 extends w0 implements u0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Application f2677a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final t0 f2678b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Bundle f2679c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final l f2680d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final i3.f f2681e;

    public o0(Application application, i3.h hVar, Bundle bundle) {
        t0 t0Var;
        nb.g.i(hVar, "owner");
        this.f2681e = hVar.getSavedStateRegistry();
        this.f2680d = hVar.getLifecycle();
        this.f2679c = bundle;
        this.f2677a = application;
        if (application != null) {
            if (t0.f2704c == null) {
                t0.f2704c = new t0(application);
            }
            t0Var = t0.f2704c;
            nb.g.f(t0Var);
        } else {
            t0Var = new t0(null);
        }
        this.f2678b = t0Var;
    }

    @Override // androidx.lifecycle.u0
    public final s0 a(Class cls) {
        String canonicalName = cls.getCanonicalName();
        if (canonicalName != null) {
            return d(cls, canonicalName);
        }
        throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
    }

    @Override // androidx.lifecycle.u0
    public final s0 c(Class cls, n1.c cVar) {
        o1.c cVar2 = o1.c.f31288a;
        LinkedHashMap linkedHashMap = cVar.f30758a;
        String str = (String) linkedHashMap.get(cVar2);
        if (str == null) {
            throw new IllegalStateException("VIEW_MODEL_KEY must always be provided by ViewModelProvider");
        }
        if (linkedHashMap.get(z.f2722a) == null || linkedHashMap.get(z.f2723b) == null) {
            if (this.f2680d != null) {
                return d(cls, str);
            }
            throw new IllegalStateException("SAVED_STATE_REGISTRY_OWNER_KEY andVIEW_MODEL_STORE_OWNER_KEY must be provided in the creation extras tosuccessfully create a ViewModel.");
        }
        Application application = (Application) linkedHashMap.get(t0.f2705d);
        boolean zIsAssignableFrom = AndroidViewModel.class.isAssignableFrom(cls);
        Constructor constructorA = (!zIsAssignableFrom || application == null) ? p0.a(p0.f2683b, cls) : p0.a(p0.f2682a, cls);
        return constructorA == null ? this.f2678b.c(cls, cVar) : (!zIsAssignableFrom || application == null) ? p0.b(cls, constructorA, z.b(cVar)) : p0.b(cls, constructorA, application, z.b(cVar));
    }

    public final s0 d(Class cls, String str) {
        l lVar = this.f2680d;
        if (lVar == null) {
            throw new UnsupportedOperationException("SavedStateViewModelFactory constructed with empty constructor supports only calls to create(modelClass: Class<T>, extras: CreationExtras).");
        }
        boolean zIsAssignableFrom = AndroidViewModel.class.isAssignableFrom(cls);
        Application application = this.f2677a;
        Constructor constructorA = (!zIsAssignableFrom || application == null) ? p0.a(p0.f2683b, cls) : p0.a(p0.f2682a, cls);
        if (constructorA == null) {
            if (application != null) {
                return this.f2678b.a(cls);
            }
            if (v0.f2718a == null) {
                v0.f2718a = new v0();
            }
            v0 v0Var = v0.f2718a;
            nb.g.f(v0Var);
            return v0Var.a(cls);
        }
        i3.f fVar = this.f2681e;
        nb.g.f(fVar);
        Bundle bundleA = fVar.a(str);
        Class[] clsArr = l0.f2662f;
        SavedStateHandleController savedStateHandleController = new SavedStateHandleController(str, k0.a(bundleA, this.f2679c));
        savedStateHandleController.attachToLifecycle(fVar, lVar);
        z.e(fVar, lVar);
        s0 s0VarB = (!zIsAssignableFrom || application == null) ? p0.b(cls, constructorA, savedStateHandleController.getHandle()) : p0.b(cls, constructorA, application, savedStateHandleController.getHandle());
        s0VarB.addCloseable("androidx.lifecycle.savedstate.vm.tag", savedStateHandleController);
        return s0VarB;
    }
}
