package com.google.ads.interactivemedia.v3.internal;

import defpackage.e;
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;

/* JADX INFO: loaded from: classes2.dex */
public class zzace implements Closeable, Flushable {
    private static final Pattern zza = Pattern.compile("-?(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][-+]?[0-9]+)?");
    private static final String[] zzb = new String[128];
    private static final String[] zzc;
    private final Writer zzd;
    private int[] zze = new int[32];
    private int zzf = 0;
    private zzvl zzg;
    private String zzh;
    private String zzi;
    private boolean zzj;
    private zzwg zzk;
    private boolean zzl;
    private String zzm;
    private boolean zzn;

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

    public zzace(Writer writer) {
        zzz(6);
        this.zzk = zzwg.LEGACY_STRICT;
        this.zzn = true;
        Objects.requireNonNull(writer, "out == null");
        this.zzd = writer;
        zzo(zzvl.zza);
    }

    private final void zzA(int i10) {
        this.zze[this.zzf - 1] = i10;
    }

    /* JADX WARN: Removed duplicated region for block: B:20:0x0036  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private final void zzB(String str) throws IOException {
        String str2;
        String[] strArr = this.zzl ? zzc : zzb;
        this.zzd.write(34);
        int length = str.length();
        int i10 = 0;
        int i11 = 0;
        while (i10 < length) {
            int i12 = i10 + 1;
            char cCharAt = str.charAt(i10);
            if (cCharAt < 128) {
                str2 = strArr[cCharAt];
                if (str2 != null) {
                    if (i11 < i10) {
                        this.zzd.write(str, i11, i10 - i11);
                    }
                    this.zzd.write(str2);
                    i11 = i12;
                }
            } else {
                if (cCharAt == 8232) {
                    str2 = "\\u2028";
                } else if (cCharAt == 8233) {
                    str2 = "\\u2029";
                }
                if (i11 < i10) {
                }
                this.zzd.write(str2);
                i11 = i12;
            }
            i10 = i12;
        }
        if (i11 < length) {
            this.zzd.write(str, i11, length - i11);
        }
        this.zzd.write(34);
    }

    private final void zzC() throws IOException {
        if (this.zzm != null) {
            int iZza = zza();
            if (iZza == 5) {
                this.zzd.write(this.zzi);
            } else if (iZza != 3) {
                throw new IllegalStateException("Nesting problem.");
            }
            zzy();
            zzA(4);
            zzB(this.zzm);
            this.zzm = null;
        }
    }

    private final int zza() {
        int i10 = this.zzf;
        if (i10 != 0) {
            return this.zze[i10 - 1];
        }
        throw new IllegalStateException("JsonWriter is closed.");
    }

    private final zzace zzv(int i10, int i11, char c6) throws IOException {
        int iZza = zza();
        if (iZza != i11 && iZza != i10) {
            throw new IllegalStateException("Nesting problem.");
        }
        String str = this.zzm;
        if (str != null) {
            throw new IllegalStateException("Dangling name: ".concat(str));
        }
        this.zzf--;
        if (iZza == i11) {
            zzy();
        }
        this.zzd.write(c6);
        return this;
    }

    private final zzace zzw(int i10, char c6) throws IOException {
        zzx();
        zzz(i10);
        this.zzd.write(c6);
        return this;
    }

    private final void zzx() throws IOException {
        int iZza = zza();
        if (iZza == 1) {
            zzA(2);
            zzy();
            return;
        }
        if (iZza == 2) {
            this.zzd.append((CharSequence) this.zzi);
            zzy();
        } else {
            if (iZza == 4) {
                this.zzd.append((CharSequence) this.zzh);
                zzA(5);
                return;
            }
            if (iZza != 6) {
                if (iZza != 7) {
                    throw new IllegalStateException("Nesting problem.");
                }
                if (this.zzk != zzwg.LENIENT) {
                    throw new IllegalStateException("JSON must have only one top-level value.");
                }
            }
            zzA(7);
        }
    }

    private final void zzy() throws IOException {
        if (this.zzj) {
            return;
        }
        this.zzd.write(this.zzg.zzb());
        int i10 = this.zzf;
        for (int i11 = 1; i11 < i10; i11++) {
            this.zzd.write(this.zzg.zza());
        }
    }

    private final void zzz(int i10) {
        int i11 = this.zzf;
        int[] iArr = this.zze;
        if (i11 == iArr.length) {
            this.zze = Arrays.copyOf(iArr, i11 + i11);
        }
        int[] iArr2 = this.zze;
        int i12 = this.zzf;
        this.zzf = i12 + 1;
        iArr2[i12] = i10;
    }

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

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

    public zzace zzb() throws IOException {
        zzC();
        zzw(1, '[');
        return this;
    }

    public zzace zzc() throws IOException {
        zzC();
        zzw(3, '{');
        return this;
    }

    public zzace zzd() throws IOException {
        zzv(1, 2, ']');
        return this;
    }

    public zzace zze() throws IOException {
        zzv(3, 5, '}');
        return this;
    }

    public zzace zzf(String str) throws IOException {
        Objects.requireNonNull(str, "name == null");
        if (this.zzm != null) {
            throw new IllegalStateException("Already wrote a name, expecting a value.");
        }
        int iZza = zza();
        if (iZza != 3 && iZza != 5) {
            throw new IllegalStateException("Please begin an object before writing a name.");
        }
        this.zzm = str;
        return this;
    }

    public zzace zzg() throws IOException {
        if (this.zzm != null) {
            if (!this.zzn) {
                this.zzm = null;
                return this;
            }
            zzC();
        }
        zzx();
        this.zzd.write("null");
        return this;
    }

    public zzace zzh(double d10) throws IOException {
        zzC();
        if (this.zzk == zzwg.LENIENT || !(Double.isNaN(d10) || Double.isInfinite(d10))) {
            zzx();
            this.zzd.append((CharSequence) Double.toString(d10));
            return this;
        }
        throw new IllegalArgumentException("Numeric values must be finite, but was " + d10);
    }

    public zzace zzi(long j10) throws IOException {
        zzC();
        zzx();
        this.zzd.write(Long.toString(j10));
        return this;
    }

    public zzace zzj(Boolean bool) throws IOException {
        if (bool == null) {
            zzg();
            return this;
        }
        zzC();
        zzx();
        this.zzd.write(true != bool.booleanValue() ? "false" : "true");
        return this;
    }

    public zzace zzk(Number number) throws IOException {
        if (number == null) {
            zzg();
            return this;
        }
        zzC();
        String string = number.toString();
        Class<?> cls = number.getClass();
        if (cls != Integer.class && cls != Long.class && cls != Byte.class && cls != Short.class && cls != BigDecimal.class && cls != BigInteger.class && cls != AtomicInteger.class && cls != AtomicLong.class) {
            if (string.equals("-Infinity") || string.equals("Infinity") || string.equals("NaN")) {
                if (this.zzk != zzwg.LENIENT) {
                    throw new IllegalArgumentException("Numeric values must be finite, but was ".concat(string));
                }
            } else if (cls != Float.class && cls != Double.class && !zza.matcher(string).matches()) {
                throw new IllegalArgumentException(e.k("String created by ", String.valueOf(cls), " is not a valid JSON number: ", string));
            }
        }
        zzx();
        this.zzd.append((CharSequence) string);
        return this;
    }

    public zzace zzl(String str) throws IOException {
        if (str == null) {
            zzg();
            return this;
        }
        zzC();
        zzx();
        zzB(str);
        return this;
    }

    public zzace zzm(boolean z10) throws IOException {
        zzC();
        zzx();
        this.zzd.write(true != z10 ? "false" : "true");
        return this;
    }

    public final zzwg zzn() {
        return this.zzk;
    }

    public final void zzo(zzvl zzvlVar) {
        Objects.requireNonNull(zzvlVar);
        this.zzg = zzvlVar;
        this.zzi = ",";
        if (zzvlVar.zzc()) {
            this.zzh = ": ";
            if (this.zzg.zzb().isEmpty()) {
                this.zzi = ", ";
            }
        } else {
            this.zzh = ":";
        }
        boolean z10 = false;
        if (this.zzg.zzb().isEmpty() && this.zzg.zza().isEmpty()) {
            z10 = true;
        }
        this.zzj = z10;
    }

    public final void zzp(boolean z10) {
        this.zzl = z10;
    }

    public final void zzq(boolean z10) {
        this.zzn = z10;
    }

    public final void zzr(zzwg zzwgVar) {
        Objects.requireNonNull(zzwgVar);
        this.zzk = zzwgVar;
    }

    public final boolean zzs() {
        return this.zzn;
    }

    public final boolean zzt() {
        return this.zzl;
    }

    public final boolean zzu() {
        return this.zzk == zzwg.LENIENT;
    }
}
