package androidx.fragment.app;

import android.util.Log;
import d2.C1864a;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
final class FragmentManagerViewModel extends androidx.lifecycle.b0 {
    private static final androidx.lifecycle.d0 FACTORY = new k0();
    private static final String TAG = "FragmentManager";
    private final boolean mStateAutomaticallySaved;
    private final HashMap<String, G> mRetainedFragments = new HashMap<>();
    private final HashMap<String, FragmentManagerViewModel> mChildNonConfigs = new HashMap<>();
    private final HashMap<String, androidx.lifecycle.i0> mViewModelStores = new HashMap<>();
    private boolean mHasBeenCleared = false;
    private boolean mHasSavedSnapshot = false;
    private boolean mIsStateSaved = false;

    public FragmentManagerViewModel(boolean z6) {
        this.mStateAutomaticallySaved = z6;
    }

    private void clearNonConfigStateInternal(String str) {
        FragmentManagerViewModel fragmentManagerViewModel = this.mChildNonConfigs.get(str);
        if (fragmentManagerViewModel != null) {
            fragmentManagerViewModel.onCleared();
            this.mChildNonConfigs.remove(str);
        }
        androidx.lifecycle.i0 i0Var = this.mViewModelStores.get(str);
        if (i0Var != null) {
            i0Var.a();
            this.mViewModelStores.remove(str);
        }
    }

    public static FragmentManagerViewModel getInstance(androidx.lifecycle.i0 store) {
        androidx.lifecycle.d0 factory = FACTORY;
        kotlin.jvm.internal.o.h(store, "store");
        kotlin.jvm.internal.o.h(factory, "factory");
        C1864a defaultCreationExtras = C1864a.f15009b;
        kotlin.jvm.internal.o.h(defaultCreationExtras, "defaultCreationExtras");
        C0.J j4 = new C0.J(store, factory, defaultCreationExtras);
        kotlin.jvm.internal.g gVarA = kotlin.jvm.internal.D.a(FragmentManagerViewModel.class);
        String strB = gVarA.b();
        if (strB != null) {
            return (FragmentManagerViewModel) j4.g(gVarA, "androidx.lifecycle.ViewModelProvider.DefaultKey:".concat(strB));
        }
        throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
    }

    public void addRetainedFragment(G g6) {
        if (this.mIsStateSaved) {
            if (AbstractC1050g0.H(2)) {
                Log.v(TAG, "Ignoring addRetainedFragment as the state is already saved");
            }
        } else {
            if (this.mRetainedFragments.containsKey(g6.mWho)) {
                return;
            }
            this.mRetainedFragments.put(g6.mWho, g6);
            if (AbstractC1050g0.H(2)) {
                Log.v(TAG, "Updating retained Fragments: Added " + g6);
            }
        }
    }

