package o6;

import java.io.IOException;
import java.util.Arrays;
import x7.C1731g;

/* JADX INFO: renamed from: o6.p, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C1396p extends AbstractC1397q {

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

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final C1731g f15888w;

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

    static {
        for (int i = 0; i <= 31; i++) {
            f15887y[i] = String.format("\\u%04x", Integer.valueOf(i));
        }
        String[] strArr = f15887y;
        strArr[34] = "\\\"";
        strArr[92] = "\\\\";
        strArr[9] = "\\t";
        strArr[8] = "\\b";
        strArr[10] = "\\n";
        strArr[13] = "\\r";
        strArr[12] = "\\f";
    }

    public C1396p(C1731g c1731g) {
        int[] iArr = new int[32];
        this.f15891r = iArr;
        this.f15892s = new String[32];
        this.f15893t = new int[32];
        this.f15895v = -1;
        this.f15888w = c1731g;
        this.f15890q = 1;
        iArr[0] = 6;
    }

    /* JADX WARN: Removed duplicated region for block: B:16:0x002b  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static void Q(C1731g c1731g, String str) {
        String str2;
        String[] strArr = f15887y;
        c1731g.u0(34);
        int length = str.length();
        int i = 0;
        for (int i5 = 0; i5 < length; i5++) {
            char cCharAt = str.charAt(i5);
            if (cCharAt < 128) {
                str2 = strArr[cCharAt];
                if (str2 != null) {
                    if (i < i5) {
                        c1731g.y0(i, i5, str);
                    }
                    c1731g.z0(str2);
                    i = i5 + 1;
                }
            } else {
                if (cCharAt == 8232) {
                    str2 = "\\u2028";
                } else if (cCharAt == 8233) {
                    str2 = "\\u2029";
                }
                if (i < i5) {
                }
                c1731g.z0(str2);
                i = i5 + 1;
            }
        }
        if (i < length) {
            c1731g.y0(i, length, str);
        }
        c1731g.u0(34);
    }

    @Override // o6.AbstractC1397q
    public final C1396p A(String str) {
        if (str == null) {
            n();
            return this;
        }
        if (this.f15894u) {
            this.f15894u = false;
            m(str);
            return this;
        }
        V();
        H();
        Q(this.f15888w, str);
        int[] iArr = this.f15893t;
        int i = this.f15890q - 1;
        iArr[i] = iArr[i] + 1;
        return this;
    }

    public final void H() {
        int iR = r();
        int i = 2;
        if (iR != 1) {
            C1731g c1731g = this.f15888w;
            if (iR == 2) {
                c1731g.u0(44);
            } else if (iR == 4) {
                c1731g.z0(":");
                i = 5;
            } else {
                if (iR == 9) {
                    throw new IllegalStateException("Sink from valueSink() was not closed");
                }
                if (iR != 6) {
                    if (iR != 7) {
                        throw new IllegalStateException("Nesting problem.");
                    }
                    throw new IllegalStateException("JSON must have only one top-level value.");
                }
                i = 7;
            }
        }
        this.f15891r[this.f15890q - 1] = i;
    }

    public final void K(int i, int i5, char c3) {
        int iR = r();
        if (iR != i5 && iR != i) {
            throw new IllegalStateException("Nesting problem.");
        }
        if (this.f15889x != null) {
            throw new IllegalStateException("Dangling name: " + this.f15889x);
        }
        int i8 = this.f15890q;
        int i9 = ~this.f15895v;
        if (i8 == i9) {
            this.f15895v = i9;
            return;
        }
        int i10 = i8 - 1;
        this.f15890q = i10;
        this.f15892s[i10] = null;
        int[] iArr = this.f15893t;
        int i11 = i8 - 2;
        iArr[i11] = iArr[i11] + 1;
        this.f15888w.u0(c3);
    }

    public final void O(int i, int i5, char c3) {
        int i8;
        int i9 = this.f15890q;
        int i10 = this.f15895v;
        if (i9 == i10 && ((i8 = this.f15891r[i9 - 1]) == i || i8 == i5)) {
            this.f15895v = ~i10;
            return;
        }
        H();
        int i11 = this.f15890q;
        int[] iArr = this.f15891r;
        if (i11 == iArr.length) {
            if (i11 == 256) {
                throw new A6.b("Nesting too deep at " + j() + ": circular reference?");
            }
            this.f15891r = Arrays.copyOf(iArr, iArr.length * 2);
            String[] strArr = this.f15892s;
            this.f15892s = (String[]) Arrays.copyOf(strArr, strArr.length * 2);
            int[] iArr2 = this.f15893t;
            this.f15893t = Arrays.copyOf(iArr2, iArr2.length * 2);
        }
        int[] iArr3 = this.f15891r;
        int i12 = this.f15890q;
        this.f15890q = i12 + 1;
        iArr3[i12] = i;
        this.f15893t[i12] = 0;
        this.f15888w.u0(c3);
    }

    public final void V() {
        if (this.f15889x != null) {
            int iR = r();
            C1731g c1731g = this.f15888w;
            if (iR == 5) {
                c1731g.u0(44);
            } else if (iR != 3) {
                throw new IllegalStateException("Nesting problem.");
            }
            this.f15891r[this.f15890q - 1] = 4;
            Q(c1731g, this.f15889x);
            this.f15889x = null;
        }
    }

    @Override // o6.AbstractC1397q
    public final C1396p a() {
        if (this.f15894u) {
            throw new IllegalStateException("Array cannot be used as a map key in JSON at path " + j());
        }
        V();
        O(1, 2, '[');
        return this;
    }

    @Override // o6.AbstractC1397q
    public final C1396p b() {
        if (this.f15894u) {
            throw new IllegalStateException("Object cannot be used as a map key in JSON at path " + j());
        }
        V();
        O(3, 5, '{');
        return this;
    }

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

    @Override // java.io.Flushable
    public final void flush() {
        if (this.f15890q == 0) {
            throw new IllegalStateException("JsonWriter is closed.");
        }
    }

    @Override // o6.AbstractC1397q
    public final C1396p g() {
        this.f15894u = false;
        K(3, 5, '}');
        return this;
    }

    @Override // o6.AbstractC1397q
    public final C1396p m(String str) {
        if (str == null) {
            throw new NullPointerException("name == null");
        }
        if (this.f15890q == 0) {
            throw new IllegalStateException("JsonWriter is closed.");
        }
        int iR = r();
        if ((iR != 3 && iR != 5) || this.f15889x != null || this.f15894u) {
            throw new IllegalStateException("Nesting problem.");
        }
        this.f15889x = str;
        this.f15892s[this.f15890q - 1] = str;
        return this;
    }

    @Override // o6.AbstractC1397q
    public final C1396p n() {
        if (this.f15894u) {
            throw new IllegalStateException("null cannot be used as a map key in JSON at path " + j());
        }
        if (this.f15889x != null) {
            this.f15889x = null;
            return this;
        }
        H();
        this.f15888w.z0("null");
        int[] iArr = this.f15893t;
        int i = this.f15890q - 1;
        iArr[i] = iArr[i] + 1;
        return this;
    }

    @Override // o6.AbstractC1397q
    public final C1396p u(long j8) {
        if (this.f15894u) {
            this.f15894u = false;
            m(Long.toString(j8));
            return this;
        }
        V();
        H();
        this.f15888w.z0(Long.toString(j8));
        int[] iArr = this.f15893t;
        int i = this.f15890q - 1;
        iArr[i] = iArr[i] + 1;
        return this;
    }
}
