package com.google.firebase.encoders.proto;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.firebase.encoders.EncodingException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import xyz.stream.utils.e0;

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Charset f24067f = Charset.forName("UTF-8");

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final ab.b f24068g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final ab.b f24069h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final cb.a f24070i;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public OutputStream f24071a;

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final f f24075e = new f(this);

    static {
        e0 e0Var = new e0("key");
        a aVar = new a();
        aVar.f24065a = 1;
        f24068g = u.a.a(aVar, e0Var);
        e0 e0Var2 = new e0("value");
        a aVar2 = new a();
        aVar2.f24065a = 2;
        f24069h = u.a.a(aVar2, e0Var2);
        f24070i = new cb.a(1);
    }

    public d(ByteArrayOutputStream byteArrayOutputStream, Map map, Map map2, ab.c cVar) {
        this.f24071a = byteArrayOutputStream;
        this.f24072b = map;
        this.f24073c = map2;
        this.f24074d = cVar;
    }

    public static int f(ab.b bVar) {
        Protobuf protobuf = (Protobuf) bVar.getProperty(Protobuf.class);
        if (protobuf != null) {
            return protobuf.tag();
        }
        throw new EncodingException("Field has no @Protobuf config");
    }

    @Override // ab.d
    public final ab.d a(ab.b bVar, int i10) {
        c(bVar, i10, true);
        return this;
    }

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

    @Override // ab.d
    public final ab.d b(ab.b bVar, long j10) throws IOException {
        d(bVar, j10, true);
        return this;
    }

    public final void c(ab.b bVar, int i10, boolean z10) {
        if (z10 && i10 == 0) {
            return;
        }
        Protobuf protobuf = (Protobuf) bVar.getProperty(Protobuf.class);
        if (protobuf == null) {
            throw new EncodingException("Field has no @Protobuf config");
        }
        int iOrdinal = protobuf.intEncoding().ordinal();
        if (iOrdinal == 0) {
            g(protobuf.tag() << 3);
            g(i10);
        } else if (iOrdinal == 1) {
            g(protobuf.tag() << 3);
            g((i10 << 1) ^ (i10 >> 31));
        } else {
            if (iOrdinal != 2) {
                return;
            }
            g((protobuf.tag() << 3) | 5);
            this.f24071a.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(i10).array());
        }
    }

    public final void d(ab.b bVar, long j10, boolean z10) throws IOException {
        if (z10 && j10 == 0) {
            return;
        }
        Protobuf protobuf = (Protobuf) bVar.getProperty(Protobuf.class);
        if (protobuf == null) {
            throw new EncodingException("Field has no @Protobuf config");
        }
        int iOrdinal = protobuf.intEncoding().ordinal();
        if (iOrdinal == 0) {
            g(protobuf.tag() << 3);
            h(j10);
        } else if (iOrdinal == 1) {
            g(protobuf.tag() << 3);
            h((j10 >> 63) ^ (j10 << 1));
        } else {
            if (iOrdinal != 2) {
                return;
            }
            g((protobuf.tag() << 3) | 1);
            this.f24071a.write(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(j10).array());
        }
    }

    public final void e(ab.c cVar, ab.b bVar, Object obj, boolean z10) throws IOException {
        b bVar2 = new b();
        bVar2.f24066a = 0L;
        try {
            OutputStream outputStream = this.f24071a;
            this.f24071a = bVar2;
            try {
                cVar.a(obj, this);
                this.f24071a = outputStream;
                long j10 = bVar2.f24066a;
                bVar2.close();
                if (z10 && j10 == 0) {
                    return;
                }
                g((f(bVar) << 3) | 2);
                h(j10);
                cVar.a(obj, this);
            } catch (Throwable th) {
                this.f24071a = outputStream;
                throw th;
            }
        } catch (Throwable th2) {
            try {
                bVar2.close();
            } catch (Throwable th3) {
                th2.addSuppressed(th3);
            }
            throw th2;
        }
    }

    public d encode(@Nullable Object obj) throws IOException {
        if (obj == null) {
            return this;
        }
        ab.c cVar = (ab.c) this.f24072b.get(obj.getClass());
        if (cVar != null) {
            cVar.a(obj, this);
            return this;
        }
        throw new EncodingException("No encoder for " + obj.getClass());
    }

    public final void g(int i10) throws IOException {
        while ((i10 & (-128)) != 0) {
            this.f24071a.write((i10 & 127) | 128);
            i10 >>>= 7;
        }
        this.f24071a.write(i10 & 127);
    }

    public final void h(long j10) throws IOException {
        while (((-128) & j10) != 0) {
            this.f24071a.write((((int) j10) & 127) | 128);
            j10 >>>= 7;
        }
        this.f24071a.write(((int) j10) & 127);
    }

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

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

    public ab.d add(@NonNull ab.b bVar, @Nullable Object obj, boolean z10) throws IOException {
        if (obj == null) {
            return this;
        }
        if (obj instanceof CharSequence) {
            CharSequence charSequence = (CharSequence) obj;
            if (z10 && charSequence.length() == 0) {
                return this;
            }
            g((f(bVar) << 3) | 2);
            byte[] bytes = charSequence.toString().getBytes(f24067f);
            g(bytes.length);
            this.f24071a.write(bytes);
            return this;
        }
        if (obj instanceof Collection) {
            Iterator it = ((Collection) obj).iterator();
            while (it.hasNext()) {
                add(bVar, it.next(), false);
            }
            return this;
        }
        if (obj instanceof Map) {
            Iterator it2 = ((Map) obj).entrySet().iterator();
            while (it2.hasNext()) {
                e(f24070i, bVar, (Map.Entry) it2.next(), false);
            }
            return this;
        }
        if (obj instanceof Double) {
            double dDoubleValue = ((Double) obj).doubleValue();
            if (!z10 || dDoubleValue != 0.0d) {
                g((f(bVar) << 3) | 1);
                this.f24071a.write(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putDouble(dDoubleValue).array());
            }
            return this;
        }
        if (obj instanceof Float) {
            float fFloatValue = ((Float) obj).floatValue();
            if (!z10 || fFloatValue != 0.0f) {
                g((f(bVar) << 3) | 5);
                this.f24071a.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(fFloatValue).array());
            }
            return this;
        }
        if (obj instanceof Number) {
            d(bVar, ((Number) obj).longValue(), z10);
            return this;
        }
        if (obj instanceof Boolean) {
            c(bVar, ((Boolean) obj).booleanValue() ? 1 : 0, z10);
            return this;
        }
        if (obj instanceof byte[]) {
            byte[] bArr = (byte[]) obj;
            if (z10 && bArr.length == 0) {
                return this;
            }
            g((f(bVar) << 3) | 2);
            g(bArr.length);
            this.f24071a.write(bArr);
            return this;
        }
        ab.c cVar = (ab.c) this.f24072b.get(obj.getClass());
        if (cVar != null) {
            e(cVar, bVar, obj, z10);
            return this;
        }
        ab.e eVar = (ab.e) this.f24073c.get(obj.getClass());
        if (eVar != null) {
            f fVar = this.f24075e;
            fVar.f24080a = false;
            fVar.f24082c = bVar;
            fVar.f24081b = z10;
            eVar.a(obj, fVar);
            return this;
        }
        if (obj instanceof c) {
            c(bVar, ((c) obj).a(), true);
            return this;
        }
        if (obj instanceof Enum) {
            c(bVar, ((Enum) obj).ordinal(), true);
            return this;
        }
        e(this.f24074d, bVar, obj, z10);
        return this;
    }
}
