package g.e.b.g0.f0;

import g.e.b.d0;
import g.e.b.e0;
import g.e.b.g0.x;
import g.e.b.i0.a;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import kotlinx.serialization.json.internal.AbstractJsonLexerKt;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final g.e.b.g0.l f4051f;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final boolean f4052m;

    public final class a<K, V> extends d0<Map<K, V>> {
        public final d0<K> a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final d0<V> f4053b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public final x<? extends Map<K, V>> f4054c;

        public a(g.e.b.j jVar, Type type, d0<K> d0Var, Type type2, d0<V> d0Var2, x<? extends Map<K, V>> xVar) {
            this.a = new p(jVar, d0Var, type);
            this.f4053b = new p(jVar, d0Var2, type2);
            this.f4054c = xVar;
        }

        @Override // g.e.b.d0
        public Object a(g.e.b.i0.a aVar) throws IOException {
            int i2;
            g.e.b.i0.b bVarI = aVar.I();
            if (bVarI == g.e.b.i0.b.NULL) {
                aVar.E();
                return null;
            }
            Map<K, V> mapA = this.f4054c.a();
            if (bVarI == g.e.b.i0.b.BEGIN_ARRAY) {
                aVar.a();
                while (aVar.q()) {
                    aVar.a();
                    K kA = this.a.a(aVar);
                    if (mapA.put(kA, this.f4053b.a(aVar)) != null) {
                        throw new g.e.b.x("duplicate key: " + kA);
                    }
                    aVar.g();
                }
                aVar.g();
            } else {
                aVar.c();
                while (aVar.q()) {
                    Objects.requireNonNull((a.C0064a) g.e.b.g0.u.a);
                    if (aVar instanceof f) {
                        f fVar = (f) aVar;
                        fVar.P(g.e.b.i0.b.NAME);
                        Map.Entry entry = (Map.Entry) ((Iterator) fVar.Q()).next();
                        fVar.S(entry.getValue());
                        fVar.S(new g.e.b.u((String) entry.getKey()));
                    } else {
                        int iE = aVar.f4175s;
                        if (iE == 0) {
                            iE = aVar.e();
                        }
                        if (iE == 13) {
                            i2 = 9;
                        } else if (iE == 12) {
                            i2 = 8;
                        } else {
                            if (iE != 14) {
                                StringBuilder sbF = g.a.a.a.a.F("Expected a name but was ");
                                sbF.append(aVar.I());
                                sbF.append(aVar.u());
                                throw new IllegalStateException(sbF.toString());
                            }
                            i2 = 10;
                        }
                        aVar.f4175s = i2;
                    }
                    K kA2 = this.a.a(aVar);
                    if (mapA.put(kA2, this.f4053b.a(aVar)) != null) {
                        throw new g.e.b.x("duplicate key: " + kA2);
                    }
                }
                aVar.h();
            }
            return mapA;
        }

        @Override // g.e.b.d0
        public void b(g.e.b.i0.c cVar, Object obj) {
            String strD;
            Map map = (Map) obj;
            if (map == null) {
                cVar.m();
                return;
            }
            if (h.this.f4052m) {
                ArrayList arrayList = new ArrayList(map.size());
                ArrayList arrayList2 = new ArrayList(map.size());
                int i2 = 0;
                boolean z2 = false;
                for (Map.Entry<K, V> entry : map.entrySet()) {
                    d0<K> d0Var = this.a;
                    K key = entry.getKey();
                    Objects.requireNonNull(d0Var);
                    try {
                        g gVar = new g();
                        d0Var.b(gVar, key);
                        if (!gVar.f4050z.isEmpty()) {
                            throw new IllegalStateException("Expected one JSON element but was " + gVar.f4050z);
                        }
                        g.e.b.o oVar = gVar.B;
                        arrayList.add(oVar);
                        arrayList2.add(entry.getValue());
                        Objects.requireNonNull(oVar);
                        z2 |= (oVar instanceof g.e.b.l) || (oVar instanceof g.e.b.r);
                    } catch (IOException e2) {
                        throw new g.e.b.p(e2);
                    }
                }
                if (z2) {
                    cVar.c();
                    int size = arrayList.size();
                    while (i2 < size) {
                        cVar.c();
                        q.V.b(cVar, (g.e.b.o) arrayList.get(i2));
                        this.f4053b.b(cVar, (V) arrayList2.get(i2));
                        cVar.g();
                        i2++;
                    }
                    cVar.g();
                    return;
                }
                cVar.d();
                int size2 = arrayList.size();
                while (i2 < size2) {
                    g.e.b.o oVar2 = (g.e.b.o) arrayList.get(i2);
                    Objects.requireNonNull(oVar2);
                    if (oVar2 instanceof g.e.b.u) {
                        g.e.b.u uVarC = oVar2.c();
                        Object obj2 = uVarC.a;
                        if (obj2 instanceof Number) {
                            strD = String.valueOf(uVarC.e());
                        } else if (obj2 instanceof Boolean) {
                            strD = Boolean.toString(uVarC.a());
                        } else {
                            if (!(obj2 instanceof String)) {
                                throw new AssertionError();
                            }
                            strD = uVarC.d();
                        }
                    } else {
                        if (!(oVar2 instanceof g.e.b.q)) {
                            throw new AssertionError();
                        }
                        strD = AbstractJsonLexerKt.NULL;
                    }
                    cVar.j(strD);
                    this.f4053b.b(cVar, (V) arrayList2.get(i2));
                    i2++;
                }
            } else {
                cVar.d();
                for (Map.Entry<K, V> entry2 : map.entrySet()) {
                    cVar.j(String.valueOf(entry2.getKey()));
                    this.f4053b.b(cVar, entry2.getValue());
                }
            }
            cVar.h();
        }
    }

    public h(g.e.b.g0.l lVar, boolean z2) {
        this.f4051f = lVar;
        this.f4052m = z2;
    }

    @Override // g.e.b.e0
    public <T> d0<T> b(g.e.b.j jVar, g.e.b.h0.a<T> aVar) {
        Type[] actualTypeArguments;
        Type type = aVar.f4166b;
        Class<? super T> cls = aVar.a;
        if (!Map.class.isAssignableFrom(cls)) {
            return null;
        }
        if (type == Properties.class) {
            actualTypeArguments = new Type[]{String.class, String.class};
        } else {
            Type typeG = g.e.b.g0.a.g(type, cls, Map.class);
            actualTypeArguments = typeG instanceof ParameterizedType ? ((ParameterizedType) typeG).getActualTypeArguments() : new Type[]{Object.class, Object.class};
        }
        Type type2 = actualTypeArguments[0];
        return new a(jVar, actualTypeArguments[0], (type2 == Boolean.TYPE || type2 == Boolean.class) ? q.f4087f : jVar.d(new g.e.b.h0.a<>(type2)), actualTypeArguments[1], jVar.d(new g.e.b.h0.a<>(actualTypeArguments[1])), this.f4051f.a(aVar));
    }
}
