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

import android.util.Base64;
import androidx.annotation.Nullable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;

/* JADX INFO: loaded from: classes2.dex */
public final class gk2 implements kk2 {

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final Random f11773g = new Random();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public jk2 f11777d;

    @Nullable
    private String zzh;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final tj f11774a = new tj();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final gj f11775b = new gj();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final HashMap f11776c = new HashMap();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public mk f11778e = mk.f14242a;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f11779f = -1;

    private final fk2 zzo(int i10, @Nullable cp2 cp2Var) {
        HashMap map = this.f11776c;
        long j10 = Long.MAX_VALUE;
        fk2 fk2Var = null;
        for (fk2 fk2Var2 : map.values()) {
            fk2Var2.zzc(i10, cp2Var);
            if (fk2Var2.zzb(i10, cp2Var)) {
                long j11 = fk2Var2.f11268c;
                if (j11 == -1 || j11 < j10) {
                    fk2Var = fk2Var2;
                    j10 = j11;
                } else if (j11 == j10) {
                    String str = bb1.f9474a;
                    if (fk2Var.f11269d != null && fk2Var2.f11269d != null) {
                        fk2Var = fk2Var2;
                    }
                }
            }
        }
        if (fk2Var != null) {
            return fk2Var;
        }
        byte[] bArr = new byte[12];
        f11773g.nextBytes(bArr);
        String strEncodeToString = Base64.encodeToString(bArr, 10);
        fk2 fk2Var3 = new fk2(this, strEncodeToString, i10, cp2Var);
        map.put(strEncodeToString, fk2Var3);
        return fk2Var3;
    }

    public final synchronized String a(mk mkVar, cp2 cp2Var) {
        return zzo(mkVar.n(cp2Var.f10025a, this.f11775b).f11762a, cp2Var).f11266a;
    }

