package A4;

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.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import x4.C1717b;
import x4.C1718c;

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final C1718c f402g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final C1718c f403h;
    public static final e i;

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

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

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

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

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

    static {
        a aVar = new a(1);
        HashMap map = new HashMap();
        map.put(d.class, aVar);
        f402g = new C1718c("key", Collections.unmodifiableMap(new HashMap(map)));
        a aVar2 = new a(2);
        HashMap map2 = new HashMap();
        map2.put(d.class, aVar2);
        f403h = new C1718c("value", Collections.unmodifiableMap(new HashMap(map2)));
        i = new e(0);
    }

    public f(ByteArrayOutputStream byteArrayOutputStream, HashMap map, HashMap map2, e eVar) {
        this.f404a = byteArrayOutputStream;
        this.f405b = map;
        this.f406c = map2;
        this.f407d = eVar;
    }

    public static int j(C1718c c1718c) {
        d dVar = (d) ((Annotation) c1718c.f18673b.get(d.class));
        if (dVar != null) {
            return ((a) dVar).f396a;
        }
        throw new C1717b("Field has no @Protobuf config");
    }

    public final void a(C1718c c1718c, double d8, boolean z8) throws IOException {
        if (z8 && d8 == 0.0d) {
            return;
        }
        k((j(c1718c) << 3) | 1);
        this.f404a.write(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putDouble(d8).array());
    }

    public final void b(C1718c c1718c, int i5, boolean z8) {
        if (z8 && i5 == 0) {
            return;
        }
        d dVar = (d) ((Annotation) c1718c.f18673b.get(d.class));
        if (dVar == null) {
            throw new C1717b("Field has no @Protobuf config");
        }
        k(((a) dVar).f396a << 3);
        k(i5);
    }

    @Override // x4.e
    public final x4.e c(C1718c c1718c, double d8) throws IOException {
        a(c1718c, d8, true);
        return this;
    }

    @Override // x4.e
    public final x4.e d(C1718c c1718c, Object obj) {
        h(c1718c, obj, true);
        return this;
    }

    @Override // x4.e
    public final x4.e e(C1718c c1718c, int i5) {
        b(c1718c, i5, true);
        return this;
    }

    @Override // x4.e
    public final x4.e f(C1718c c1718c, long j8) throws IOException {
        if (j8 == 0) {
            return this;
        }
        d dVar = (d) ((Annotation) c1718c.f18673b.get(d.class));
        if (dVar == null) {
            throw new C1717b("Field has no @Protobuf config");
        }
        k(((a) dVar).f396a << 3);
        l(j8);
        return this;
    }

    @Override // x4.e
    public final x4.e g(C1718c c1718c, boolean z8) {
        b(c1718c, z8 ? 1 : 0, true);
        return this;
    }

    public final void h(C1718c c1718c, Object obj, boolean z8) {
        if (obj == null) {
            return;
        }
        if (obj instanceof CharSequence) {
            CharSequence charSequence = (CharSequence) obj;
            if (z8 && charSequence.length() == 0) {
                return;
            }
            k((j(c1718c) << 3) | 2);
            byte[] bytes = charSequence.toString().getBytes(f401f);
            k(bytes.length);
            this.f404a.write(bytes);
            return;
        }
        if (obj instanceof Collection) {
            Iterator it = ((Collection) obj).iterator();
            while (it.hasNext()) {
                h(c1718c, it.next(), false);
            }
            return;
        }
        if (obj instanceof Map) {
            Iterator it2 = ((Map) obj).entrySet().iterator();
            while (it2.hasNext()) {
                i(i, c1718c, (Map.Entry) it2.next(), false);
            }
            return;
        }
        if (obj instanceof Double) {
            a(c1718c, ((Double) obj).doubleValue(), z8);
            return;
        }
        if (obj instanceof Float) {
            float fFloatValue = ((Float) obj).floatValue();
            if (z8 && fFloatValue == 0.0f) {
                return;
            }
            k((j(c1718c) << 3) | 5);
            this.f404a.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(fFloatValue).array());
            return;
        }
        if (obj instanceof Number) {
            long jLongValue = ((Number) obj).longValue();
            if (z8 && jLongValue == 0) {
                return;
            }
            d dVar = (d) ((Annotation) c1718c.f18673b.get(d.class));
            if (dVar == null) {
                throw new C1717b("Field has no @Protobuf config");
            }
            k(((a) dVar).f396a << 3);
            l(jLongValue);
            return;
        }
        if (obj instanceof Boolean) {
            b(c1718c, ((Boolean) obj).booleanValue() ? 1 : 0, z8);
            return;
        }
        if (obj instanceof byte[]) {
            byte[] bArr = (byte[]) obj;
            if (z8 && bArr.length == 0) {
                return;
            }
            k((j(c1718c) << 3) | 2);
            k(bArr.length);
            this.f404a.write(bArr);
            return;
        }
        x4.d dVar2 = (x4.d) this.f405b.get(obj.getClass());
        if (dVar2 != null) {
            i(dVar2, c1718c, obj, z8);
            return;
        }
        x4.f fVar = (x4.f) this.f406c.get(obj.getClass());
        if (fVar != null) {
            h hVar = this.f408e;
            hVar.f410a = false;
            hVar.f412c = c1718c;
            hVar.f411b = z8;
            fVar.a(obj, hVar);
            return;
        }
        if (obj instanceof C2.d) {
            b(c1718c, ((C2.d) obj).f1175q, true);
        } else if (obj instanceof Enum) {
            b(c1718c, ((Enum) obj).ordinal(), true);
        } else {
            i(this.f407d, c1718c, obj, z8);
        }
    }

    public final void i(x4.d dVar, C1718c c1718c, Object obj, boolean z8) throws IOException {
        b bVar = new b();
        bVar.f397q = 0L;
        try {
            OutputStream outputStream = this.f404a;
            this.f404a = bVar;
            try {
                dVar.a(obj, this);
                this.f404a = outputStream;
                long j8 = bVar.f397q;
                bVar.close();
                if (z8 && j8 == 0) {
                    return;
                }
                k((j(c1718c) << 3) | 2);
                l(j8);
                dVar.a(obj, this);
            } catch (Throwable th) {
                this.f404a = outputStream;
                throw th;
            }
        } catch (Throwable th2) {
            try {
                bVar.close();
            } catch (Throwable th3) {
                th2.addSuppressed(th3);
            }
            throw th2;
        }
    }

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

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