package g0;

import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.lifecycle.g;
import androidx.lifecycle.i;
import androidx.lifecycle.k;
import androidx.savedstate.Recreator;
import java.util.Map;

/* JADX INFO: compiled from: SavedStateRegistry.kt */
/* JADX INFO: loaded from: classes.dex */
@SuppressLint({"RestrictedApi"})
public final class c {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private boolean f4648b;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private boolean f4650d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private Recreator.b f4651e;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final h.b<String, InterfaceC0065c> f4647a = new h.b<>();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private boolean f4652f = true;

    /* JADX INFO: compiled from: SavedStateRegistry.kt */
    public interface a {
        void a(e eVar);
    }

    /* JADX INFO: compiled from: SavedStateRegistry.kt */
    private static final class b {
        private b() {
        }

        public /* synthetic */ b(w1.e eVar) {
            this();
        }
    }

    /* JADX INFO: renamed from: g0.c$c, reason: collision with other inner class name */
    /* JADX INFO: compiled from: SavedStateRegistry.kt */
    public interface InterfaceC0065c {
        Bundle a();
    }

    static {
        new b(null);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void d(c cVar, k kVar, g.b bVar) {
        w1.g.e(cVar, "this$0");
        w1.g.e(kVar, "<anonymous parameter 0>");
        w1.g.e(bVar, "event");
        if (bVar == g.b.ON_START) {
            cVar.f4652f = true;
        } else if (bVar == g.b.ON_STOP) {
            cVar.f4652f = false;
        }
    }

    public final Bundle b(String str) {
        w1.g.e(str, "key");
        if (!this.f4650d) {
            throw new IllegalStateException("You can consumeRestoredStateForKey only after super.onCreate of corresponding component".toString());
        }
        Bundle bundle = this.f4649c;
        if (bundle == null) {
            return null;
        }
        Bundle bundle2 = bundle != null ? bundle.getBundle(str) : null;
        Bundle bundle3 = this.f4649c;
        if (bundle3 != null) {
            bundle3.remove(str);
        }
        Bundle bundle4 = this.f4649c;
        boolean z2 = false;
        if (bundle4 != null && !bundle4.isEmpty()) {
            z2 = true;
        }
        if (!z2) {
            this.f4649c = null;
        }
        return bundle2;
    }

    public final InterfaceC0065c c(String str) {
        w1.g.e(str, "key");
        for (Map.Entry<String, InterfaceC0065c> entry : this.f4647a) {
            w1.g.d(entry, "components");
            String key = entry.getKey();
            InterfaceC0065c value = entry.getValue();
            if (w1.g.a(key, str)) {
                return value;
            }
        }
        return null;
    }

    public final void e(g gVar) {
        w1.g.e(gVar, "lifecycle");
        if (!(!this.f4648b)) {
            throw new IllegalStateException("SavedStateRegistry was already attached.".toString());
        }
        gVar.a(new i() { // from class: g0.b
            @Override // androidx.lifecycle.i
            public final void g(k kVar, g.b bVar) {
                c.d(this.f4646a, kVar, bVar);
            }
        });
        this.f4648b = true;
    }

    public final void f(Bundle bundle) {
        if (!this.f4648b) {
            throw new IllegalStateException("You must call performAttach() before calling performRestore(Bundle).".toString());
        }
        if (!(!this.f4650d)) {
            throw new IllegalStateException("SavedStateRegistry was already restored.".toString());
        }
        this.f4649c = bundle != null ? bundle.getBundle("androidx.lifecycle.BundlableSavedStateRegistry.key") : null;
        this.f4650d = true;
    }

    public final void g(Bundle bundle) {
        w1.g.e(bundle, "outBundle");
        Bundle bundle2 = new Bundle();
        Bundle bundle3 = this.f4649c;
        if (bundle3 != null) {
            bundle2.putAll(bundle3);
        }
        h.b<String, InterfaceC0065c>.d dVarD = this.f4647a.d();
        w1.g.d(dVarD, "this.components.iteratorWithAdditions()");
        while (dVarD.hasNext()) {
            Map.Entry next = dVarD.next();
            bundle2.putBundle((String) next.getKey(), ((InterfaceC0065c) next.getValue()).a());
        }
        if (bundle2.isEmpty()) {
            return;
        }
        bundle.putBundle("androidx.lifecycle.BundlableSavedStateRegistry.key", bundle2);
    }

    public final void h(String str, InterfaceC0065c interfaceC0065c) {
        w1.g.e(str, "key");
        w1.g.e(interfaceC0065c, "provider");
        if (!(this.f4647a.g(str, interfaceC0065c) == null)) {
            throw new IllegalArgumentException("SavedStateProvider with the given key is already registered".toString());
        }
    }

    public final void i(Class<? extends a> cls) {
        w1.g.e(cls, "clazz");
        if (!this.f4652f) {
            throw new IllegalStateException("Can not perform this action after onSaveInstanceState".toString());
        }
        Recreator.b bVar = this.f4651e;
        if (bVar == null) {
            bVar = new Recreator.b(this);
        }
        this.f4651e = bVar;
        try {
            cls.getDeclaredConstructor(new Class[0]);
            Recreator.b bVar2 = this.f4651e;
            if (bVar2 != null) {
                String name = cls.getName();
                w1.g.d(name, "clazz.name");
                bVar2.b(name);
            }
        } catch (NoSuchMethodException e3) {
            throw new IllegalArgumentException("Class " + cls.getSimpleName() + " must have default constructor in order to be automatically recreated", e3);
        }
    }
}
