package com.google.android.gms.ads.nonagon.signalgeneration;

import android.text.TextUtils;
import android.util.Pair;
import androidx.annotation.Nullable;
import com.google.android.gms.internal.ads.ao;
import com.google.android.gms.internal.ads.b30;
import com.google.android.gms.internal.ads.gr0;
import com.google.android.gms.internal.ads.tn;
import com.google.android.gms.internal.ads.yn;
import com.google.android.gms.internal.ads.zq0;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONException;
import org.json.JSONObject;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final int f8124a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final long f8125b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final boolean f8126c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final boolean f8127d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Map f8128e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final ArrayDeque f8129f = new ArrayDeque();

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final gr0 f8131h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public ConcurrentHashMap f8132i;

    public r0(gr0 gr0Var) {
        this.f8131h = gr0Var;
        tn tnVar = ao.G7;
        n7.y yVar = n7.y.f31093e;
        this.f8124a = ((Integer) yVar.f31096c.b(tnVar)).intValue();
        tn tnVar2 = ao.H7;
        yn ynVar = yVar.f31096c;
        this.f8125b = ((Long) ynVar.b(tnVar2)).longValue();
        this.f8126c = ((Boolean) ynVar.b(ao.L7)).booleanValue();
        this.f8127d = ((Boolean) ynVar.b(ao.K7)).booleanValue();
        this.f8128e = Collections.synchronizedMap(new zzs(this));
    }

    public final synchronized void a(String str, String str2, zq0 zq0Var) {
        m7.n.C.f30472k.getClass();
        this.f8128e.put(str, new q0(Long.valueOf(System.currentTimeMillis()), str2, new HashSet()));
        b();
        c(zq0Var);
    }

    public final synchronized void b() {
        m7.n.C.f30472k.getClass();
        long jCurrentTimeMillis = System.currentTimeMillis();
        try {
            Iterator it = this.f8128e.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                if (jCurrentTimeMillis - ((q0) entry.getValue()).f8120a.longValue() <= this.f8125b) {
                    break;
                }
                this.f8130g.add(new Pair((String) entry.getKey(), ((q0) entry.getValue()).f8121b));
                it.remove();
            }
        } catch (ConcurrentModificationException e10) {
            m7.n.C.f30469h.d("QueryJsonMap.removeExpiredEntries", e10);
        }
    }

    public final synchronized void c(zq0 zq0Var) {
        if (this.f8126c) {
            ArrayDeque arrayDeque = this.f8130g;
            ArrayDeque arrayDequeClone = arrayDeque.clone();
            arrayDeque.clear();
            ArrayDeque arrayDeque2 = this.f8129f;
            ArrayDeque arrayDequeClone2 = arrayDeque2.clone();
            arrayDeque2.clear();
            b30.f9359a.execute(new k.h(this, zq0Var, arrayDequeClone, arrayDequeClone2, 6, 0));
        }
    }

    public final void d(zq0 zq0Var, ArrayDeque arrayDeque, String str) {
        Pair pair;
        while (!arrayDeque.isEmpty()) {
            Pair pair2 = (Pair) arrayDeque.poll();
            ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap(zq0Var.f19364a);
            this.f8132i = concurrentHashMap;
            concurrentHashMap.put("action", "ev");
            this.f8132i.put("e_r", str);
            this.f8132i.put("e_id", (String) pair2.first);
            if (this.f8127d) {
                try {
                    JSONObject jSONObject = new JSONObject((String) pair2.second);
                    pair = new Pair(b.zzb(jSONObject.getJSONObject("extras").getString("query_info_type")), jSONObject.getString("request_agent"));
                } catch (JSONException unused) {
                    pair = new Pair("", "");
                }
                ConcurrentHashMap concurrentHashMap2 = this.f8132i;
                String str2 = (String) pair.first;
                if (!TextUtils.isEmpty(str2)) {
                    concurrentHashMap2.put("e_type", str2);
                }
                ConcurrentHashMap concurrentHashMap3 = this.f8132i;
                String str3 = (String) pair.second;
                if (!TextUtils.isEmpty(str3)) {
                    concurrentHashMap3.put("e_agent", str3);
                }
            }
            this.f8131h.a(false, this.f8132i);
        }
    }

    @Nullable
    public final synchronized String zzb(String str, zq0 zq0Var) {
        q0 q0Var = (q0) this.f8128e.get(str);
        zq0Var.f19364a.put("request_id", str);
        if (q0Var == null) {
            zq0Var.f19364a.put("mhit", "false");
            return null;
        }
        zq0Var.f19364a.put("mhit", "true");
        return q0Var.f8121b;
    }
}
