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;

/* JADX INFO: renamed from: ᵪˑˑ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C3891 implements InterfaceC4364 {

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public static final C3961 f14850;

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public static final C3961 f14851;

    /* JADX INFO: renamed from: ᵻʽᵯ, reason: contains not printable characters */
    public static final C2582 f14852;

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public static final Charset f14853 = Charset.forName("UTF-8");

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final HashMap f14854;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final HashMap f14855;

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final C2297 f14856 = new C2297(this);

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public OutputStream f14857;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final InterfaceC2474 f14858;

    static {
        C1066 c1066 = new C1066(1);
        HashMap map = new HashMap();
        map.put(InterfaceC2189.class, c1066);
        f14851 = new C3961("key", DesugarCollections.unmodifiableMap(new HashMap(map)));
        C1066 c10662 = new C1066(2);
        HashMap map2 = new HashMap();
        map2.put(InterfaceC2189.class, c10662);
        f14850 = new C3961("value", DesugarCollections.unmodifiableMap(new HashMap(map2)));
        f14852 = new C2582(1);
    }

    public C3891(ByteArrayOutputStream byteArrayOutputStream, HashMap map, HashMap map2, InterfaceC2474 interfaceC2474) {
        this.f14857 = byteArrayOutputStream;
        this.f14854 = map;
        this.f14855 = map2;
        this.f14858 = interfaceC2474;
    }

    /* JADX INFO: renamed from: ᵶᵥᵝ, reason: contains not printable characters */
    public static int m8602(C3961 c3961) {
        InterfaceC2189 interfaceC2189 = (InterfaceC2189) ((Annotation) c3961.f15129.get(InterfaceC2189.class));
        if (interfaceC2189 != null) {
            return interfaceC2189.tag();
        }
        throw new EncodingException("Field has no @Protobuf config");
    }

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public final void m8603(C3961 c3961, double d, boolean z) throws IOException {
        if (z && d == 0.0d) {
            return;
        }
        m8608((m8602(c3961) << 3) | 1);
        this.f14857.write(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putDouble(d).array());
    }

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public final void m8604(C3961 c3961, long j, boolean z) throws IOException {
        if (z && j == 0) {
            return;
        }
        InterfaceC2189 interfaceC2189 = (InterfaceC2189) ((Annotation) c3961.f15129.get(InterfaceC2189.class));
        if (interfaceC2189 == null) {
            throw new EncodingException("Field has no @Protobuf config");
        }
        int iOrdinal = interfaceC2189.intEncoding().ordinal();
        if (iOrdinal == 0) {
            m8608(interfaceC2189.tag() << 3);
            m8606(j);
        } else if (iOrdinal == 1) {
            m8608(interfaceC2189.tag() << 3);
            m8606((j >> 63) ^ (j << 1));
        } else {
            if (iOrdinal != 2) {
                return;
            }
            m8608((interfaceC2189.tag() << 3) | 1);
            this.f14857.write(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(j).array());
        }
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final void m8605(C3961 c3961, int i, boolean z) {
        if (z && i == 0) {
            return;
        }
        InterfaceC2189 interfaceC2189 = (InterfaceC2189) ((Annotation) c3961.f15129.get(InterfaceC2189.class));
        if (interfaceC2189 == null) {
            throw new EncodingException("Field has no @Protobuf config");
        }
        int iOrdinal = interfaceC2189.intEncoding().ordinal();
        if (iOrdinal == 0) {
            m8608(interfaceC2189.tag() << 3);
            m8608(i);
        } else if (iOrdinal == 1) {
            m8608(interfaceC2189.tag() << 3);
            m8608((i << 1) ^ (i >> 31));
        } else {
            if (iOrdinal != 2) {
                return;
            }
            m8608((interfaceC2189.tag() << 3) | 5);
            this.f14857.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(i).array());
        }
    }

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

    @Override // defpackage.InterfaceC4364
    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public final InterfaceC4364 mo8607(C3961 c3961, int i) {
        m8605(c3961, i, true);
        return this;
    }

    /* JADX INFO: renamed from: ᵮᵣᵗˈ, reason: contains not printable characters */
    public final void m8608(int i) throws IOException {
        while (true) {
            long j = i & (-128);
            OutputStream outputStream = this.f14857;
            if (j == 0) {
                outputStream.write(i & 127);
                return;
            } else {
                outputStream.write((i & 127) | 128);
                i >>>= 7;
            }
        }
    }

    /* JADX INFO: renamed from: ᵱᵽʡᵚ, reason: contains not printable characters */
    public final void m8609(InterfaceC2474 interfaceC2474, C3961 c3961, Object obj, boolean z) throws IOException {
        C2225 c2225 = new C2225();
        c2225.f8831 = 0L;
        try {
            OutputStream outputStream = this.f14857;
            this.f14857 = c2225;
            try {
                interfaceC2474.mo1073(obj, this);
                this.f14857 = outputStream;
                long j = c2225.f8831;
                c2225.close();
                if (z && j == 0) {
                    return;
                }
                m8608((m8602(c3961) << 3) | 2);
                m8606(j);
                interfaceC2474.mo1073(obj, this);
            } catch (Throwable th) {
                this.f14857 = outputStream;
                throw th;
            }
        } catch (Throwable th2) {
            try {
                c2225.close();
            } catch (Throwable th3) {
                th2.addSuppressed(th3);
            }
            throw th2;
        }
    }

    @Override // defpackage.InterfaceC4364
    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final InterfaceC4364 mo8610(C3961 c3961, Object obj) {
        m8613(c3961, obj, true);
        return this;
    }

    @Override // defpackage.InterfaceC4364
    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public final InterfaceC4364 mo8611(C3961 c3961, long j) throws IOException {
        m8604(c3961, j, true);
        return this;
    }

    @Override // defpackage.InterfaceC4364
    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public final InterfaceC4364 mo8612(C3961 c3961, boolean z) {
        m8605(c3961, z ? 1 : 0, true);
        return this;
    }

    /* JADX INFO: renamed from: ᵻʽᵯ, reason: contains not printable characters */
    public final void m8613(C3961 c3961, Object obj, boolean z) {
        if (obj == null) {
            return;
        }
        if (obj instanceof CharSequence) {
            CharSequence charSequence = (CharSequence) obj;
            if (z && charSequence.length() == 0) {
                return;
            }
            m8608((m8602(c3961) << 3) | 2);
            byte[] bytes = charSequence.toString().getBytes(f14853);
            m8608(bytes.length);
            this.f14857.write(bytes);
            return;
        }
        if (obj instanceof Collection) {
            Iterator it = ((Collection) obj).iterator();
            while (it.hasNext()) {
                m8613(c3961, it.next(), false);
            }
            return;
        }
        if (obj instanceof Map) {
            Iterator it2 = ((Map) obj).entrySet().iterator();
            while (it2.hasNext()) {
                m8609(f14852, c3961, (Map.Entry) it2.next(), false);
            }
            return;
        }
        if (obj instanceof Double) {
            m8603(c3961, ((Double) obj).doubleValue(), z);
            return;
        }
        if (obj instanceof Float) {
            float fFloatValue = ((Float) obj).floatValue();
            if (z && fFloatValue == 0.0f) {
                return;
            }
            m8608((m8602(c3961) << 3) | 5);
            this.f14857.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(fFloatValue).array());
            return;
        }
        if (obj instanceof Number) {
            m8604(c3961, ((Number) obj).longValue(), z);
            return;
        }
        if (obj instanceof Boolean) {
            m8605(c3961, ((Boolean) obj).booleanValue() ? 1 : 0, z);
            return;
        }
        if (obj instanceof byte[]) {
            byte[] bArr = (byte[]) obj;
            if (z && bArr.length == 0) {
                return;
            }
            m8608((m8602(c3961) << 3) | 2);
            m8608(bArr.length);
            this.f14857.write(bArr);
            return;
        }
        InterfaceC2474 interfaceC2474 = (InterfaceC2474) this.f14854.get(obj.getClass());
        if (interfaceC2474 != null) {
            m8609(interfaceC2474, c3961, obj, z);
            return;
        }
        InterfaceC4429 interfaceC4429 = (InterfaceC4429) this.f14855.get(obj.getClass());
        if (interfaceC4429 != null) {
            C2297 c2297 = this.f14856;
            c2297.f9018 = false;
            c2297.f9017 = c3961;
            c2297.f9016 = z;
            interfaceC4429.mo1073(obj, c2297);
            return;
        }
        if (obj instanceof EnumC1197) {
            m8605(c3961, ((EnumC1197) obj).f5100, true);
        } else if (obj instanceof Enum) {
            m8605(c3961, ((Enum) obj).ordinal(), true);
        } else {
            m8609(this.f14858, c3961, obj, z);
        }
    }

    @Override // defpackage.InterfaceC4364
    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public final InterfaceC4364 mo8614(C3961 c3961, double d) throws IOException {
        m8603(c3961, d, true);
        return this;
    }
}
