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

import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/* JADX INFO: loaded from: classes2.dex */
public final class pr0 {

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

    /* JADX WARN: Removed duplicated region for block: B:24:0x0071  */
    /* JADX WARN: Removed duplicated region for block: B:29:0x00a1 A[Catch: all -> 0x004c, TryCatch #0 {all -> 0x004c, blocks: (B:3:0x0001, B:8:0x0019, B:10:0x0038, B:12:0x003e, B:14:0x0047, B:18:0x004f, B:25:0x0073, B:29:0x00a1, B:31:0x00ab, B:32:0x00b3, B:33:0x00ba, B:35:0x00c0, B:36:0x00c4, B:38:0x00d9, B:39:0x00e2, B:41:0x00e8, B:43:0x00f6, B:45:0x00fc, B:46:0x010e, B:48:0x0114, B:50:0x0126, B:52:0x012e, B:56:0x013f, B:58:0x0147, B:60:0x014d, B:62:0x0156, B:63:0x015f, B:26:0x0082, B:27:0x0091), top: B:70:0x0001 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final synchronized void a(int i10, long j10, long j11) {
        int iIntValue;
        ArrayDeque arrayDeque;
        Long l10;
        try {
            tn tnVar = ao.G8;
            n7.y yVar = n7.y.f31093e;
            if (((Boolean) yVar.f31096c.b(tnVar)).booleanValue()) {
                if (i10 == 2) {
                    nr0 nr0Var = new nr0();
                    nr0Var.f14709a = j10;
                    byte b10 = (byte) (nr0Var.f14711c | 1);
                    nr0Var.f14710b = 1;
                    nr0Var.f14711c = (byte) (b10 | 2);
                    or0 or0VarA = nr0Var.a();
                    ConcurrentHashMap concurrentHashMap = this.f15496a;
                    ArrayDeque arrayDeque2 = (ArrayDeque) concurrentHashMap.get(or0VarA);
                    if (arrayDeque2 != null && !arrayDeque2.isEmpty()) {
                        arrayDeque2.removeFirst();
                        if (arrayDeque2.isEmpty()) {
                            concurrentHashMap.remove(or0VarA);
                        }
                    }
                    i10 = 2;
                }
                nr0 nr0Var2 = new nr0();
                nr0Var2.f14709a = j10;
                byte b11 = (byte) (nr0Var2.f14711c | 1);
                nr0Var2.f14710b = i10;
                nr0Var2.f14711c = (byte) (b11 | 2);
                or0 or0VarA2 = nr0Var2.a();
                int i11 = or0VarA2.f15161b;
                if (i11 == 0) {
                    iIntValue = 0;
                    if (iIntValue > 0) {
                        ConcurrentHashMap concurrentHashMap2 = this.f15496a;
                        ArrayDeque arrayDeque3 = (ArrayDeque) concurrentHashMap2.get(or0VarA2);
                        if (arrayDeque3 == null) {
                            arrayDeque3 = new ArrayDeque();
                            concurrentHashMap2.put(or0VarA2, arrayDeque3);
                        }
                        arrayDeque3.addLast(Long.valueOf(j11));
                        while (arrayDeque3.size() > iIntValue) {
                            arrayDeque3.removeFirst();
                        }
                        b();
                        int iIntValue2 = ((Integer) n7.y.f31093e.f31096c.b(ao.P8)).intValue();
                        if (iIntValue2 <= 0) {
                            concurrentHashMap2.clear();
                            return;
                        }
                        while (true) {
                            Iterator it = concurrentHashMap2.values().iterator();
                            int size = 0;
                            while (it.hasNext()) {
                                size += ((ArrayDeque) it.next()).size();
                            }
                            if (size <= iIntValue2) {
                                break;
                            }
                            if (!concurrentHashMap2.isEmpty()) {
                                Long l11 = Long.MAX_VALUE;
                                Map.Entry entry = null;
                                for (Map.Entry entry2 : concurrentHashMap2.entrySet()) {
                                    ArrayDeque arrayDeque4 = (ArrayDeque) entry2.getValue();
                                    if (!arrayDeque4.isEmpty() && (l10 = (Long) arrayDeque4.peekFirst()) != null && l10.longValue() < l11.longValue()) {
                                        entry = entry2;
                                        l11 = l10;
                                    }
                                }
                                if (entry != null && (arrayDeque = (ArrayDeque) entry.getValue()) != null && !arrayDeque.isEmpty()) {
                                    arrayDeque.removeFirst();
                                    if (arrayDeque.isEmpty()) {
                                        concurrentHashMap2.remove(entry.getKey());
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (i11 == 1) {
                        iIntValue = ((Integer) yVar.f31096c.b(ao.M8)).intValue();
                    } else if (i11 == 2) {
                        iIntValue = ((Integer) yVar.f31096c.b(ao.N8)).intValue();
                    } else if (i11 == 3) {
                        iIntValue = ((Integer) yVar.f31096c.b(ao.O8)).intValue();
                    }
                    if (iIntValue > 0) {
                    }
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x003c  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void b() {
        long jLongValue;
        m7.n.C.f30472k.getClass();
        long jCurrentTimeMillis = System.currentTimeMillis();
        Iterator it = this.f15496a.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            or0 or0Var = (or0) entry.getKey();
            ArrayDeque arrayDeque = (ArrayDeque) entry.getValue();
            int i10 = or0Var.f15161b;
            if (i10 == 0) {
                jLongValue = 0;
            } else if (i10 == 1) {
                jLongValue = ((Long) n7.y.f31093e.f31096c.b(ao.J8)).longValue();
            } else if (i10 == 2) {
                jLongValue = ((Long) n7.y.f31093e.f31096c.b(ao.K8)).longValue();
            } else if (i10 == 3) {
                jLongValue = ((Long) n7.y.f31093e.f31096c.b(ao.L8)).longValue();
            }
            if (jLongValue == 0) {
                it.remove();
                jLongValue = 0;
            }
            if (jLongValue > 0) {
                Iterator it2 = arrayDeque.iterator();
                while (it2.hasNext() && jCurrentTimeMillis - ((Long) it2.next()).longValue() > jLongValue) {
                    it2.remove();
                }
                if (arrayDeque.isEmpty()) {
                    it.remove();
                }
            }
        }
    }
}
