package g.e.b.i0;

import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;
import kotlinx.serialization.json.internal.AbstractJsonLexerKt;

/* JADX INFO: loaded from: classes.dex */
public class c implements Closeable, Flushable {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Pattern f4194f = Pattern.compile("-?(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][-+]?[0-9]+)?");

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static final String[] f4195m = new String[128];

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public static final String[] f4196n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final Writer f4197o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public int[] f4198p = new int[32];

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public int f4199q = 0;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public String f4200r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public String f4201s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public boolean f4202t;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public boolean f4203u;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public String f4204v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public boolean f4205w;

    static {
        for (int i2 = 0; i2 <= 31; i2++) {
            f4195m[i2] = String.format("\\u%04x", Integer.valueOf(i2));
        }
        String[] strArr = f4195m;
        strArr[34] = "\\\"";
        strArr[92] = "\\\\";
        strArr[9] = "\\t";
        strArr[8] = "\\b";
        strArr[10] = "\\n";
        strArr[13] = "\\r";
        strArr[12] = "\\f";
        String[] strArr2 = (String[]) strArr.clone();
        f4196n = strArr2;
        strArr2[60] = "\\u003c";
        strArr2[62] = "\\u003e";
        strArr2[38] = "\\u0026";
        strArr2[61] = "\\u003d";
        strArr2[39] = "\\u0027";
    }

    public c(Writer writer) {
        q(6);
        this.f4201s = ":";
        this.f4205w = true;
        Objects.requireNonNull(writer, "out == null");
        this.f4197o = writer;
    }

    public c A(Number number) {
        if (number == null) {
            return m();
        }
        D();
        String string = number.toString();
        if (!string.equals("-Infinity") && !string.equals("Infinity") && !string.equals("NaN")) {
            Class<?> cls = number.getClass();
            if (!(cls == Integer.class || cls == Long.class || cls == Double.class || cls == Float.class || cls == Byte.class || cls == Short.class || cls == BigDecimal.class || cls == BigInteger.class || cls == AtomicInteger.class || cls == AtomicLong.class) && !f4194f.matcher(string).matches()) {
                throw new IllegalArgumentException("String created by " + cls + " is not a valid JSON number: " + string);
            }
        } else if (!this.f4202t) {
            throw new IllegalArgumentException(g.a.a.a.a.t("Numeric values must be finite, but was ", string));
        }
        a();
        this.f4197o.append((CharSequence) string);
        return this;
    }

    public c B(String str) {
        if (str == null) {
            return m();
        }
        D();
        a();
        u(str);
        return this;
    }

    public c C(boolean z2) {
        D();
        a();
        this.f4197o.write(z2 ? "true" : "false");
        return this;
    }

    public final void D() throws IOException {
        if (this.f4204v != null) {
            int iO = o();
            if (iO == 5) {
                this.f4197o.write(44);
            } else if (iO != 3) {
                throw new IllegalStateException("Nesting problem.");
            }
            l();
            s(4);
            u(this.f4204v);
            this.f4204v = null;
        }
    }

    public final void a() throws IOException {
        int iO = o();
        if (iO == 1) {
            s(2);
        } else {
            if (iO != 2) {
                if (iO == 4) {
                    this.f4197o.append((CharSequence) this.f4201s);
                    s(5);
                    return;
                }
                if (iO != 6) {
                    if (iO != 7) {
                        throw new IllegalStateException("Nesting problem.");
                    }
                    if (!this.f4202t) {
                        throw new IllegalStateException("JSON must have only one top-level value.");
                    }
                }
                s(7);
                return;
            }
            this.f4197o.append(',');
        }
        l();
    }

    public c c() {
        D();
        a();
        q(1);
        this.f4197o.write(91);
        return this;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.f4197o.close();
        int i2 = this.f4199q;
        if (i2 > 1 || (i2 == 1 && this.f4198p[i2 - 1] != 7)) {
            throw new IOException("Incomplete document");
        }
        this.f4199q = 0;
    }

    public c d() {
        D();
        a();
        q(3);
        this.f4197o.write(123);
        return this;
    }

    public final c e(int i2, int i3, char c2) throws IOException {
        int iO = o();
        if (iO != i3 && iO != i2) {
            throw new IllegalStateException("Nesting problem.");
        }
        if (this.f4204v != null) {
            StringBuilder sbF = g.a.a.a.a.F("Dangling name: ");
            sbF.append(this.f4204v);
            throw new IllegalStateException(sbF.toString());
        }
        this.f4199q--;
        if (iO == i3) {
            l();
        }
        this.f4197o.write(c2);
        return this;
    }