    /* JADX WARN: Removed duplicated region for block: B:23:0x0040 A[Catch: all -> 0x0025, TryCatch #0 {all -> 0x0025, blocks: (B:3:0x0001, B:5:0x0006, B:8:0x0010, B:10:0x0014, B:12:0x001c, B:17:0x0028, B:19:0x0034, B:21:0x003a, B:23:0x0040, B:25:0x004a, B:28:0x0051, B:30:0x0057, B:32:0x006a, B:33:0x0088, B:35:0x008c, B:36:0x008e, B:38:0x0098, B:40:0x009c), top: B:47:0x0001 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final synchronized void b(nj2 nj2Var) {
        try {
            this.f11777d.getClass();
            mk mkVar = nj2Var.f14652b;
            if (!mkVar.g()) {
                cp2 cp2Var = nj2Var.zzd;
                if (cp2Var != null) {
                    long j10 = cp2Var.f10028d;
                    if (j10 == -1 || j10 >= h()) {
                        fk2 fk2Var = (fk2) this.f11776c.get(this.zzh);
                        if (fk2Var == null || fk2Var.f11268c != -1 || fk2Var.f11267b == nj2Var.f14653c) {
                            int i10 = nj2Var.f14653c;
                            fk2 fk2VarZzo = zzo(i10, cp2Var);
                            if (this.zzh == null) {
                                this.zzh = fk2VarZzo.f11266a;
                            }
                            if (cp2Var != null && cp2Var.b()) {
                                Object obj = cp2Var.f10025a;
                                long j11 = cp2Var.f10028d;
                                int i11 = cp2Var.f10026b;
                                fk2 fk2VarZzo2 = zzo(i10, new cp2(obj, j11, i11));
                                if (!fk2VarZzo2.f11270e) {
                                    fk2VarZzo2.f11270e = true;
                                    gj gjVar = this.f11775b;
                                    mkVar.n(obj, gjVar);
                                    gjVar.f11765d.a(i11).getClass();
                                    Math.max(0L, bb1.o(0L) + bb1.o(0L));
                                }
                            }
                            if (!fk2VarZzo.f11270e) {
                                fk2VarZzo.f11270e = true;
                            }
                            if (fk2VarZzo.f11266a.equals(this.zzh) && !fk2VarZzo.f11271f) {
                                fk2VarZzo.f11271f = true;
                                ((ik2) this.f11777d).g(nj2Var, fk2VarZzo.f11266a);
                            }
                        }
                    }
                }
            }
        } finally {
        }
    }

    public final synchronized void c(nj2 nj2Var) {
        try {
            this.f11777d.getClass();
            mk mkVar = this.f11778e;
            this.f11778e = nj2Var.f14652b;
            Iterator it = this.f11776c.values().iterator();
            while (it.hasNext()) {
                fk2 fk2Var = (fk2) it.next();
                if (!fk2Var.a(mkVar, this.f11778e) || fk2Var.b(nj2Var)) {
                    it.remove();
                    if (fk2Var.f11266a.equals(this.zzh)) {
                        g(fk2Var);
                    }
                    if (fk2Var.f11270e) {
                        ((ik2) this.f11777d).k(nj2Var, fk2Var.f11266a);
                    }
                }
            }
            f(nj2Var);
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void d(nj2 nj2Var, int i10) {
        try {
            this.f11777d.getClass();
            Iterator it = this.f11776c.values().iterator();
            while (it.hasNext()) {
                fk2 fk2Var = (fk2) it.next();
                if (fk2Var.b(nj2Var)) {
                    it.remove();
                    if (fk2Var.f11266a.equals(this.zzh)) {
                        g(fk2Var);
                    }
                    if (fk2Var.f11270e) {
                        ((ik2) this.f11777d).k(nj2Var, fk2Var.f11266a);
                    }
                }
            }
            f(nj2Var);
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void e(nj2 nj2Var) {
        jk2 jk2Var;
        try {
            String str = this.zzh;
            if (str != null) {
                fk2 fk2Var = (fk2) this.f11776c.get(str);
                fk2Var.getClass();
                g(fk2Var);
            }
            Iterator it = this.f11776c.values().iterator();
            while (it.hasNext()) {
                fk2 fk2Var2 = (fk2) it.next();
                it.remove();
                if (fk2Var2.f11270e && (jk2Var = this.f11777d) != null) {
                    ((ik2) jk2Var).k(nj2Var, fk2Var2.f11266a);
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final void f(nj2 nj2Var) {
        cp2 cp2Var;
        boolean zG = nj2Var.f14652b.g();
        HashMap map = this.f11776c;
        if (zG) {
            String str = this.zzh;
            if (str != null) {
                fk2 fk2Var = (fk2) map.get(str);
                fk2Var.getClass();
                g(fk2Var);
                return;
            }
            return;
        }
        fk2 fk2Var2 = (fk2) map.get(this.zzh);
        cp2 cp2Var2 = nj2Var.zzd;
        int i10 = nj2Var.f14653c;
        this.zzh = zzo(i10, cp2Var2).f11266a;
        b(nj2Var);
        if (cp2Var2 == null || !cp2Var2.b()) {
            return;
        }
        long j10 = cp2Var2.f10028d;
        if (fk2Var2 != null && fk2Var2.f11268c == j10 && (cp2Var = fk2Var2.f11269d) != null) {
            if (cp2Var.f10026b == cp2Var2.f10026b) {
                if (cp2Var.f10027c == cp2Var2.f10027c) {
                    return;
                }
            }
        }
        zzo(i10, new cp2(cp2Var2.f10025a, j10));
    }

    public final void g(fk2 fk2Var) {
        long j10 = fk2Var.f11268c;
        if (j10 != -1 && fk2Var.f11270e) {
            this.f11779f = j10;
        }
        this.zzh = null;
    }

    public final long h() {
        fk2 fk2Var = (fk2) this.f11776c.get(this.zzh);
        if (fk2Var != null) {
            long j10 = fk2Var.f11268c;
            if (j10 != -1) {
                return j10;
            }
        }
        return this.f11779f + 1;
    }

    @Override // com.google.android.gms.internal.ads.kk2
    @Nullable
    public final synchronized String zzf() {
        return this.zzh;
    }
}
