package androidx.datastore.preferences.core;

import androidx.datastore.core.CorruptionException;
import androidx.datastore.core.g0;
import androidx.datastore.core.z;
import androidx.datastore.preferences.PreferencesProto$Value$ValueCase;
import androidx.datastore.preferences.i;
import androidx.datastore.preferences.protobuf.ByteString;
import androidx.datastore.preferences.protobuf.InvalidProtocolBufferException;
import androidx.datastore.preferences.protobuf.a0;
import androidx.datastore.preferences.protobuf.b0;
import androidx.datastore.preferences.protobuf.m;
import androidx.datastore.preferences.protobuf.y;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import kotlin.NoWhenBranchMatchedException;
import kotlin.collections.n;
import kotlin.collections.q;

/* JADX INFO: loaded from: classes.dex */
public final class h implements z {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final h f1993a = new h();

    @Override // androidx.datastore.core.z
    public final Object a() {
        return new a(true);
    }

    @Override // androidx.datastore.core.z
    public final Object b(FileInputStream fileInputStream) throws CorruptionException {
        byte[] bArr;
        try {
            androidx.datastore.preferences.e eVarQ = androidx.datastore.preferences.e.q(fileInputStream);
            a aVar = new a(false);
            e[] eVarArr = (e[]) Arrays.copyOf(new e[0], 0);
            nb.g.i(eVarArr, "pairs");
            aVar.c();
            if (eVarArr.length > 0) {
                e eVar = eVarArr[0];
                throw null;
            }
            Map mapO = eVarQ.o();
            nb.g.h(mapO, "preferencesProto.preferencesMap");
            for (Map.Entry entry : mapO.entrySet()) {
                String str = (String) entry.getKey();
                i iVar = (i) entry.getValue();
                nb.g.h(str, "name");
                nb.g.h(iVar, "value");
                PreferencesProto$Value$ValueCase preferencesProto$Value$ValueCaseE = iVar.E();
                switch (preferencesProto$Value$ValueCaseE == null ? -1 : g.f1992a[preferencesProto$Value$ValueCaseE.ordinal()]) {
                    case -1:
                        throw new CorruptionException("Value case is null.", null);
                    case 0:
                    default:
                        throw new NoWhenBranchMatchedException();
                    case 1:
                        aVar.d(g7.z.a(str), Boolean.valueOf(iVar.v()));
                        break;
                    case 2:
                        aVar.d(new d(str), Float.valueOf(iVar.z()));
                        break;
                    case 3:
                        aVar.d(g7.z.b(str), Double.valueOf(iVar.y()));
                        break;
                    case 4:
                        aVar.d(new d(str), Integer.valueOf(iVar.A()));
                        break;
                    case 5:
                        aVar.d(g7.z.d(str), Long.valueOf(iVar.B()));
                        break;
                    case 6:
                        d dVarE = g7.z.e(str);
                        String strC = iVar.C();
                        nb.g.h(strC, "value.string");
                        aVar.d(dVarE, strC);
                        break;
                    case 7:
                        d dVar = new d(str);
                        a0 a0VarP = iVar.D().p();
                        nb.g.h(a0VarP, "value.stringSet.stringsList");
                        aVar.d(dVar, n.t1(a0VarP));
                        break;
                    case 8:
                        d dVar2 = new d(str);
                        ByteString byteStringW = iVar.w();
                        int size = byteStringW.size();
                        if (size == 0) {
                            bArr = b0.f2048b;
                        } else {
                            byte[] bArr2 = new byte[size];
                            byteStringW.h(size, bArr2);
                            bArr = bArr2;
                        }
                        nb.g.h(bArr, "value.bytes.toByteArray()");
                        aVar.d(dVar2, bArr);
                        break;
                    case 9:
                        throw new CorruptionException("Value not set.", null);
                }
            }
            return new a(true, q.g0(aVar.a()));
        } catch (InvalidProtocolBufferException e10) {
            throw new CorruptionException("Unable to parse preferences proto.", e10);
        }
    }