    public void flush() throws IOException {
        if (this.f4199q == 0) {
            throw new IllegalStateException("JsonWriter is closed.");
        }
        this.f4197o.flush();
    }

    public c g() {
        e(1, 2, AbstractJsonLexerKt.END_LIST);
        return this;
    }

    public c h() {
        e(3, 5, AbstractJsonLexerKt.END_OBJ);
        return this;
    }

    public c j(String str) {
        Objects.requireNonNull(str, "name == null");
        if (this.f4204v != null) {
            throw new IllegalStateException();
        }
        if (this.f4199q == 0) {
            throw new IllegalStateException("JsonWriter is closed.");
        }
        this.f4204v = str;
        return this;
    }

    public final void l() throws IOException {
        if (this.f4200r == null) {
            return;
        }
        this.f4197o.write(10);
        int i2 = this.f4199q;
        for (int i3 = 1; i3 < i2; i3++) {
            this.f4197o.write(this.f4200r);
        }
    }

    public c m() {
        if (this.f4204v != null) {
            if (!this.f4205w) {
                this.f4204v = null;
                return this;
            }
            D();
        }
        a();
        this.f4197o.write(AbstractJsonLexerKt.NULL);
        return this;
    }

    public final int o() {
        int i2 = this.f4199q;
        if (i2 != 0) {
            return this.f4198p[i2 - 1];
        }
        throw new IllegalStateException("JsonWriter is closed.");
    }

    public final void q(int i2) {
        int i3 = this.f4199q;
        int[] iArr = this.f4198p;
        if (i3 == iArr.length) {
            this.f4198p = Arrays.copyOf(iArr, i3 * 2);
        }
        int[] iArr2 = this.f4198p;
        int i4 = this.f4199q;
        this.f4199q = i4 + 1;
        iArr2[i4] = i2;
    }

    public final void s(int i2) {
        this.f4198p[this.f4199q - 1] = i2;
    }

    /* JADX WARN: Removed duplicated region for block: B:20:0x0034  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void u(java.lang.String r9) throws java.io.IOException {
        /*
            r8 = this;
            boolean r0 = r8.f4203u
            if (r0 == 0) goto L7
            java.lang.String[] r0 = g.e.b.i0.c.f4196n
            goto L9
        L7:
            java.lang.String[] r0 = g.e.b.i0.c.f4195m
        L9:
            java.io.Writer r1 = r8.f4197o
            r2 = 34
            r1.write(r2)
            int r1 = r9.length()
            r3 = 0
            r4 = r3
        L16:
            if (r3 >= r1) goto L45
            char r5 = r9.charAt(r3)
            r6 = 128(0x80, float:1.8E-43)
            if (r5 >= r6) goto L25
            r5 = r0[r5]
            if (r5 != 0) goto L32
            goto L42
        L25:
            r6 = 8232(0x2028, float:1.1535E-41)
            if (r5 != r6) goto L2c
            java.lang.String r5 = "\\u2028"
            goto L32
        L2c:
            r6 = 8233(0x2029, float:1.1537E-41)
            if (r5 != r6) goto L42
            java.lang.String r5 = "\\u2029"
        L32:
            if (r4 >= r3) goto L3b
            java.io.Writer r6 = r8.f4197o
            int r7 = r3 - r4
            r6.write(r9, r4, r7)
        L3b:
            java.io.Writer r4 = r8.f4197o
            r4.write(r5)
            int r4 = r3 + 1
        L42:
            int r3 = r3 + 1
            goto L16
        L45:
            if (r4 >= r1) goto L4d
            java.io.Writer r0 = r8.f4197o
            int r1 = r1 - r4
            r0.write(r9, r4, r1)
        L4d:
            java.io.Writer r9 = r8.f4197o
            r9.write(r2)
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: g.e.b.i0.c.u(java.lang.String):void");
    }

    public c x(long j2) {
        D();
        a();
        this.f4197o.write(Long.toString(j2));
        return this;
    }

    public c z(Boolean bool) {
        if (bool == null) {
            return m();
        }
        D();
        a();
        this.f4197o.write(bool.booleanValue() ? "true" : "false");
        return this;
    }
}
