package X3;

import C3.J;
import C3.W;
import P2.w;
import a4.C0398a;
import a4.C0400c;
import a4.InterfaceC0399b;
import android.os.Bundle;
import b4.AbstractC0538a;
import com.google.android.gms.internal.measurement.C0662l0;
import com.google.android.gms.internal.measurement.X;
import com.google.android.gms.internal.measurement.Y;
import d3.G0;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.ParseException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public final class c {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final D4.b f7225a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public Integer f7226b = null;

    public c(D4.b bVar) {
        this.f7225a = bVar;
    }

    public static boolean a(ArrayList arrayList, b bVar) {
        String str = bVar.f7219a;
        int size = arrayList.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayList.get(i);
            i++;
            b bVar2 = (b) obj;
            if (bVar2.f7219a.equals(str) && bVar2.f7220b.equals(bVar.f7220b)) {
                return true;
            }
        }
        return false;
    }

    public final ArrayList b() {
        C0400c c0400c = (C0400c) ((InterfaceC0399b) this.f7225a.get());
        c0400c.getClass();
        ArrayList arrayList = new ArrayList();
        for (Bundle bundle : ((C0662l0) c0400c.f7612a.f5246r).e("frc", "")) {
            J j8 = AbstractC0538a.f9191a;
            w.f(bundle);
            C0398a c0398a = new C0398a();
            String str = (String) G0.a(bundle, "origin", String.class, null);
            w.f(str);
            c0398a.f7597a = str;
            String str2 = (String) G0.a(bundle, "name", String.class, null);
            w.f(str2);
            c0398a.f7598b = str2;
            c0398a.f7599c = G0.a(bundle, "value", Object.class, null);
            c0398a.f7600d = (String) G0.a(bundle, "trigger_event_name", String.class, null);
            c0398a.f7601e = ((Long) G0.a(bundle, "trigger_timeout", Long.class, 0L)).longValue();
            c0398a.f7602f = (String) G0.a(bundle, "timed_out_event_name", String.class, null);
            c0398a.f7603g = (Bundle) G0.a(bundle, "timed_out_event_params", Bundle.class, null);
            c0398a.f7604h = (String) G0.a(bundle, "triggered_event_name", String.class, null);
            c0398a.i = (Bundle) G0.a(bundle, "triggered_event_params", Bundle.class, null);
            c0398a.f7605j = ((Long) G0.a(bundle, "time_to_live", Long.class, 0L)).longValue();
            c0398a.f7606k = (String) G0.a(bundle, "expired_event_name", String.class, null);
            c0398a.f7607l = (Bundle) G0.a(bundle, "expired_event_params", Bundle.class, null);
            c0398a.f7609n = ((Boolean) G0.a(bundle, "active", Boolean.class, Boolean.FALSE)).booleanValue();
            c0398a.f7608m = ((Long) G0.a(bundle, "creation_timestamp", Long.class, 0L)).longValue();
            c0398a.f7610o = ((Long) G0.a(bundle, "triggered_timestamp", Long.class, 0L)).longValue();
            arrayList.add(c0398a);
        }
        return arrayList;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:122:0x02ff  */
    /* JADX WARN: Removed duplicated region for block: B:142:0x034a  */
    /* JADX WARN: Removed duplicated region for block: B:145:0x0353  */
    /* JADX WARN: Removed duplicated region for block: B:148:0x035c  */
    /* JADX WARN: Removed duplicated region for block: B:151:0x0363  */
    /* JADX WARN: Removed duplicated region for block: B:154:0x0373  */
    /* JADX WARN: Removed duplicated region for block: B:157:0x037c  */
    /* JADX WARN: Removed duplicated region for block: B:160:0x0385  */
    /* JADX WARN: Removed duplicated region for block: B:163:0x038e  */
    /* JADX WARN: Removed duplicated region for block: B:166:0x039e  */
    /* JADX WARN: Removed duplicated region for block: B:169:0x03a7  */
    /* JADX WARN: Removed duplicated region for block: B:95:0x029a  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c(ArrayList arrayList) throws a {
        ObjectInputStream objectInputStream;
        ObjectOutputStream objectOutputStream;
        String str;
        String str2;
        String str3;
        String str4;
        String str5;
        Object obj;
        String str6;
        String str7;
        Bundle bundle;
        String str8;
        Bundle bundle2;
        String str9;
        Bundle bundle3;
        int i = 0;
        int i5 = 1;
        D4.b bVar = this.f7225a;
        if (bVar.get() == null) {
            throw new a("The Analytics SDK is not available. Please check that the Analytics SDK is included in your app dependencies.");
        }
        ArrayList arrayList2 = new ArrayList();
        Iterator it = arrayList.iterator();
        while (true) {
            if (!it.hasNext()) {
                String str10 = null;
                if (arrayList2.isEmpty()) {
                    if (bVar.get() == null) {
                        throw new a("The Analytics SDK is not available. Please check that the Analytics SDK is included in your app dependencies.");
                    }
                    ArrayList arrayListB = b();
                    int size = arrayListB.size();
                    while (i < size) {
                        Object obj2 = arrayListB.get(i);
                        i++;
                        String str11 = ((C0398a) obj2).f7598b;
                        C0662l0 c0662l0 = (C0662l0) ((C0400c) ((InterfaceC0399b) bVar.get())).f7612a.f5246r;
                        c0662l0.getClass();
                        c0662l0.b(new Y(c0662l0, str11, null, null, 0));
                    }
                    return;
                }
                if (bVar.get() == null) {
                    throw new a("The Analytics SDK is not available. Please check that the Analytics SDK is included in your app dependencies.");
                }
                ArrayList arrayListB2 = b();
                ArrayList arrayList3 = new ArrayList();
                int size2 = arrayListB2.size();
                int i8 = 0;
                while (i8 < size2) {
                    Object obj3 = arrayListB2.get(i8);
                    i8 += i5;
                    C0398a c0398a = (C0398a) obj3;
                    String[] strArr = b.f7217g;
                    String str12 = c0398a.f7600d;
                    arrayList3.add(new b(c0398a.f7598b, String.valueOf(c0398a.f7599c), str12 != null ? str12 : "", new Date(c0398a.f7608m), c0398a.f7601e, c0398a.f7605j));
                    str10 = str10;
                    bVar = bVar;
                    i5 = i5;
                }
                D4.b bVar2 = bVar;
                String str13 = str10;
                ArrayList arrayList4 = new ArrayList();
                int size3 = arrayList3.size();
                int i9 = 0;
                while (i9 < size3) {
                    Object obj4 = arrayList3.get(i9);
                    i9++;
                    b bVar3 = (b) obj4;
                    if (!a(arrayList2, bVar3)) {
                        arrayList4.add(bVar3.a());
                    }
                }
                int size4 = arrayList4.size();
                int i10 = 0;
                while (i10 < size4) {
                    Object obj5 = arrayList4.get(i10);
                    i10++;
                    String str14 = ((C0398a) obj5).f7598b;
                    C0662l0 c0662l02 = (C0662l0) ((C0400c) ((InterfaceC0399b) bVar2.get())).f7612a.f5246r;
                    c0662l02.getClass();
                    String str15 = str13;
                    c0662l02.b(new Y(c0662l02, str14, str15, str13, 0));
                    str13 = str15;
                }
                String str16 = str13;
                ArrayList arrayList5 = new ArrayList();
                int size5 = arrayList2.size();
                int i11 = 0;
                while (i11 < size5) {
                    Object obj6 = arrayList2.get(i11);
                    i11++;
                    b bVar4 = (b) obj6;
                    if (!a(arrayList3, bVar4)) {
                        arrayList5.add(bVar4);
                    }
                }
                ArrayDeque arrayDeque = new ArrayDeque(b());
                if (this.f7226b == null) {
                    this.f7226b = Integer.valueOf(((C0662l0) ((C0400c) ((InterfaceC0399b) bVar2.get())).f7612a.f5246r).c("frc"));
                }
                int iIntValue = this.f7226b.intValue();
                int size6 = arrayList5.size();
                int i12 = 0;
                while (i12 < size6) {
                    int i13 = i12 + 1;
                    b bVar5 = (b) arrayList5.get(i12);
                    while (arrayDeque.size() >= iIntValue) {
                        String str17 = ((C0398a) arrayDeque.pollFirst()).f7598b;
                        C0662l0 c0662l03 = (C0662l0) ((C0400c) ((InterfaceC0399b) bVar2.get())).f7612a.f5246r;
                        c0662l03.getClass();
                        c0662l03.b(new Y(c0662l03, str17, str16, str16, 0));
                    }
                    C0398a c0398aA = bVar5.a();
                    C0400c c0400c = (C0400c) ((InterfaceC0399b) bVar2.get());
                    c0400c.getClass();
                    J j8 = AbstractC0538a.f9191a;
                    String str18 = c0398aA.f7597a;
                    if (str18 != null && !str18.isEmpty()) {
                        Object obj7 = c0398aA.f7599c;
                        if (obj7 != null) {
                            try {
                                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                                ObjectOutputStream objectOutputStream2 = new ObjectOutputStream(byteArrayOutputStream);
                                try {
                                    objectOutputStream2.writeObject(obj7);
                                    objectOutputStream2.flush();
                                    ObjectInputStream objectInputStream2 = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
                                    try {
                                        Object object = objectInputStream2.readObject();
                                        try {
                                            objectOutputStream2.close();
                                            objectInputStream2.close();
                                        } catch (IOException | ClassNotFoundException unused) {
                                            object = str16;
                                        }
                                        if (object != null) {
                                            if (!AbstractC0538a.f9193c.contains(str18)) {
                                                String str19 = c0398aA.f7598b;
                                                if ("_ce1".equals(str19) || "_ce2".equals(str19)) {
                                                    if (str18.equals("fcm") || str18.equals("frc")) {
                                                        str = c0398aA.f7606k;
                                                        if ((str != null || (AbstractC0538a.b(str, c0398aA.f7607l) && AbstractC0538a.a(str18, c0398aA.f7606k, c0398aA.f7607l))) && (((str2 = c0398aA.f7604h) == null || (AbstractC0538a.b(str2, c0398aA.i) && AbstractC0538a.a(str18, c0398aA.f7604h, c0398aA.i))) && ((str3 = c0398aA.f7602f) == null || (AbstractC0538a.b(str3, c0398aA.f7603g) && AbstractC0538a.a(str18, c0398aA.f7602f, c0398aA.f7603g))))) {
                                                            Bundle bundle4 = new Bundle();
                                                            str4 = c0398aA.f7597a;
                                                            if (str4 != null) {
                                                                bundle4.putString("origin", str4);
                                                            }
                                                            str5 = c0398aA.f7598b;
                                                            if (str5 != null) {
                                                                bundle4.putString("name", str5);
                                                            }
                                                            obj = c0398aA.f7599c;
                                                            if (obj != null) {
                                                                G0.e(bundle4, obj);
                                                            }
                                                            str6 = c0398aA.f7600d;
                                                            if (str6 != null) {
                                                                bundle4.putString("trigger_event_name", str6);
                                                            }
                                                            bundle4.putLong("trigger_timeout", c0398aA.f7601e);
                                                            str7 = c0398aA.f7602f;
                                                            if (str7 != null) {
                                                                bundle4.putString("timed_out_event_name", str7);
                                                            }
                                                            bundle = c0398aA.f7603g;
                                                            if (bundle != null) {
                                                                bundle4.putBundle("timed_out_event_params", bundle);
                                                            }
                                                            str8 = c0398aA.f7604h;
                                                            if (str8 != null) {
                                                                bundle4.putString("triggered_event_name", str8);
                                                            }
                                                            bundle2 = c0398aA.i;
                                                            if (bundle2 != null) {
                                                                bundle4.putBundle("triggered_event_params", bundle2);
                                                            }
                                                            bundle4.putLong("time_to_live", c0398aA.f7605j);
                                                            str9 = c0398aA.f7606k;
                                                            if (str9 != null) {
                                                                bundle4.putString("expired_event_name", str9);
                                                            }
                                                            bundle3 = c0398aA.f7607l;
                                                            if (bundle3 != null) {
                                                                bundle4.putBundle("expired_event_params", bundle3);
                                                            }
                                                            bundle4.putLong("creation_timestamp", c0398aA.f7608m);
                                                            bundle4.putBoolean("active", c0398aA.f7609n);
                                                            bundle4.putLong("triggered_timestamp", c0398aA.f7610o);
                                                            C0662l0 c0662l04 = (C0662l0) c0400c.f7612a.f5246r;
                                                            c0662l04.getClass();
                                                            c0662l04.b(new X(c0662l04, bundle4, i));
                                                        }
                                                    }
                                                } else if ("_ln".equals(str19)) {
                                                    if (str18.equals("fcm") || str18.equals("fiam")) {
                                                    }
                                                } else if (!AbstractC0538a.f9195e.contains(str19)) {
                                                    W w4 = AbstractC0538a.f9196f;
                                                    int i14 = w4.f1227t;
                                                    int i15 = 0;
                                                    while (i15 < i14) {
                                                        boolean zMatches = str19.matches((String) w4.get(i15));
                                                        i15++;
                                                        if (zMatches) {
                                                            break;
                                                        }
                                                    }
                                                    str = c0398aA.f7606k;
                                                    if (str != null) {
                                                        Bundle bundle42 = new Bundle();
                                                        str4 = c0398aA.f7597a;
                                                        if (str4 != null) {
                                                        }
                                                        str5 = c0398aA.f7598b;
                                                        if (str5 != null) {
                                                        }
                                                        obj = c0398aA.f7599c;
                                                        if (obj != null) {
                                                        }
                                                        str6 = c0398aA.f7600d;
                                                        if (str6 != null) {
                                                        }
                                                        bundle42.putLong("trigger_timeout", c0398aA.f7601e);
                                                        str7 = c0398aA.f7602f;
                                                        if (str7 != null) {
                                                        }
                                                        bundle = c0398aA.f7603g;
                                                        if (bundle != null) {
                                                        }
                                                        str8 = c0398aA.f7604h;
                                                        if (str8 != null) {
                                                        }
                                                        bundle2 = c0398aA.i;
                                                        if (bundle2 != null) {
                                                        }
                                                        bundle42.putLong("time_to_live", c0398aA.f7605j);
                                                        str9 = c0398aA.f7606k;
                                                        if (str9 != null) {
                                                        }
                                                        bundle3 = c0398aA.f7607l;
                                                        if (bundle3 != null) {
                                                        }
                                                        bundle42.putLong("creation_timestamp", c0398aA.f7608m);
                                                        bundle42.putBoolean("active", c0398aA.f7609n);
                                                        bundle42.putLong("triggered_timestamp", c0398aA.f7610o);
                                                        C0662l0 c0662l042 = (C0662l0) c0400c.f7612a.f5246r;
                                                        c0662l042.getClass();
                                                        c0662l042.b(new X(c0662l042, bundle42, i));
                                                    } else {
                                                        Bundle bundle422 = new Bundle();
                                                        str4 = c0398aA.f7597a;
                                                        if (str4 != null) {
                                                        }
                                                        str5 = c0398aA.f7598b;
                                                        if (str5 != null) {
                                                        }
                                                        obj = c0398aA.f7599c;
                                                        if (obj != null) {
                                                        }
                                                        str6 = c0398aA.f7600d;
                                                        if (str6 != null) {
                                                        }
                                                        bundle422.putLong("trigger_timeout", c0398aA.f7601e);
                                                        str7 = c0398aA.f7602f;
                                                        if (str7 != null) {
                                                        }
                                                        bundle = c0398aA.f7603g;
                                                        if (bundle != null) {
                                                        }
                                                        str8 = c0398aA.f7604h;
                                                        if (str8 != null) {
                                                        }
                                                        bundle2 = c0398aA.i;
                                                        if (bundle2 != null) {
                                                        }
                                                        bundle422.putLong("time_to_live", c0398aA.f7605j);
                                                        str9 = c0398aA.f7606k;
                                                        if (str9 != null) {
                                                        }
                                                        bundle3 = c0398aA.f7607l;
                                                        if (bundle3 != null) {
                                                        }
                                                        bundle422.putLong("creation_timestamp", c0398aA.f7608m);
                                                        bundle422.putBoolean("active", c0398aA.f7609n);
                                                        bundle422.putLong("triggered_timestamp", c0398aA.f7610o);
                                                        C0662l0 c0662l0422 = (C0662l0) c0400c.f7612a.f5246r;
                                                        c0662l0422.getClass();
                                                        c0662l0422.b(new X(c0662l0422, bundle422, i));
                                                    }
                                                }
                                            }
                                        }
                                    } catch (Throwable th) {
                                        th = th;
                                        objectOutputStream = objectOutputStream2;
                                        objectInputStream = objectInputStream2;
                                        if (objectOutputStream != 0) {
                                            objectOutputStream.close();
                                        }
                                        if (objectInputStream != 0) {
                                            objectInputStream.close();
                                        }
                                        throw th;
                                    }
                                } catch (Throwable th2) {
                                    th = th2;
                                    objectInputStream = str16;
                                    objectOutputStream = objectOutputStream2;
                                }
                            } catch (Throwable th3) {
                                th = th3;
                                String str20 = str16;
                                objectInputStream = str20;
                                objectOutputStream = str20;
                            }
                        }
                    }
                    arrayDeque.offer(c0398aA);
                    i12 = i13;
                    str16 = null;
                }
                return;
            }
            Map map = (Map) it.next();
            String[] strArr2 = b.f7217g;
            ArrayList arrayList6 = new ArrayList();
            String[] strArr3 = b.f7217g;
            for (int i16 = 0; i16 < 5; i16++) {
                String str21 = strArr3[i16];
                if (!map.containsKey(str21)) {
                    arrayList6.add(str21);
                }
            }
            if (!arrayList6.isEmpty()) {
                throw new a(String.format("The following keys are missing from the experiment info map: %s", arrayList6));
            }
            try {
                arrayList2.add(new b((String) map.get("experimentId"), (String) map.get("variantId"), map.containsKey("triggerEvent") ? (String) map.get("triggerEvent") : "", b.f7218h.parse((String) map.get("experimentStartTime")), Long.parseLong((String) map.get("triggerTimeoutMillis")), Long.parseLong((String) map.get("timeToLiveMillis"))));
            } catch (NumberFormatException e8) {
                throw new a("Could not process experiment: one of the durations could not be converted into a long.", e8);
            } catch (ParseException e9) {
                throw new a("Could not process experiment: parsing experiment start time failed.", e9);
            }
        }
    }
}
