package i5;

import android.os.Bundle;
import androidx.lifecycle.n;
import androidx.lifecycle.r;
import androidx.lifecycle.u;
import i5.b;
import java.util.Iterator;
import java.util.Map;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.t;
import q.b;

/* loaded from: classes.dex */
public final class d {

    /* renamed from: g, reason: collision with root package name */
    private static final b f22426g = new b(null);

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

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

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

    /* renamed from: e, reason: collision with root package name */
    private b.C0479b f22431e;

    /* renamed from: a, reason: collision with root package name */
    private final q.b f22427a = new q.b();

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

    /* loaded from: classes.dex */
    public interface a {
        void a(f fVar);
    }

    /* loaded from: classes.dex */
    private static final class b {
        private b() {
        }

        public /* synthetic */ b(k kVar) {
            this();
        }
    }

    /* loaded from: classes.dex */
    public interface c {
        Bundle a();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void d(d this$0, u uVar, n.a event) {
        t.g(this$0, "this$0");
        t.g(uVar, "<anonymous parameter 0>");
        t.g(event, "event");
        if (event == n.a.ON_START) {
            this$0.f22432f = true;
        } else if (event == n.a.ON_STOP) {
            this$0.f22432f = false;
        }
    }

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

    public final c c(String key) {
        t.g(key, "key");
        Iterator it = this.f22427a.iterator();
        while (it.hasNext()) {
            Map.Entry components = (Map.Entry) it.next();
            t.f(components, "components");
            String str = (String) components.getKey();
            c cVar = (c) components.getValue();
            if (t.b(str, key)) {
                return cVar;
            }
        }
        return null;
    }

    public final void e(n lifecycle) {
        t.g(lifecycle, "lifecycle");
        if (!(!this.f22428b)) {
            throw new IllegalStateException("SavedStateRegistry was already attached.".toString());
        }
        lifecycle.a(new r() { // from class: i5.c
            @Override // androidx.lifecycle.r
            public final void f(u uVar, n.a aVar) {
                d.d(d.this, uVar, aVar);
            }
        });
        this.f22428b = true;
    }

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

    public final void g(Bundle outBundle) {
        t.g(outBundle, "outBundle");
        Bundle bundle = new Bundle();
        Bundle bundle2 = this.f22429c;
        if (bundle2 != null) {
            bundle.putAll(bundle2);
        }
        b.d i10 = this.f22427a.i();
        t.f(i10, "this.components.iteratorWithAdditions()");
        while (i10.hasNext()) {
            Map.Entry entry = (Map.Entry) i10.next();
            bundle.putBundle((String) entry.getKey(), ((c) entry.getValue()).a());
        }
        if (bundle.isEmpty()) {
            return;
        }
        outBundle.putBundle("androidx.lifecycle.BundlableSavedStateRegistry.key", bundle);
    }

    public final void h(String key, c provider) {
        t.g(key, "key");
        t.g(provider, "provider");
        if (!(((c) this.f22427a.m(key, provider)) == null)) {
            throw new IllegalArgumentException("SavedStateProvider with the given key is already registered".toString());
        }
    }

    public final void i(Class clazz) {
        t.g(clazz, "clazz");
        if (!this.f22432f) {
            throw new IllegalStateException("Can not perform this action after onSaveInstanceState".toString());
        }
        b.C0479b c0479b = this.f22431e;
        if (c0479b == null) {
            c0479b = new b.C0479b(this);
        }
        this.f22431e = c0479b;
        try {
            clazz.getDeclaredConstructor(new Class[0]);
            b.C0479b c0479b2 = this.f22431e;
            if (c0479b2 != null) {
                String name = clazz.getName();
                t.f(name, "clazz.name");
                c0479b2.b(name);
            }
        } catch (NoSuchMethodException e10) {
            throw new IllegalArgumentException("Class " + clazz.getSimpleName() + " must have default constructor in order to be automatically recreated", e10);
        }
    }

    public final void j(String key) {
        t.g(key, "key");
        this.f22427a.n(key);
    }
}
