package cb;

import ab.f;
import android.util.Base64;
import android.util.JsonWriter;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.firebase.encoders.EncodingException;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/* JADX INFO: loaded from: classes2.dex */
public final class e implements ab.d, f {

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

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final ab.c f4956e;

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

    public e(BufferedWriter bufferedWriter, HashMap map, HashMap map2, a aVar, boolean z10) {
        this.f4953b = new JsonWriter(bufferedWriter);
        this.f4954c = map;
        this.f4955d = map2;
        this.f4956e = aVar;
        this.f4957f = z10;
    }

    private e internalAdd(@NonNull String str, @Nullable Object obj) throws EncodingException, IOException {
        d();
        JsonWriter jsonWriter = this.f4953b;
        jsonWriter.name(str);
        if (obj != null) {
            return add(obj, false);
        }
        jsonWriter.nullValue();
        return this;
    }

    private e internalAddIgnoreNullValues(@NonNull String str, @Nullable Object obj) throws EncodingException, IOException {
        if (obj == null) {
            return this;
        }
        d();
        this.f4953b.name(str);
        return add(obj, false);
    }

    @Override // ab.d
    public final ab.d a(ab.b bVar, int i10) throws IOException {
        String str = bVar.f418a;
        d();
        JsonWriter jsonWriter = this.f4953b;
        jsonWriter.name(str);
        d();
        jsonWriter.value(i10);
        return this;
    }

    @Override // ab.d
    public final ab.d b(ab.b bVar, long j10) throws IOException {
        String str = bVar.f418a;
        d();
        JsonWriter jsonWriter = this.f4953b;
        jsonWriter.name(str);
        d();
        jsonWriter.value(j10);
        return this;
    }

    @Override // ab.f
    public final f c(boolean z10) throws IOException {
        d();
        this.f4953b.value(z10);
        return this;
    }

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

    @Override // ab.d
    @NonNull
    public ab.d inline(@Nullable Object obj) throws IOException {
        return add(obj, true);
    }

    @Override // ab.d
    @NonNull
    public ab.d add(@NonNull ab.b bVar, @Nullable Object obj) throws IOException {
        return add(bVar.f418a, obj);
    }

    @Override // ab.d
    @NonNull
    public e add(@NonNull String str, @Nullable Object obj) throws IOException {
        if (this.f4957f) {
            return internalAddIgnoreNullValues(str, obj);
        }
        return internalAdd(str, obj);
    }

    @Override // ab.f
    @NonNull
    public e add(@Nullable String str) throws IOException {
        d();
        this.f4953b.value(str);
        return this;
    }

    @NonNull
    public e add(@Nullable byte[] bArr) throws IOException {
        d();
        JsonWriter jsonWriter = this.f4953b;
        if (bArr == null) {
            jsonWriter.nullValue();
        } else {
            jsonWriter.value(Base64.encodeToString(bArr, 2));
        }
        return this;
    }

    @NonNull
    public e add(@Nullable Object obj, boolean z10) throws IOException {
        int i10 = 0;
        if (z10 && (obj == null || obj.getClass().isArray() || (obj instanceof Collection) || (obj instanceof Date) || (obj instanceof Enum) || (obj instanceof Number))) {
            Object[] objArr = new Object[1];
            objArr[0] = obj == null ? null : obj.getClass();
            throw new EncodingException(String.format("%s cannot be encoded inline", objArr));
        }
        JsonWriter jsonWriter = this.f4953b;
        if (obj == null) {
            jsonWriter.nullValue();
            return this;
        }
        if (obj instanceof Number) {
            jsonWriter.value((Number) obj);
            return this;
        }
        if (obj.getClass().isArray()) {
            if (obj instanceof byte[]) {
                return add((byte[]) obj);
            }
            jsonWriter.beginArray();
            if (obj instanceof int[]) {
                int length = ((int[]) obj).length;
                while (i10 < length) {
                    jsonWriter.value(r5[i10]);
                    i10++;
                }
            } else if (obj instanceof long[]) {
                long[] jArr = (long[]) obj;
                int length2 = jArr.length;
                while (i10 < length2) {
                    long j10 = jArr[i10];
                    d();
                    jsonWriter.value(j10);
                    i10++;
                }
            } else if (obj instanceof double[]) {
                double[] dArr = (double[]) obj;
                int length3 = dArr.length;
                while (i10 < length3) {
                    jsonWriter.value(dArr[i10]);
                    i10++;
                }
            } else if (obj instanceof boolean[]) {
                boolean[] zArr = (boolean[]) obj;
                int length4 = zArr.length;
                while (i10 < length4) {
                    jsonWriter.value(zArr[i10]);
                    i10++;
                }
            } else if (obj instanceof Number[]) {
                for (Number number : (Number[]) obj) {
                    add((Object) number, false);
                }
            } else {
                for (Object obj2 : (Object[]) obj) {
                    add(obj2, false);
                }
            }
            jsonWriter.endArray();
            return this;
        }
        if (obj instanceof Collection) {
            jsonWriter.beginArray();
            Iterator it = ((Collection) obj).iterator();
            while (it.hasNext()) {
                add(it.next(), false);
            }
            jsonWriter.endArray();
            return this;
        }
        if (obj instanceof Map) {
            jsonWriter.beginObject();
            for (Map.Entry entry : ((Map) obj).entrySet()) {
                Object key = entry.getKey();
                try {
                    add((String) key, entry.getValue());
                } catch (ClassCastException e10) {
                    throw new EncodingException(String.format("Only String keys are currently supported in maps, got %s of type %s instead.", key, key.getClass()), e10);
                }
            }
            jsonWriter.endObject();
            return this;
        }
        ab.c cVar = (ab.c) this.f4954c.get(obj.getClass());
        if (cVar != null) {
            if (!z10) {
                jsonWriter.beginObject();
            }
            cVar.a(obj, this);
            if (!z10) {
                jsonWriter.endObject();
            }
            return this;
        }
        ab.e eVar = (ab.e) this.f4955d.get(obj.getClass());
        if (eVar != null) {
            eVar.a(obj, this);
            return this;
        }
        if (obj instanceof Enum) {
            add(((Enum) obj).name());
            return this;
        }
        if (!z10) {
            jsonWriter.beginObject();
        }
        this.f4956e.a(obj, this);
        if (!z10) {
            jsonWriter.endObject();
        }
        return this;
    }
}
