package V2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import p0.AbstractC0928a;

/* JADX INFO: loaded from: classes.dex */
public final class f implements S2.e {

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final S2.c f2994g = new S2.c("key", AbstractC0928a.p(AbstractC0928a.o(e.class, new a(1))));

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final S2.c f2995h = new S2.c("value", AbstractC0928a.p(AbstractC0928a.o(e.class, new a(2))));
    public static final U2.a i = new U2.a(1);

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final U2.a f2999d;

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

    public f(ByteArrayOutputStream byteArrayOutputStream, HashMap map, HashMap map2, U2.a aVar) {
        this.f2996a = byteArrayOutputStream;
        this.f2997b = map;
        this.f2998c = map2;
        this.f2999d = aVar;
    }

    public static int g(S2.c cVar) {
        e eVar = (e) ((Annotation) cVar.f2810b.get(e.class));
        if (eVar != null) {
            return ((a) eVar).f2989a;
        }
        throw new S2.b("Field has no @Protobuf config");
    }

    @Override // S2.e
    public final S2.e a(S2.c cVar, int i5) {
        c(cVar, i5, true);
        return this;
    }

    @Override // S2.e
    public final S2.e b(S2.c cVar, long j5) throws IOException {
        if (j5 == 0) {
            return this;
        }
        e eVar = (e) ((Annotation) cVar.f2810b.get(e.class));
        if (eVar == null) {
            throw new S2.b("Field has no @Protobuf config");
        }
        h(((a) eVar).f2989a << 3);
        i(j5);
        return this;
    }

    public final void c(S2.c cVar, int i5, boolean z5) {
        if (z5 && i5 == 0) {
            return;
        }
        e eVar = (e) ((Annotation) cVar.f2810b.get(e.class));
        if (eVar == null) {
            throw new S2.b("Field has no @Protobuf config");
        }
        h(((a) eVar).f2989a << 3);
        h(i5);
    }

    public final void d(S2.c cVar, Object obj, boolean z5) {
        if (obj == null) {
            return;
        }
        if (obj instanceof CharSequence) {
            CharSequence charSequence = (CharSequence) obj;
            if (z5 && charSequence.length() == 0) {
                return;
            }
            h((g(cVar) << 3) | 2);
            byte[] bytes = charSequence.toString().getBytes(f2993f);
            h(bytes.length);
            this.f2996a.write(bytes);
            return;
        }
        if (obj instanceof Collection) {
            Iterator it = ((Collection) obj).iterator();
            while (it.hasNext()) {
                d(cVar, it.next(), false);
            }
            return;
        }
        if (obj instanceof Map) {
            Iterator it2 = ((Map) obj).entrySet().iterator();
            while (it2.hasNext()) {
                f(i, cVar, (Map.Entry) it2.next(), false);
            }
            return;
        }
        if (obj instanceof Double) {
            double dDoubleValue = ((Double) obj).doubleValue();
            if (z5 && dDoubleValue == 0.0d) {
                return;
            }
            h((g(cVar) << 3) | 1);
            this.f2996a.write(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putDouble(dDoubleValue).array());
            return;
        }
        if (obj instanceof Float) {
            float fFloatValue = ((Float) obj).floatValue();
            if (z5 && fFloatValue == 0.0f) {
                return;
            }
            h((g(cVar) << 3) | 5);
            this.f2996a.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(fFloatValue).array());
            return;
        }
        if (obj instanceof Number) {
            long jLongValue = ((Number) obj).longValue();
            if (z5 && jLongValue == 0) {
                return;
            }
            e eVar = (e) ((Annotation) cVar.f2810b.get(e.class));
            if (eVar == null) {
                throw new S2.b("Field has no @Protobuf config");
            }
            h(((a) eVar).f2989a << 3);
            i(jLongValue);
            return;
        }
        if (obj instanceof Boolean) {
            c(cVar, ((Boolean) obj).booleanValue() ? 1 : 0, z5);
            return;
        }
        if (obj instanceof byte[]) {
            byte[] bArr = (byte[]) obj;
            if (z5 && bArr.length == 0) {
                return;
            }
            h((g(cVar) << 3) | 2);
            h(bArr.length);
            this.f2996a.write(bArr);
            return;
        }
        S2.d dVar = (S2.d) this.f2997b.get(obj.getClass());
        if (dVar != null) {
            f(dVar, cVar, obj, z5);
            return;
        }
        S2.f fVar = (S2.f) this.f2998c.get(obj.getClass());
        if (fVar != null) {
            h hVar = this.f3000e;
            hVar.f3002a = false;
            hVar.f3004c = cVar;
            hVar.f3003b = z5;
            fVar.a(obj, hVar);
            return;
        }
        if (obj instanceof c) {
            c(cVar, ((c) obj).a(), true);
        } else if (obj instanceof Enum) {
            c(cVar, ((Enum) obj).ordinal(), true);
        } else {
            f(this.f2999d, cVar, obj, z5);
        }
    }

    @Override // S2.e
    public final S2.e e(S2.c cVar, Object obj) {
        d(cVar, obj, true);
        return this;
    }

    public final void f(S2.d dVar, S2.c cVar, Object obj, boolean z5) throws IOException {
        b bVar = new b();
        bVar.f2990k = 0L;
        try {
            OutputStream outputStream = this.f2996a;
            this.f2996a = bVar;
            try {
                dVar.a(obj, this);
                this.f2996a = outputStream;
                long j5 = bVar.f2990k;
                bVar.close();
                if (z5 && j5 == 0) {
                    return;
                }
                h((g(cVar) << 3) | 2);
                i(j5);
                dVar.a(obj, this);
            } catch (Throwable th) {
                this.f2996a = outputStream;
                throw th;
            }
        } catch (Throwable th2) {
            try {
                bVar.close();
            } catch (Throwable th3) {
                th2.addSuppressed(th3);
            }
            throw th2;
        }
    }

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

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