package c.b.a.u.y;

import c.b.a.f;
import c.b.a.i;
import c.b.a.k;
import c.b.a.l;
import c.b.a.n;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public final class b extends c.b.a.w.c {
    public static final Writer l = new a();
    public static final n m = new n("closed");
    public final List<i> n;
    public String o;
    public i p;

    public class a extends Writer {
        @Override // java.io.Writer, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            throw new AssertionError();
        }

        @Override // java.io.Writer, java.io.Flushable
        public void flush() {
            throw new AssertionError();
        }

        @Override // java.io.Writer
        public void write(char[] cArr, int i, int i2) {
            throw new AssertionError();
        }
    }

    public b() {
        super(l);
        this.n = new ArrayList();
        this.p = k.f1326a;
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c b() {
        f fVar = new f();
        v(fVar);
        this.n.add(fVar);
        return this;
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c c() {
        l lVar = new l();
        v(lVar);
        this.n.add(lVar);
        return this;
    }

    @Override // c.b.a.w.c, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        if (!this.n.isEmpty()) {
            throw new IOException("Incomplete document");
        }
        this.n.add(m);
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c e() {
        if (this.n.isEmpty() || this.o != null) {
            throw new IllegalStateException();
        }
        if (!(u() instanceof f)) {
            throw new IllegalStateException();
        }
        this.n.remove(r0.size() - 1);
        return this;
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c f() {
        if (this.n.isEmpty() || this.o != null) {
            throw new IllegalStateException();
        }
        if (!(u() instanceof l)) {
            throw new IllegalStateException();
        }
        this.n.remove(r0.size() - 1);
        return this;
    }

    @Override // c.b.a.w.c, java.io.Flushable
    public void flush() {
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c g(String str) {
        Objects.requireNonNull(str, "name == null");
        if (this.n.isEmpty() || this.o != null) {
            throw new IllegalStateException();
        }
        if (!(u() instanceof l)) {
            throw new IllegalStateException();
        }
        this.o = str;
        return this;
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c i() {
        v(k.f1326a);
        return this;
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c n(long j) {
        v(new n(Long.valueOf(j)));
        return this;
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c o(Boolean bool) {
        if (bool == null) {
            v(k.f1326a);
            return this;
        }
        v(new n(bool));
        return this;
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c p(Number number) {
        if (number == null) {
            v(k.f1326a);
            return this;
        }
        if (!this.h) {
            double dDoubleValue = number.doubleValue();
            if (Double.isNaN(dDoubleValue) || Double.isInfinite(dDoubleValue)) {
                throw new IllegalArgumentException("JSON forbids NaN and infinities: " + number);
            }
        }
        v(new n(number));
        return this;
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c q(String str) {
        if (str == null) {
            v(k.f1326a);
            return this;
        }
        v(new n(str));
        return this;
    }

    @Override // c.b.a.w.c
    public c.b.a.w.c r(boolean z) {
        v(new n(Boolean.valueOf(z)));
        return this;
    }

    public i t() {
        if (this.n.isEmpty()) {
            return this.p;
        }
        StringBuilder sbD = c.a.a.a.a.d("Expected one JSON element but was ");
        sbD.append(this.n);
        throw new IllegalStateException(sbD.toString());
    }

    public final i u() {
        return this.n.get(r1.size() - 1);
    }

    public final void v(i iVar) {
        if (this.o != null) {
            if (!(iVar instanceof k) || this.k) {
                l lVar = (l) u();
                lVar.f1327a.put(this.o, iVar);
            }
            this.o = null;
            return;
        }
        if (this.n.isEmpty()) {
            this.p = iVar;
            return;
        }
        i iVarU = u();
        if (!(iVarU instanceof f)) {
            throw new IllegalStateException();
        }
        ((f) iVarU).f1325a.add(iVar);
    }
}
