package com.google.android.gms.internal.ads;

import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/* JADX INFO: loaded from: classes2.dex */
public final class ps1 extends rb {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final ps1 f15526b = new ps1(8);

    public /* synthetic */ ps1() {
        super(5, 0);
    }

    public static void A(aa2 aa2Var, m92 m92Var) throws IOException {
        if (m92Var == null || (m92Var instanceof n92)) {
            aa2Var.e();
            return;
        }
        boolean z10 = m92Var instanceof p92;
        Writer writer = aa2Var.f8599a;
        if (z10) {
            p92 p92VarG = m92Var.g();
            Serializable serializable = p92VarG.f15315a;
            if (!(serializable instanceof Number)) {
                boolean z11 = serializable instanceof Boolean;
                if (z11) {
                    boolean zBooleanValue = z11 ? ((Boolean) serializable).booleanValue() : Boolean.parseBoolean(p92VarG.c());
                    aa2Var.m();
                    aa2Var.E();
                    writer.write(true != zBooleanValue ? "false" : "true");
                    return;
                }
                String strC = p92VarG.c();
                if (strC == null) {
                    aa2Var.e();
                    return;
                }
                aa2Var.m();
                aa2Var.E();
                aa2Var.p(strC);
                return;
            }
            Number numberH = p92VarG.h();
            if (numberH == null) {
                aa2Var.e();
                return;
            }
            aa2Var.m();
            String string = numberH.toString();
            Class<?> cls = numberH.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 (aa2Var.f8606h != zzhyq.f19851a) {
                        throw new IllegalArgumentException("Numeric values must be finite, but was ".concat(string));
                    }
                } else if (cls != Float.class && cls != Double.class && !aa2.f8597j.matcher(string).matches()) {
                    String strValueOf = String.valueOf(cls);
                    throw new IllegalArgumentException(com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.h(new StringBuilder(strValueOf.length() + 47 + string.length()), "String created by ", strValueOf, " is not a valid JSON number: ", string));
                }
            }
            aa2Var.E();
            writer.append((CharSequence) string);
            return;
        }
        boolean z12 = m92Var instanceof l92;
        if (z12) {
            aa2Var.m();
            aa2Var.E();
            int i10 = aa2Var.f8601c;
            int[] iArr = aa2Var.f8600b;
            if (i10 == iArr.length) {
                aa2Var.f8600b = Arrays.copyOf(iArr, i10 + i10);
            }
            int[] iArr2 = aa2Var.f8600b;
            int i11 = aa2Var.f8601c;
            aa2Var.f8601c = i11 + 1;
            iArr2[i11] = 1;
            writer.write(91);
            if (!z12) {
                throw new IllegalStateException("Not a JSON Array: ".concat(m92Var.toString()));
            }
            Iterator it = ((l92) m92Var).f13550a.iterator();
            while (it.hasNext()) {
                A(aa2Var, (m92) it.next());
            }
            aa2Var.g(1, 2, ']');
            return;
        }
        if (!(m92Var instanceof o92)) {
            throw new IllegalArgumentException("Couldn't write ".concat(String.valueOf(m92Var.getClass())));
        }
        aa2Var.m();
        aa2Var.E();
        int i12 = aa2Var.f8601c;
        int[] iArr3 = aa2Var.f8600b;
        if (i12 == iArr3.length) {
            aa2Var.f8600b = Arrays.copyOf(iArr3, i12 + i12);
        }
        int[] iArr4 = aa2Var.f8600b;
        int i13 = aa2Var.f8601c;
        aa2Var.f8601c = i13 + 1;
        iArr4[i13] = 3;
        writer.write(123);
        r92 r92Var = new r92((s92) m92Var.f().f14965a.entrySet());
        while (r92Var.hasNext()) {
            Map.Entry entry = (Map.Entry) r92Var.next();
            String str = (String) entry.getKey();
            Objects.requireNonNull(str, "name == null");
            if (aa2Var.f8607i != null) {
                throw new IllegalStateException("Already wrote a name, expecting a value.");
            }
            int iK = aa2Var.k();
            if (iK != 3 && iK != 5) {
                throw new IllegalStateException("Please begin an object before writing a name.");
            }
            aa2Var.f8607i = str;
            A(aa2Var, (m92) entry.getValue());
        }
        aa2Var.g(3, 5, '}');
    }

    @Override // com.google.android.gms.internal.ads.rb
    public Map j() {
        return new zzgtm(0);
    }

    @Override // com.google.android.gms.internal.ads.rb
    public void l(fu1 fu1Var, Thread thread) {
        fu1Var.f11392a = thread;
    }

    @Override // com.google.android.gms.internal.ads.rb
    public void m(ou1 ou1Var, Set set) {
        synchronized (ou1Var) {
            try {
                if (ou1Var.f15171h == null) {
                    ou1Var.f15171h = set;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.rb
    public int o(ou1 ou1Var) {
        int i10;
        synchronized (ou1Var) {
            i10 = ou1Var.f15172i - 1;
            ou1Var.f15172i = i10;
        }
        return i10;
    }

    @Override // com.google.android.gms.internal.ads.rb
    public void s(fu1 fu1Var, fu1 fu1Var2) {
        fu1Var.f11393b = fu1Var2;
    }

    @Override // com.google.android.gms.internal.ads.rb
    public boolean u(gu1 gu1Var, fu1 fu1Var, fu1 fu1Var2) {
        synchronized (gu1Var) {
            try {
                if (gu1Var.f11889c != fu1Var) {
                    return false;
                }
                gu1Var.f11889c = fu1Var2;
                return true;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.rb
    public boolean v(gu1 gu1Var, zt1 zt1Var, zt1 zt1Var2) {
        synchronized (gu1Var) {
            try {
                if (gu1Var.f11888b != zt1Var) {
                    return false;
                }
                gu1Var.f11888b = zt1Var2;
                return true;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // com.google.android.gms.internal.ads.rb
    public fu1 w(cu1 cu1Var) {
        fu1 fu1Var;
        fu1 fu1Var2 = fu1.f11391c;
        synchronized (cu1Var) {
            try {
                fu1Var = cu1Var.f11889c;
                if (fu1Var != fu1Var2) {
                    cu1Var.f11889c = fu1Var2;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return fu1Var;
    }

    @Override // com.google.android.gms.internal.ads.rb
    public zt1 x(cu1 cu1Var) {
        zt1 zt1Var;
        zt1 zt1Var2 = zt1.f19392d;
        synchronized (cu1Var) {
            try {
                zt1Var = cu1Var.f11888b;
                if (zt1Var != zt1Var2) {
                    cu1Var.f11888b = zt1Var2;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return zt1Var;
    }

    @Override // com.google.android.gms.internal.ads.rb
    public boolean y(gu1 gu1Var, Object obj, Object obj2) {
        synchronized (gu1Var) {
            try {
                if (gu1Var.f11887a != obj) {
                    return false;
                }
                gu1Var.f11887a = obj2;
                return true;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public /* bridge */ /* synthetic */ void z(aa2 aa2Var, Object obj) throws IOException {
        A(aa2Var, (m92) obj);
    }

    public /* synthetic */ ps1(int i10) {
        super(8);
    }
}