    @Override // androidx.datastore.core.z
    public final void c(Object obj, g0 g0Var) {
        y yVarA;
        Map mapA = ((f) obj).a();
        androidx.datastore.preferences.c cVarP = androidx.datastore.preferences.e.p();
        for (Map.Entry entry : mapA.entrySet()) {
            d dVar = (d) entry.getKey();
            Object value = entry.getValue();
            String str = dVar.f1991a;
            if (value instanceof Boolean) {
                androidx.datastore.preferences.h hVarF = i.F();
                boolean zBooleanValue = ((Boolean) value).booleanValue();
                hVarF.c();
                i.s((i) hVarF.f2164b, zBooleanValue);
                yVarA = hVarF.a();
            } else if (value instanceof Float) {
                androidx.datastore.preferences.h hVarF2 = i.F();
                float fFloatValue = ((Number) value).floatValue();
                hVarF2.c();
                i.t((i) hVarF2.f2164b, fFloatValue);
                yVarA = hVarF2.a();
            } else if (value instanceof Double) {
                androidx.datastore.preferences.h hVarF3 = i.F();
                double dDoubleValue = ((Number) value).doubleValue();
                hVarF3.c();
                i.q((i) hVarF3.f2164b, dDoubleValue);
                yVarA = hVarF3.a();
            } else if (value instanceof Integer) {
                androidx.datastore.preferences.h hVarF4 = i.F();
                int iIntValue = ((Number) value).intValue();
                hVarF4.c();
                i.u((i) hVarF4.f2164b, iIntValue);
                yVarA = hVarF4.a();
            } else if (value instanceof Long) {
                androidx.datastore.preferences.h hVarF5 = i.F();
                long jLongValue = ((Number) value).longValue();
                hVarF5.c();
                i.n((i) hVarF5.f2164b, jLongValue);
                yVarA = hVarF5.a();
            } else if (value instanceof String) {
                androidx.datastore.preferences.h hVarF6 = i.F();
                hVarF6.c();
                i.o((i) hVarF6.f2164b, (String) value);
                yVarA = hVarF6.a();
            } else if (value instanceof Set) {
                androidx.datastore.preferences.h hVarF7 = i.F();
                androidx.datastore.preferences.f fVarQ = androidx.datastore.preferences.g.q();
                nb.g.g(value, "null cannot be cast to non-null type kotlin.collections.Set<kotlin.String>");
                fVarQ.c();
                androidx.datastore.preferences.g.n((androidx.datastore.preferences.g) fVarQ.f2164b, (Set) value);
                hVarF7.c();
                i.p((i) hVarF7.f2164b, (androidx.datastore.preferences.g) fVarQ.a());
                yVarA = hVarF7.a();
            } else {
                if (!(value instanceof byte[])) {
                    throw new IllegalStateException("PreferencesSerializer does not support type: ".concat(value.getClass().getName()));
                }
                androidx.datastore.preferences.h hVarF8 = i.F();
                byte[] bArr = (byte[]) value;
                ByteString byteString = ByteString.f1995a;
                ByteString byteStringG = ByteString.g(0, bArr.length, bArr);
                hVarF8.c();
                i.r((i) hVarF8.f2164b, byteStringG);
                yVarA = hVarF8.a();
            }
            cVarP.getClass();
            str.getClass();
            cVarP.c();
            androidx.datastore.preferences.e.n((androidx.datastore.preferences.e) cVarP.f2164b).put(str, (i) yVarA);
        }
        androidx.datastore.preferences.e eVar = (androidx.datastore.preferences.e) cVarP.a();
        int iA = eVar.a(null);
        Logger logger = androidx.datastore.preferences.protobuf.n.f2117c;
        if (iA > 4096) {
            iA = 4096;
        }
        m mVar = new m(g0Var, iA);
        eVar.m(mVar);
        if (mVar.f2114g > 0) {
            mVar.d0();
        }
    }
}
