package androidx.savedstate;

import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.lifecycle.Lifecycle;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Iterator;
import ඓ.K.K.K.C1293;
import ῲ.Ԛ.AbstractC1560;
import ῲ.Ԛ.C1562;
import ῲ.ؿ.AbstractC1574;
import ῲ.ؿ.AbstractC1583;
import ῲ.ؿ.C1584;

@SuppressLint({"RestrictedApi"})
public final class Recreator implements AbstractC1583 {

    /* renamed from: K  reason: contains not printable characters */
    public final AbstractC1560 f1601;

    public Recreator(AbstractC1560 r1) {
        this.f1601 = r1;
    }

    @Override // ῲ.ؿ.AbstractC1569
    /* renamed from: K  reason: contains not printable characters */
    public void m944(AbstractC1574 r4, Lifecycle.Event event) {
        Bundle bundle;
        Class cls;
        if (event == Lifecycle.Event.ON_CREATE) {
            ((C1584) r4.getLifecycle()).f5753.remove(this);
            C1562 savedStateRegistry = this.f1601.getSavedStateRegistry();
            if (savedStateRegistry.f5734) {
                Bundle bundle2 = savedStateRegistry.f5735;
                if (bundle2 != null) {
                    bundle = bundle2.getBundle("androidx.savedstate.Restarter");
                    savedStateRegistry.f5735.remove("androidx.savedstate.Restarter");
                    if (savedStateRegistry.f5735.isEmpty()) {
                        savedStateRegistry.f5735 = null;
                    }
                } else {
                    bundle = null;
                }
                if (bundle != null) {
                    ArrayList<String> stringArrayList = bundle.getStringArrayList("classes_to_restore");
                    if (stringArrayList != null) {
                        Iterator<String> it2 = stringArrayList.iterator();
                        while (it2.hasNext()) {
                            String next = it2.next();
                            try {
                                try {
                                    Constructor<? extends U> declaredConstructor = Class.forName(next, false, Recreator.class.getClassLoader()).asSubclass(C1562.AbstractC1564.class).getDeclaredConstructor(new Class[0]);
                                    declaredConstructor.setAccessible(true);
                                    try {
                                        ((C1562.AbstractC1564) declaredConstructor.newInstance(new Object[0])).m4108(this.f1601);
                                    } catch (Exception e) {
                                        throw new RuntimeException(C1293.m3412("Failed to instantiate ", next), e);
                                    }
                                } catch (NoSuchMethodException e2) {
                                    StringBuilder r1 = C1293.m3416("Class");
                                    r1.append(cls.getSimpleName());
                                    r1.append(" must have default constructor in order to be automatically recreated");
                                    throw new IllegalStateException(r1.toString(), e2);
                                }
                            } catch (ClassNotFoundException e3) {
                                throw new RuntimeException(C1293.m3413("Class ", next, " wasn't found"), e3);
                            }
                        }
                        return;
                    }
                    throw new IllegalStateException("Bundle with restored state for the component \"androidx.savedstate.Restarter\" must contain list of strings by the key \"classes_to_restore\"");
                }
                return;
            }
            throw new IllegalStateException("You can consumeRestoredStateForKey only after super.onCreate of corresponding component");
        }
        throw new AssertionError("Next event must be ON_CREATE");
    }
}
