package c.l;

import android.annotation.SuppressLint;
import android.app.Application;
import android.os.Bundle;
import androidx.lifecycle.SavedStateHandleController;
import c.l.f0;
import c.l.y;
import java.lang.reflect.Constructor;

/* JADX INFO: compiled from: SavedStateViewModelFactory.kt */
/* JADX INFO: loaded from: classes.dex */
@g.i
public final class c0 extends f0.d implements f0.b {
    public Application a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final f0.b f1763b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public j f1765d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public c.o.b f1766e;

    @SuppressLint({"LambdaLast"})
    public c0(Application application, c.o.d dVar, Bundle bundle) {
        f0.a aVar;
        g.h0.c.l.f(dVar, "owner");
        this.f1766e = dVar.getSavedStateRegistry();
        this.f1765d = dVar.getLifecycle();
        this.f1764c = bundle;
        this.a = application;
        if (application != null) {
            f0.a.C0017a c0017a = f0.a.f1772c;
            g.h0.c.l.f(application, "application");
            if (f0.a.f1773d == null) {
                f0.a.f1773d = new f0.a(application);
            }
            aVar = f0.a.f1773d;
            g.h0.c.l.c(aVar);
        } else {
            aVar = new f0.a();
        }
        this.f1763b = aVar;
    }

    @Override // c.l.f0.b
    public <T extends e0> T a(Class<T> cls) {
        g.h0.c.l.f(cls, "modelClass");
        String canonicalName = cls.getCanonicalName();
        if (canonicalName != null) {
            return (T) d(canonicalName, cls);
        }
        throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
    }

    @Override // c.l.f0.b
    public <T extends e0> T b(Class<T> cls, c.l.i0.a aVar) {
        g.h0.c.l.f(cls, "modelClass");
        g.h0.c.l.f(aVar, "extras");
        f0.c.a aVar2 = f0.c.a;
        String str = (String) aVar.a(f0.c.a.C0019a.a);
        if (str == null) {
            throw new IllegalStateException("VIEW_MODEL_KEY must always be provided by ViewModelProvider");
        }
        if (aVar.a(z.a) == null || aVar.a(z.f1805b) == null) {
            if (this.f1765d != null) {
                return (T) d(str, cls);
            }
            throw new IllegalStateException("SAVED_STATE_REGISTRY_OWNER_KEY andVIEW_MODEL_STORE_OWNER_KEY must be provided in the creation extras tosuccessfully create a ViewModel.");
        }
        f0.a.C0017a c0017a = f0.a.f1772c;
        Application application = (Application) aVar.a(f0.a.C0017a.C0018a.a);
        boolean zIsAssignableFrom = b.class.isAssignableFrom(cls);
        Constructor constructorA = (!zIsAssignableFrom || application == null) ? d0.a(cls, d0.f1767b) : d0.a(cls, d0.a);
        return constructorA == null ? (T) this.f1763b.b(cls, aVar) : (!zIsAssignableFrom || application == null) ? (T) d0.b(cls, constructorA, z.a(aVar)) : (T) d0.b(cls, constructorA, application, z.a(aVar));
    }

    @Override // c.l.f0.d
    public void c(e0 e0Var) {
        g.h0.c.l.f(e0Var, "viewModel");
        j jVar = this.f1765d;
        if (jVar != null) {
            c.f.b.h.a(e0Var, this.f1766e, jVar);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final <T extends e0> T d(String str, Class<T> cls) {
        T t;
        Object obj;
        Application application;
        g.h0.c.l.f(str, "key");
        g.h0.c.l.f(cls, "modelClass");
        if (this.f1765d == null) {
            throw new UnsupportedOperationException("SavedStateViewModelFactory constructed with empty constructor supports only calls to create(modelClass: Class<T>, extras: CreationExtras).");
        }
        boolean zIsAssignableFrom = b.class.isAssignableFrom(cls);
        Constructor constructorA = (!zIsAssignableFrom || this.a == null) ? d0.a(cls, d0.f1767b) : d0.a(cls, d0.a);
        if (constructorA == null) {
            if (this.a != null) {
                return (T) this.f1763b.a(cls);
            }
            if (f0.c.f1775b == null) {
                f0.c.f1775b = new f0.c();
            }
            f0.c cVar = f0.c.f1775b;
            g.h0.c.l.c(cVar);
            return (T) cVar.a(cls);
        }
        c.o.b bVar = this.f1766e;
        j jVar = this.f1765d;
        Bundle bundle = this.f1764c;
        Bundle bundleA = bVar.a(str);
        y.a aVar = y.a;
        y yVarA = y.a.a(bundleA, bundle);
        SavedStateHandleController savedStateHandleController = new SavedStateHandleController(str, yVarA);
        savedStateHandleController.h(bVar, jVar);
        c.f.b.h.J(bVar, jVar);
        if (!zIsAssignableFrom || (application = this.a) == null) {
            g.h0.c.l.e(yVarA, "controller.handle");
            t = (T) d0.b(cls, constructorA, yVarA);
        } else {
            g.h0.c.l.c(application);
            g.h0.c.l.e(yVarA, "controller.handle");
            t = (T) d0.b(cls, constructorA, application, yVarA);
        }
        synchronized (t.a) {
            obj = t.a.get("androidx.lifecycle.savedstate.vm.tag");
            if (obj == null) {
                t.a.put("androidx.lifecycle.savedstate.vm.tag", savedStateHandleController);
            }
        }
        if (obj != null) {
            savedStateHandleController = obj;
        }
        if (t.f1769c) {
            e0.a(savedStateHandleController);
        }
        return t;
    }
}
