package z4;

import android.util.Base64;
import android.util.JsonWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import x4.C1717b;
import x4.C1718c;
import x4.f;
import x4.g;

/* JADX INFO: loaded from: classes.dex */
public final class d implements x4.e, g {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final boolean f19365a = true;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final JsonWriter f19366b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final HashMap f19367c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final HashMap f19368d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final A4.e f19369e;

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

    public d(Writer writer, HashMap map, HashMap map2, A4.e eVar, boolean z8) {
        this.f19366b = new JsonWriter(writer);
        this.f19367c = map;
        this.f19368d = map2;
        this.f19369e = eVar;
        this.f19370f = z8;
    }

    @Override // x4.g
    public final g a(String str) throws IOException {
        j();
        this.f19366b.value(str);
        return this;
    }

    @Override // x4.g
    public final g b(boolean z8) throws IOException {
        j();
        this.f19366b.value(z8);
        return this;
    }

    @Override // x4.e
    public final x4.e c(C1718c c1718c, double d8) throws IOException {
        String str = c1718c.f18672a;
        j();
        JsonWriter jsonWriter = this.f19366b;
        jsonWriter.name(str);
        j();
        jsonWriter.value(d8);
        return this;
    }

    @Override // x4.e
    public final x4.e d(C1718c c1718c, Object obj) throws IOException {
        i(obj, c1718c.f18672a);
        return this;
    }

    @Override // x4.e
    public final x4.e e(C1718c c1718c, int i) throws IOException {
        String str = c1718c.f18672a;
        j();
        JsonWriter jsonWriter = this.f19366b;
        jsonWriter.name(str);
        j();
        jsonWriter.value(i);
        return this;
    }

    @Override // x4.e
    public final x4.e f(C1718c c1718c, long j8) throws IOException {
        String str = c1718c.f18672a;
        j();
        JsonWriter jsonWriter = this.f19366b;
        jsonWriter.name(str);
        j();
        jsonWriter.value(j8);
        return this;
    }

    @Override // x4.e
    public final x4.e g(C1718c c1718c, boolean z8) throws IOException {
        String str = c1718c.f18672a;
        j();
        JsonWriter jsonWriter = this.f19366b;
        jsonWriter.name(str);
        j();
        jsonWriter.value(z8);
        return this;
    }

    public final d h(Object obj) {
        int i = 0;
        JsonWriter jsonWriter = this.f19366b;
        if (obj == null) {
            jsonWriter.nullValue();
            return this;
        }
        if (obj instanceof Number) {
            jsonWriter.value((Number) obj);
            return this;
        }
        if (!obj.getClass().isArray()) {
            if (obj instanceof Collection) {
                jsonWriter.beginArray();
                Iterator it = ((Collection) obj).iterator();
                while (it.hasNext()) {
                    h(it.next());
                }
                jsonWriter.endArray();
                return this;
            }
            if (obj instanceof Map) {
                jsonWriter.beginObject();
                for (Map.Entry entry : ((Map) obj).entrySet()) {
                    Object key = entry.getKey();
                    try {
                        i(entry.getValue(), (String) key);
                    } catch (ClassCastException e8) {
                        throw new C1717b(String.format("Only String keys are currently supported in maps, got %s of type %s instead.", key, key.getClass()), e8);
                    }
                }
                jsonWriter.endObject();
                return this;
            }
            x4.d dVar = (x4.d) this.f19367c.get(obj.getClass());
            if (dVar != null) {
                jsonWriter.beginObject();
                dVar.a(obj, this);
                jsonWriter.endObject();
                return this;
            }
            f fVar = (f) this.f19368d.get(obj.getClass());
            if (fVar != null) {
                fVar.a(obj, this);
                return this;
            }
            if (!(obj instanceof Enum)) {
                jsonWriter.beginObject();
                this.f19369e.a(obj, this);
                throw null;
            }
            if (obj instanceof e) {
                int iA = ((e) obj).a();
                j();
                jsonWriter.value(iA);
                return this;
            }
            String strName = ((Enum) obj).name();
            j();
            jsonWriter.value(strName);
            return this;
        }
        if (obj instanceof byte[]) {
            j();
            jsonWriter.value(Base64.encodeToString((byte[]) obj, 2));
            return this;
        }
        jsonWriter.beginArray();
        if (obj instanceof int[]) {
            int length = ((int[]) obj).length;
            while (i < length) {
                jsonWriter.value(r8[i]);
                i++;
            }
        } else if (obj instanceof long[]) {
            long[] jArr = (long[]) obj;
            int length2 = jArr.length;
            while (i < length2) {
                long j8 = jArr[i];
                j();
                jsonWriter.value(j8);
                i++;
            }
        } else if (obj instanceof double[]) {
            double[] dArr = (double[]) obj;
            int length3 = dArr.length;
            while (i < length3) {
                jsonWriter.value(dArr[i]);
                i++;
            }
        } else if (obj instanceof boolean[]) {
            boolean[] zArr = (boolean[]) obj;
            int length4 = zArr.length;
            while (i < length4) {
                jsonWriter.value(zArr[i]);
                i++;
            }
        } else if (obj instanceof Number[]) {
            Number[] numberArr = (Number[]) obj;
            int length5 = numberArr.length;
            while (i < length5) {
                h(numberArr[i]);
                i++;
            }
        } else {
            Object[] objArr = (Object[]) obj;
            int length6 = objArr.length;
            while (i < length6) {
                h(objArr[i]);
                i++;
            }
        }
        jsonWriter.endArray();
        return this;
    }

    public final d i(Object obj, String str) throws IOException {
        boolean z8 = this.f19370f;
        JsonWriter jsonWriter = this.f19366b;
        if (z8) {
            if (obj == null) {
                return this;
            }
            j();
            jsonWriter.name(str);
            h(obj);
            return this;
        }
        j();
        jsonWriter.name(str);
        if (obj == null) {
            jsonWriter.nullValue();
            return this;
        }
        h(obj);
        return this;
    }

    public final void j() {
        if (!this.f19365a) {
            throw new IllegalStateException("Parent context used since this context was created. Cannot use this context anymore.");
        }
    }
}
