package defpackage;

import com.google.firebase.encoders.EncodingException;
import j$.util.DesugarCollections;
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;

/* renamed from: ˋˁᵟ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C1457 implements InterfaceC4148 {

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public static final C0613 f5885;

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public static final Charset f5886 = Charset.forName("UTF-8");

    /* renamed from: ᵝᵵᵺ, reason: contains not printable characters */
    public static final C0962 f5887;

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public static final C0613 f5888;

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public final C4410 f5889 = new C4410(this);

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final HashMap f5890;

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public OutputStream f5891;

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final HashMap f5892;

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final InterfaceC3555 f5893;

    static {
        C2109 c2109 = new C2109(1);
        HashMap map = new HashMap();
        map.put(InterfaceC0513.class, c2109);
        f5885 = new C0613("key", DesugarCollections.unmodifiableMap(new HashMap(map)));
        C2109 c21092 = new C2109(2);
        HashMap map2 = new HashMap();
        map2.put(InterfaceC0513.class, c21092);
        f5888 = new C0613("value", DesugarCollections.unmodifiableMap(new HashMap(map2)));
        f5887 = new C0962(1);
    }

    public C1457(ByteArrayOutputStream byteArrayOutputStream, HashMap map, HashMap map2, InterfaceC3555 interfaceC3555) {
        this.f5891 = byteArrayOutputStream;
        this.f5892 = map;
        this.f5890 = map2;
        this.f5893 = interfaceC3555;
    }

    /* renamed from: ᵸˑ, reason: contains not printable characters */
    public static int m4360(C0613 c0613) {
        InterfaceC0513 interfaceC0513 = (InterfaceC0513) ((Annotation) c0613.f3081.get(InterfaceC0513.class));
        if (interfaceC0513 != null) {
            return interfaceC0513.tag();
        }
        throw new EncodingException("Field has no @Protobuf config");
    }

    @Override // defpackage.InterfaceC4148
    /* renamed from: ˇʼʤˎ */
    public final InterfaceC4148 mo4003(C0613 c0613, int i) {
        m4362(c0613, i, true);
        return this;
    }

    /* renamed from: ˌᵜᵹᵩ, reason: contains not printable characters */
    public final void m4361(int i) throws IOException {
        while (true) {
            long j = i & (-128);
            OutputStream outputStream = this.f5891;
            if (j == 0) {
                outputStream.write(i & 127);
                return;
            } else {
                outputStream.write((i & 127) | 128);
                i >>>= 7;
            }
        }
    }

    @Override // defpackage.InterfaceC4148
    /* renamed from: ˎˏʜʾ */
    public final InterfaceC4148 mo4004(C0613 c0613, long j) throws IOException {
        m4367(c0613, j, true);
        return this;
    }

    @Override // defpackage.InterfaceC4148
    /* renamed from: ˏˋٴ */
    public final InterfaceC4148 mo4005(C0613 c0613, double d) throws IOException {
        m4365(c0613, d, true);
        return this;
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final void m4362(C0613 c0613, int i, boolean z) {
        if (z && i == 0) {
            return;
        }
        InterfaceC0513 interfaceC0513 = (InterfaceC0513) ((Annotation) c0613.f3081.get(InterfaceC0513.class));
        if (interfaceC0513 == null) {
            throw new EncodingException("Field has no @Protobuf config");
        }
        int iOrdinal = interfaceC0513.intEncoding().ordinal();
        if (iOrdinal == 0) {
            m4361(interfaceC0513.tag() << 3);
            m4361(i);
        } else if (iOrdinal == 1) {
            m4361(interfaceC0513.tag() << 3);
            m4361((i << 1) ^ (i >> 31));
        } else {
            if (iOrdinal != 2) {
                return;
            }
            m4361((interfaceC0513.tag() << 3) | 5);
            this.f5891.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(i).array());
        }
    }

    /* renamed from: ᵝᵵᵺ, reason: contains not printable characters */
    public final void m4363(C0613 c0613, Object obj, boolean z) {
        if (obj == null) {
            return;
        }
        if (obj instanceof CharSequence) {
            CharSequence charSequence = (CharSequence) obj;
            if (z && charSequence.length() == 0) {
                return;
            }
            m4361((m4360(c0613) << 3) | 2);
            byte[] bytes = charSequence.toString().getBytes(f5886);
            m4361(bytes.length);
            this.f5891.write(bytes);
            return;
        }
        if (obj instanceof Collection) {
            Iterator it = ((Collection) obj).iterator();
            while (it.hasNext()) {
                m4363(c0613, it.next(), false);
            }
            return;
        }
        if (obj instanceof Map) {
            Iterator it2 = ((Map) obj).entrySet().iterator();
            while (it2.hasNext()) {
                m4366(f5887, c0613, (Map.Entry) it2.next(), false);
            }
            return;
        }
        if (obj instanceof Double) {
            m4365(c0613, ((Double) obj).doubleValue(), z);
            return;
        }
        if (obj instanceof Float) {
            float fFloatValue = ((Float) obj).floatValue();
            if (z && fFloatValue == 0.0f) {
                return;
            }
            m4361((m4360(c0613) << 3) | 5);
            this.f5891.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(fFloatValue).array());
            return;
        }
        if (obj instanceof Number) {
            m4367(c0613, ((Number) obj).longValue(), z);
            return;
        }
        if (obj instanceof Boolean) {
            m4362(c0613, ((Boolean) obj).booleanValue() ? 1 : 0, z);
            return;
        }
        if (obj instanceof byte[]) {
            byte[] bArr = (byte[]) obj;
            if (z && bArr.length == 0) {
                return;
            }
            m4361((m4360(c0613) << 3) | 2);
            m4361(bArr.length);
            this.f5891.write(bArr);
            return;
        }
        InterfaceC3555 interfaceC3555 = (InterfaceC3555) this.f5892.get(obj.getClass());
        if (interfaceC3555 != null) {
            m4366(interfaceC3555, c0613, obj, z);
            return;
        }
        InterfaceC2807 interfaceC2807 = (InterfaceC2807) this.f5890.get(obj.getClass());
        if (interfaceC2807 != null) {
            C4410 c4410 = this.f5889;
            c4410.f16489 = false;
            c4410.f16488 = c0613;
            c4410.f16490 = z;
            interfaceC2807.mo1298(obj, c4410);
            return;
        }
        if (obj instanceof EnumC2677) {
            m4362(c0613, ((EnumC2677) obj).f10390, true);
        } else if (obj instanceof Enum) {
            m4362(c0613, ((Enum) obj).ordinal(), true);
        } else {
            m4366(this.f5893, c0613, obj, z);
        }
    }

    @Override // defpackage.InterfaceC4148
    /* renamed from: ᵥᵯˎ */
    public final InterfaceC4148 mo4008(C0613 c0613, Object obj) {
        m4363(c0613, obj, true);
        return this;
    }

    /* renamed from: ᵯٴᵦ, reason: contains not printable characters */
    public final void m4364(long j) throws IOException {
        while (true) {
            long j2 = (-128) & j;
            OutputStream outputStream = this.f5891;
            if (j2 == 0) {
                outputStream.write(((int) j) & 127);
                return;
            } else {
                outputStream.write((((int) j) & 127) | 128);
                j >>>= 7;
            }
        }
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final void m4365(C0613 c0613, double d, boolean z) throws IOException {
        if (z && d == 0.0d) {
            return;
        }
        m4361((m4360(c0613) << 3) | 1);
        this.f5891.write(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putDouble(d).array());
    }

    /* renamed from: ᵶᵷᵕ, reason: contains not printable characters */
    public final void m4366(InterfaceC3555 interfaceC3555, C0613 c0613, Object obj, boolean z) throws IOException {
        C5035 c5035 = new C5035();
        c5035.f18817 = 0L;
        try {
            OutputStream outputStream = this.f5891;
            this.f5891 = c5035;
            try {
                interfaceC3555.mo1298(obj, this);
                this.f5891 = outputStream;
                long j = c5035.f18817;
                c5035.close();
                if (z && j == 0) {
                    return;
                }
                m4361((m4360(c0613) << 3) | 2);
                m4364(j);
                interfaceC3555.mo1298(obj, this);
            } catch (Throwable th) {
                this.f5891 = outputStream;
                throw th;
            }
        } catch (Throwable th2) {
            try {
                c5035.close();
            } catch (Throwable th3) {
                th2.addSuppressed(th3);
            }
            throw th2;
        }
    }

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public final void m4367(C0613 c0613, long j, boolean z) throws IOException {
        if (z && j == 0) {
            return;
        }
        InterfaceC0513 interfaceC0513 = (InterfaceC0513) ((Annotation) c0613.f3081.get(InterfaceC0513.class));
        if (interfaceC0513 == null) {
            throw new EncodingException("Field has no @Protobuf config");
        }
        int iOrdinal = interfaceC0513.intEncoding().ordinal();
        if (iOrdinal == 0) {
            m4361(interfaceC0513.tag() << 3);
            m4364(j);
        } else if (iOrdinal == 1) {
            m4361(interfaceC0513.tag() << 3);
            m4364((j >> 63) ^ (j << 1));
        } else {
            if (iOrdinal != 2) {
                return;
            }
            m4361((interfaceC0513.tag() << 3) | 1);
            this.f5891.write(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(j).array());
        }
    }

    @Override // defpackage.InterfaceC4148
    /* renamed from: ᵻᵢﹳᵰ */
    public final InterfaceC4148 mo4012(C0613 c0613, boolean z) {
        m4362(c0613, z ? 1 : 0, true);
        return this;
    }
}
