package androidx.savedstate;

import android.os.Bundle;
import androidx.lifecycle.Lifecycle$Event;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.p;
import androidx.lifecycle.s0;
import androidx.lifecycle.y0;
import androidx.lifecycle.z;
import androidx.lifecycle.z0;
import com.google.android.gms.measurement.internal.u1;
import defpackage.e;
import i3.b;
import i3.d;
import i3.f;
import i3.h;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import kotlin.Metadata;
import nb.g;
import org.jetbrains.annotations.NotNull;

/* JADX INFO: loaded from: classes.dex */
@Metadata(d1 = {"\u0000*\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\b\b\u0000\u0018\u0000 \u00102\u00020\u0001:\u0002\u0011\u0012B\u000f\u0012\u0006\u0010\f\u001a\u00020\u000b¢\u0006\u0004\b\u000e\u0010\u000fJ\u0010\u0010\u0005\u001a\u00020\u00042\u0006\u0010\u0003\u001a\u00020\u0002H\u0002J\u0018\u0010\n\u001a\u00020\u00042\u0006\u0010\u0007\u001a\u00020\u00062\u0006\u0010\t\u001a\u00020\bH\u0016R\u0014\u0010\f\u001a\u00020\u000b8\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\f\u0010\r¨\u0006\u0013"}, d2 = {"Landroidx/savedstate/Recreator;", "Landroidx/lifecycle/LifecycleEventObserver;", "", "className", "Lkd/e;", "reflectiveNew", "Landroidx/lifecycle/p;", "source", "Landroidx/lifecycle/Lifecycle$Event;", "event", "onStateChanged", "Li3/h;", "owner", "Li3/h;", "<init>", "(Li3/h;)V", "Companion", "i3/b", "e/n", "savedstate_release"}, k = 1, mv = {1, 8, 0})
public final class Recreator implements LifecycleEventObserver {

    @NotNull
    public static final String CLASSES_KEY = "classes_to_restore";

    @NotNull
    public static final String COMPONENT_KEY = "androidx.savedstate.Restarter";

    @NotNull
    public static final b Companion = new b();

    @NotNull
    private final h owner;

    public Recreator(@NotNull h hVar) {
        g.i(hVar, "owner");
        this.owner = hVar;
    }

    private final void reflectiveNew(String str) {
        try {
            Class<? extends U> clsAsSubclass = Class.forName(str, false, Recreator.class.getClassLoader()).asSubclass(d.class);
            g.h(clsAsSubclass, "{\n                Class.…class.java)\n            }");
            try {
                Constructor declaredConstructor = clsAsSubclass.getDeclaredConstructor(new Class[0]);
                declaredConstructor.setAccessible(true);
                try {
                    Object objNewInstance = declaredConstructor.newInstance(new Object[0]);
                    g.h(objNewInstance, "{\n                constr…wInstance()\n            }");
                    h hVar = this.owner;
                    g.i(hVar, "owner");
                    if (!(hVar instanceof z0)) {
                        throw new IllegalStateException("Internal error: OnRecreation should be registered only on components that implement ViewModelStoreOwner".toString());
                    }
                    y0 viewModelStore = ((z0) hVar).getViewModelStore();
                    f savedStateRegistry = hVar.getSavedStateRegistry();
                    viewModelStore.getClass();
                    LinkedHashMap linkedHashMap = viewModelStore.f2721a;
                    for (String str2 : new HashSet(linkedHashMap.keySet())) {
                        g.i(str2, "key");
                        s0 s0Var = (s0) linkedHashMap.get(str2);
                        g.f(s0Var);
                        z.a(s0Var, savedStateRegistry, hVar.getLifecycle());
                    }
                    if (!new HashSet(linkedHashMap.keySet()).isEmpty()) {
                        savedStateRegistry.d();
                    }
                } catch (Exception e10) {
                    throw new RuntimeException(e.i("Failed to instantiate ", str), e10);
                }
            } catch (NoSuchMethodException e11) {
                throw new IllegalStateException("Class " + clsAsSubclass.getSimpleName() + " must have default constructor in order to be automatically recreated", e11);
            }
        } catch (ClassNotFoundException e12) {
            throw new RuntimeException(u1.k("Class ", str, " wasn't found"), e12);
        }
    }

    @Override // androidx.lifecycle.LifecycleEventObserver
    public void onStateChanged(@NotNull p pVar, @NotNull Lifecycle$Event lifecycle$Event) {
        g.i(pVar, "source");
        g.i(lifecycle$Event, "event");
        if (lifecycle$Event != Lifecycle$Event.ON_CREATE) {
            throw new AssertionError("Next event must be ON_CREATE");
        }
        pVar.getLifecycle().b(this);
        Bundle bundleA = this.owner.getSavedStateRegistry().a(COMPONENT_KEY);
        if (bundleA == null) {
            return;
        }
        ArrayList<String> stringArrayList = bundleA.getStringArrayList(CLASSES_KEY);
        if (stringArrayList == null) {
            throw new IllegalStateException("Bundle with restored state for the component \"androidx.savedstate.Restarter\" must contain list of strings by the key \"classes_to_restore\"");
        }
        Iterator<String> it = stringArrayList.iterator();
        while (it.hasNext()) {
            reflectiveNew(it.next());
        }
    }
}
