package defpackage;

import android.content.Context;
import android.os.Build;
import j$.time.ZoneOffset;
import j$.time.format.DateTimeFormatter;
import j$.util.DateRetargetClass;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/* renamed from: ᵶᵸᵲ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C4839 {

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final C3139 f17971;

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public static final C4661 f17969 = new C4661("fire-global");

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public static final C4661 f17968 = new C4661("fire-count");

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public static final C4661 f17970 = new C4661("last-used-date");

    public C4839(Context context, String str) {
        this.f17971 = new C3139(context, "FirebaseHeartBeat".concat(str));
    }

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public final synchronized C4661 m10167(C1173 c1173, String str) {
        for (Map.Entry entry : c1173.m3685().entrySet()) {
            if (entry.getValue() instanceof Set) {
                Iterator it = ((Set) entry.getValue()).iterator();
                while (it.hasNext()) {
                    if (str.equals((String) it.next())) {
                        return new C4661(((C4661) entry.getKey()).f17262);
                    }
                }
            }
        }
        return null;
    }

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public final synchronized void m10168(long j, final String str) {
        final String strM10174 = m10174(j);
        final C4661 c4661 = new C4661(str);
        C3139 c3139 = this.f17971;
        InterfaceC0362 interfaceC0362 = new InterfaceC0362() { // from class: ᵦᵯᵬ
            @Override // defpackage.InterfaceC0362
            /* renamed from: ᵴᵳ */
            public final Object mo1588(Object obj) {
                C4839 c4839 = this.f13438;
                String str2 = strM10174;
                String str3 = str;
                C4661 c46612 = c4661;
                C1173 c1173 = (C1173) obj;
                c4839.getClass();
                C4661 c46613 = C4839.f17970;
                if (((String) AbstractC0494.m2151(c1173, c46613, "")).equals(str2)) {
                    C4661 c4661M10167 = c4839.m10167(c1173, str2);
                    if (c4661M10167 == null || c4661M10167.f17262.equals(str3)) {
                        return null;
                    }
                    synchronized (c4839) {
                        c4839.m10169(c1173, str2);
                        HashSet hashSet = new HashSet((Collection) AbstractC0494.m2151(c1173, c46612, new HashSet()));
                        hashSet.add(str2);
                        c1173.m3684(c46612, hashSet);
                    }
                    return null;
                }
                C4661 c46614 = C4839.f17968;
                long jLongValue = ((Long) AbstractC0494.m2151(c1173, c46614, 0L)).longValue();
                if (jLongValue + 1 == 30) {
                    jLongValue = c4839.m10171(c1173);
                }
                HashSet hashSet2 = new HashSet((Collection) AbstractC0494.m2151(c1173, c46612, new HashSet()));
                hashSet2.add(str2);
                c1173.m3684(c46612, hashSet2);
                c1173.m3684(c46614, Long.valueOf(jLongValue + 1));
                c1173.m3684(c46613, str2);
                return null;
            }
        };
        c3139.getClass();
    }

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public final synchronized void m10169(C1173 c1173, String str) {
        C4661 c4661M10167 = m10167(c1173, str);
        if (c4661M10167 == null) {
            return;
        }
        HashSet hashSet = new HashSet((Collection) AbstractC0494.m2151(c1173, c4661M10167, new HashSet()));
        hashSet.remove(str);
        if (hashSet.isEmpty()) {
            c1173.m3686();
            c1173.f4988.remove(c4661M10167);
        } else {
            c1173.m3684(c4661M10167, hashSet);
        }
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final synchronized ArrayList m10170() {
        ArrayList arrayList;
        try {
            arrayList = new ArrayList();
            String strM10174 = m10174(System.currentTimeMillis());
            C3139 c3139 = this.f17971;
            c3139.getClass();
            for (Map.Entry entry : ((Map) ᵮʛˀ.ˀᵗᵝᵭ(C4555.f16926, new ᵖᵨᵮ(c3139, (InterfaceC4943) null, 18))).entrySet()) {
                if (entry.getValue() instanceof Set) {
                    HashSet hashSet = new HashSet((Set) entry.getValue());
                    hashSet.remove(strM10174);
                    if (!hashSet.isEmpty()) {
                        arrayList.add(new C3005(((C4661) entry.getKey()).f17262, new ArrayList(hashSet)));
                    }
                }
            }
            m10173(System.currentTimeMillis());
        } catch (Throwable th) {
            throw th;
        }
        return arrayList;
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final synchronized long m10171(C1173 c1173) {
        long j;
        try {
            long jLongValue = ((Long) AbstractC0494.m2151(c1173, f17968, 0L)).longValue();
            String str = "";
            Set hashSet = new HashSet();
            String str2 = null;
            for (Map.Entry entry : c1173.m3685().entrySet()) {
                if (entry.getValue() instanceof Set) {
                    Set<String> set = (Set) entry.getValue();
                    for (String str3 : set) {
                        if (str2 == null || str2.compareTo(str3) > 0) {
                            str = ((C4661) entry.getKey()).f17262;
                            hashSet = set;
                            str2 = str3;
                        }
                    }
                }
            }
            HashSet hashSet2 = new HashSet(hashSet);
            hashSet2.remove(str2);
            c1173.m3684(new C4661(str), hashSet2);
            j = jLongValue - 1;
            c1173.m3684(f17968, Long.valueOf(j));
        } catch (Throwable th) {
            throw th;
        }
        return j;
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final synchronized void m10172() {
        C3139 c3139 = this.f17971;
        ᵡᵘˎʽ r1 = new ᵡᵘˎʽ(6, this);
        c3139.getClass();
    }

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public final synchronized void m10173(long j) {
        C3139 c3139 = this.f17971;
        C4829 c4829 = new C4829(j, 0);
        c3139.getClass();
    }

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public final synchronized String m10174(long j) {
        if (Build.VERSION.SDK_INT >= 26) {
            return DateRetargetClass.toInstant(new Date(j)).atOffset(ZoneOffset.UTC).toLocalDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE);
        }
        return new SimpleDateFormat("yyyy-MM-dd", Locale.UK).format(new Date(j));
    }
}