    public void clearNonConfigState(G g6) {
        if (AbstractC1050g0.H(3)) {
            Log.d(TAG, "Clearing non-config state for " + g6);
        }
        clearNonConfigStateInternal(g6.mWho);
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj != null && FragmentManagerViewModel.class == obj.getClass()) {
            FragmentManagerViewModel fragmentManagerViewModel = (FragmentManagerViewModel) obj;
            if (this.mRetainedFragments.equals(fragmentManagerViewModel.mRetainedFragments) && this.mChildNonConfigs.equals(fragmentManagerViewModel.mChildNonConfigs) && this.mViewModelStores.equals(fragmentManagerViewModel.mViewModelStores)) {
                return true;
            }
        }
        return false;
    }

    public G findRetainedFragmentByWho(String str) {
        return this.mRetainedFragments.get(str);
    }

    public FragmentManagerViewModel getChildNonConfig(G g6) {
        FragmentManagerViewModel fragmentManagerViewModel = this.mChildNonConfigs.get(g6.mWho);
        if (fragmentManagerViewModel != null) {
            return fragmentManagerViewModel;
        }
        FragmentManagerViewModel fragmentManagerViewModel2 = new FragmentManagerViewModel(this.mStateAutomaticallySaved);
        this.mChildNonConfigs.put(g6.mWho, fragmentManagerViewModel2);
        return fragmentManagerViewModel2;
    }

    public Collection<G> getRetainedFragments() {
        return new ArrayList(this.mRetainedFragments.values());
    }

    @Deprecated
    public i0 getSnapshot() {
        if (this.mRetainedFragments.isEmpty() && this.mChildNonConfigs.isEmpty() && this.mViewModelStores.isEmpty()) {
            return null;
        }
        HashMap map = new HashMap();
        for (Map.Entry<String, FragmentManagerViewModel> entry : this.mChildNonConfigs.entrySet()) {
            i0 snapshot = entry.getValue().getSnapshot();
            if (snapshot != null) {
                map.put(entry.getKey(), snapshot);
            }
        }
        this.mHasSavedSnapshot = true;
        if (this.mRetainedFragments.isEmpty() && map.isEmpty() && this.mViewModelStores.isEmpty()) {
            return null;
        }
        return new i0(new ArrayList(this.mRetainedFragments.values()), map, new HashMap(this.mViewModelStores));
    }

    public androidx.lifecycle.i0 getViewModelStore(G g6) {
        androidx.lifecycle.i0 i0Var = this.mViewModelStores.get(g6.mWho);
        if (i0Var != null) {
            return i0Var;
        }
        androidx.lifecycle.i0 i0Var2 = new androidx.lifecycle.i0();
        this.mViewModelStores.put(g6.mWho, i0Var2);
        return i0Var2;
    }

    public int hashCode() {
        return this.mViewModelStores.hashCode() + ((this.mChildNonConfigs.hashCode() + (this.mRetainedFragments.hashCode() * 31)) * 31);
    }

    public boolean isCleared() {
        return this.mHasBeenCleared;
    }

    @Override // androidx.lifecycle.b0
    public void onCleared() {
        if (AbstractC1050g0.H(3)) {
            Log.d(TAG, "onCleared called for " + this);
        }
        this.mHasBeenCleared = true;
    }

    public void removeRetainedFragment(G g6) {
        if (this.mIsStateSaved) {
            if (AbstractC1050g0.H(2)) {
                Log.v(TAG, "Ignoring removeRetainedFragment as the state is already saved");
            }
        } else {
            if (this.mRetainedFragments.remove(g6.mWho) == null || !AbstractC1050g0.H(2)) {
                return;
            }
            Log.v(TAG, "Updating retained Fragments: Removed " + g6);
        }
    }

    @Deprecated
    public void restoreFromSnapshot(i0 i0Var) {
        this.mRetainedFragments.clear();
        this.mChildNonConfigs.clear();
        this.mViewModelStores.clear();
        if (i0Var != null) {
            for (G g6 : i0Var.f11635a) {
                if (g6 != null) {
                    this.mRetainedFragments.put(g6.mWho, g6);
                }
            }
            for (Map.Entry entry : i0Var.f11636b.entrySet()) {
                FragmentManagerViewModel fragmentManagerViewModel = new FragmentManagerViewModel(this.mStateAutomaticallySaved);
                fragmentManagerViewModel.restoreFromSnapshot((i0) entry.getValue());
                this.mChildNonConfigs.put((String) entry.getKey(), fragmentManagerViewModel);
            }
            this.mViewModelStores.putAll(i0Var.f11637c);
        }
        this.mHasSavedSnapshot = false;
    }

    public void setIsStateSaved(boolean z6) {
        this.mIsStateSaved = z6;
    }

    public boolean shouldDestroy(G g6) {
        if (this.mRetainedFragments.containsKey(g6.mWho)) {
            return this.mStateAutomaticallySaved ? this.mHasBeenCleared : !this.mHasSavedSnapshot;
        }
        return true;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder("FragmentManagerViewModel{");
        sb.append(Integer.toHexString(System.identityHashCode(this)));
        sb.append("} Fragments (");
        Iterator<G> it = this.mRetainedFragments.values().iterator();
        while (it.hasNext()) {
            sb.append(it.next());
            if (it.hasNext()) {
                sb.append(", ");
            }
        }
        sb.append(") Child Non Config (");
        Iterator<String> it2 = this.mChildNonConfigs.keySet().iterator();
        while (it2.hasNext()) {
            sb.append(it2.next());
            if (it2.hasNext()) {
                sb.append(", ");
            }
        }
        sb.append(") ViewModelStores (");
        Iterator<String> it3 = this.mViewModelStores.keySet().iterator();
        while (it3.hasNext()) {
            sb.append(it3.next());
            if (it3.hasNext()) {
                sb.append(", ");
            }
        }
        sb.append(')');
        return sb.toString();
    }

    public void clearNonConfigState(String str) {
        if (AbstractC1050g0.H(3)) {
            Log.d(TAG, "Clearing non-config state for saved state of Fragment " + str);
        }
        clearNonConfigStateInternal(str);
    }
}
