package com.google.gson.internal.bind;

import com.google.gson.i;
import com.google.gson.n;
import com.google.gson.q;
import com.google.gson.u;
import com.google.gson.v;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Map;
import uk.e;
import uk.h;
import uk.k;

/* loaded from: classes3.dex */
public final class MapTypeAdapterFactory implements v {

    /* renamed from: p, reason: collision with root package name */
    private final uk.c f14888p;

    /* renamed from: q, reason: collision with root package name */
    final boolean f14889q;

    /* loaded from: classes3.dex */
    private final class a extends u {

        /* renamed from: a, reason: collision with root package name */
        private final u f14890a;

        /* renamed from: b, reason: collision with root package name */
        private final u f14891b;

        /* renamed from: c, reason: collision with root package name */
        private final h f14892c;

        public a(com.google.gson.d dVar, Type type, u uVar, Type type2, u uVar2, h hVar) {
            this.f14890a = new d(dVar, uVar, type);
            this.f14891b = new d(dVar, uVar2, type2);
            this.f14892c = hVar;
        }

        private String a(i iVar) {
            if (!iVar.q()) {
                if (iVar.n()) {
                    return "null";
                }
                throw new AssertionError();
            }
            n i10 = iVar.i();
            if (i10.B()) {
                return String.valueOf(i10.v());
            }
            if (i10.x()) {
                return Boolean.toString(i10.r());
            }
            if (i10.C()) {
                return i10.l();
            }
            throw new AssertionError();
        }

        @Override // com.google.gson.u
        /* renamed from: b, reason: merged with bridge method [inline-methods] */
        public Map read(xk.a aVar) {
            xk.b L = aVar.L();
            if (L == xk.b.NULL) {
                aVar.G();
                return null;
            }
            Map map = (Map) this.f14892c.a();
            if (L == xk.b.BEGIN_ARRAY) {
                aVar.a();
                while (aVar.p()) {
                    aVar.a();
                    Object read = this.f14890a.read(aVar);
                    if (map.put(read, this.f14891b.read(aVar)) != null) {
                        throw new q("duplicate key: " + read);
                    }
                    aVar.f();
                }
                aVar.f();
            } else {
                aVar.b();
                while (aVar.p()) {
                    e.f37279a.a(aVar);
                    Object read2 = this.f14890a.read(aVar);
                    if (map.put(read2, this.f14891b.read(aVar)) != null) {
                        throw new q("duplicate key: " + read2);
                    }
                }
                aVar.g();
            }
            return map;
        }

        @Override // com.google.gson.u
        /* renamed from: c, reason: merged with bridge method [inline-methods] */
        public void write(xk.c cVar, Map map) {
            if (map == null) {
                cVar.t();
                return;
            }
            if (!MapTypeAdapterFactory.this.f14889q) {
                cVar.d();
                for (Map.Entry entry : map.entrySet()) {
                    cVar.r(String.valueOf(entry.getKey()));
                    this.f14891b.write(cVar, entry.getValue());
                }
                cVar.g();
                return;
            }
            ArrayList arrayList = new ArrayList(map.size());
            ArrayList arrayList2 = new ArrayList(map.size());
            int i10 = 0;
            boolean z10 = false;
            for (Map.Entry entry2 : map.entrySet()) {
                i jsonTree = this.f14890a.toJsonTree(entry2.getKey());
                arrayList.add(jsonTree);
                arrayList2.add(entry2.getValue());
                z10 |= jsonTree.m() || jsonTree.o();
            }
            if (!z10) {
                cVar.d();
                int size = arrayList.size();
                while (i10 < size) {
                    cVar.r(a((i) arrayList.get(i10)));
                    this.f14891b.write(cVar, arrayList2.get(i10));
                    i10++;
                }
                cVar.g();
                return;
            }
            cVar.c();
            int size2 = arrayList.size();
            while (i10 < size2) {
                cVar.c();
                k.b((i) arrayList.get(i10), cVar);
                this.f14891b.write(cVar, arrayList2.get(i10));
                cVar.f();
                i10++;
            }
            cVar.f();
        }
    }

    public MapTypeAdapterFactory(uk.c cVar, boolean z10) {
        this.f14888p = cVar;
        this.f14889q = z10;
    }

    private u a(com.google.gson.d dVar, Type type) {
        return (type == Boolean.TYPE || type == Boolean.class) ? TypeAdapters.f14937f : dVar.m(com.google.gson.reflect.a.get(type));
    }

    @Override // com.google.gson.v
    public u create(com.google.gson.d dVar, com.google.gson.reflect.a aVar) {
        Type type = aVar.getType();
        if (!Map.class.isAssignableFrom(aVar.getRawType())) {
            return null;
        }
        Type[] j10 = uk.b.j(type, uk.b.k(type));
        return new a(dVar, j10[0], a(dVar, j10[0]), j10[1], dVar.m(com.google.gson.reflect.a.get(j10[1])), this.f14888p.a(aVar));
    }
}
