package sg;

import com.adobe.marketing.mobile.Event;
import com.adobe.marketing.mobile.ExtensionApi;
import com.adobe.marketing.mobile.LoggingMode;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import kotlin.jvm.internal.k;
import kotlin.jvm.internal.t;
import rm.c0;
import sm.p0;
import sm.q0;
import vg.v;
import vg.x;

/* loaded from: classes4.dex */
public final class d {

    /* renamed from: d, reason: collision with root package name */
    public static final a f35677d = new a(null);

    /* renamed from: a, reason: collision with root package name */
    private final String f35678a;

    /* renamed from: b, reason: collision with root package name */
    private Map f35679b;

    /* renamed from: c, reason: collision with root package name */
    private final ExtensionApi f35680c;

    /* loaded from: classes4.dex */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(k kVar) {
            this();
        }
    }

    public d(ExtensionApi extensionApi) {
        t.g(extensionApi, "extensionApi");
        this.f35680c = extensionApi;
        this.f35678a = "LaunchRulesConsequence";
        this.f35679b = new LinkedHashMap();
    }

    private final Event a(h hVar, Event event) {
        Map e10;
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("detail", hVar.a());
        linkedHashMap.put("id", hVar.b());
        linkedHashMap.put("type", hVar.c());
        Event.Builder builder = new Event.Builder("Rules Consequence Event", "com.adobe.eventType.rulesEngine", "com.adobe.eventSource.responseContent");
        e10 = p0.e(c0.a("triggeredconsequence", linkedHashMap));
        Event a10 = builder.d(e10).b(event).a();
        t.f(a10, "Event.Builder(\n         …ent)\n            .build()");
        return a10;
    }

    private final Map c(h hVar, Map map) {
        Map e10;
        e10 = e.e(hVar);
        Map a10 = ch.d.a(e10);
        if (a10 == null) {
            wg.t.b("LaunchRulesEngine", this.f35678a, "Unable to process an AttachDataConsequence Event, 'eventData' is missing from 'details'", new Object[0]);
            return null;
        }
        if (map == null) {
            wg.t.b("LaunchRulesEngine", this.f35678a, "Unable to process an AttachDataConsequence Event, 'eventData' is missing from original event", new Object[0]);
            return null;
        }
        if (wg.t.c() == LoggingMode.VERBOSE) {
            wg.t.e("LaunchRulesEngine", this.f35678a, "Attaching event data with " + com.adobe.marketing.mobile.internal.util.e.f(a10), new Object[0]);
        }
        return com.adobe.marketing.mobile.internal.util.b.e(a10, map, false);
    }

    private final Event d(h hVar, Event event) {
        String h10;
        String g10;
        String f10;
        Map e10;
        Map map;
        h10 = e.h(hVar);
        LinkedHashMap linkedHashMap = null;
        if (h10 == null) {
            wg.t.b("LaunchRulesEngine", this.f35678a, "Unable to process a DispatchConsequence Event, 'type' is missing from 'details'", new Object[0]);
            return null;
        }
        g10 = e.g(hVar);
        if (g10 == null) {
            wg.t.b("LaunchRulesEngine", this.f35678a, "Unable to process a DispatchConsequence Event, 'source' is missing from 'details'", new Object[0]);
            return null;
        }
        f10 = e.f(hVar);
        if (f10 == null) {
            wg.t.b("LaunchRulesEngine", this.f35678a, "Unable to process a DispatchConsequence Event, 'eventdataaction' is missing from 'details'", new Object[0]);
            return null;
        }
        int hashCode = f10.hashCode();
        if (hashCode != 108960) {
            if (hashCode == 3059573 && f10.equals("copy")) {
                map = event.o();
                return new Event.Builder("Dispatch Consequence Result", h10, g10).d(map).b(event).a();
            }
            wg.t.b("LaunchRulesEngine", this.f35678a, "Unable to process a DispatchConsequence Event, unsupported 'eventdataaction', expected values copy/new", new Object[0]);
            return null;
        }
        if (f10.equals("new")) {
            e10 = e.e(hVar);
            Map a10 = ch.d.a(e10);
            if (a10 != null) {
                linkedHashMap = new LinkedHashMap();
                for (Map.Entry entry : a10.entrySet()) {
                    if (entry.getValue() != null) {
                        linkedHashMap.put(entry.getKey(), entry.getValue());
                    }
                }
            }
            map = linkedHashMap;
            return new Event.Builder("Dispatch Consequence Result", h10, g10).d(map).b(event).a();
        }
        wg.t.b("LaunchRulesEngine", this.f35678a, "Unable to process a DispatchConsequence Event, unsupported 'eventdataaction', expected values copy/new", new Object[0]);
        return null;
    }

    private final Map e(h hVar, Map map) {
        Map e10;
        e10 = e.e(hVar);
        Map a10 = ch.d.a(e10);
        if (a10 == null) {
            wg.t.b("LaunchRulesEngine", this.f35678a, "Unable to process a ModifyDataConsequence Event, 'eventData' is missing from 'details'", new Object[0]);
            return null;
        }
        if (map == null) {
            wg.t.b("LaunchRulesEngine", this.f35678a, "Unable to process a ModifyDataConsequence Event, 'eventData' is missing from original event", new Object[0]);
            return null;
        }
        if (wg.t.c() == LoggingMode.VERBOSE) {
            wg.t.e("LaunchRulesEngine", this.f35678a, "Modifying event data with " + com.adobe.marketing.mobile.internal.util.e.f(a10), new Object[0]);
        }
        return com.adobe.marketing.mobile.internal.util.b.e(a10, map, true);
    }

    private final String f(String str, x xVar) {
        String a10 = new v(str, new vg.d("{%", "%}")).a(xVar, c.f35671a.c());
        t.f(a10, "template.render(tokenFin…mer.createTransforming())");
        return a10;
    }

    private final Map g(Map map, x xVar) {
        Map w10;
        if (map == null || map.isEmpty()) {
            return null;
        }
        w10 = q0.w(map);
        for (Map.Entry entry : map.entrySet()) {
            String str = (String) entry.getKey();
            Object value = entry.getValue();
            if (value instanceof String) {
                w10.put(str, f((String) value, xVar));
            } else if (value instanceof Map) {
                w10.put(str, g(ch.d.a((Map) value), xVar));
            }
        }
        return w10;
    }

    private final h h(h hVar, x xVar) {
        return new h(hVar.b(), hVar.c(), g(hVar.a(), xVar));
    }

    public final Event b(Event event, List matchedRules) {
        t.g(event, "event");
        t.g(matchedRules, "matchedRules");
        Integer num = (Integer) this.f35679b.remove(event.x());
        int intValue = num != null ? num.intValue() : 0;
        g gVar = new g(event, this.f35680c);
        Iterator it = matchedRules.iterator();
        Event event2 = event;
        while (it.hasNext()) {
            Iterator it2 = ((b) it.next()).b().iterator();
            while (it2.hasNext()) {
                h h10 = h((h) it2.next(), gVar);
                String c10 = h10.c();
                int hashCode = c10.hashCode();
                if (hashCode != 96417) {
                    if (hashCode != 108290) {
                        if (hashCode == 284771450 && c10.equals("dispatch")) {
                            if (intValue >= 1) {
                                wg.t.f("LaunchRulesEngine", this.f35678a, "Unable to process dispatch consequence, max chained dispatch consequences limit of 1met for this event uuid " + event.x(), new Object[0]);
                            } else {
                                Event d10 = d(h10, event2);
                                if (d10 != null) {
                                    wg.t.e("LaunchRulesEngine", this.f35678a, "processDispatchConsequence - Dispatching event - " + d10.x(), new Object[0]);
                                    this.f35680c.c(d10);
                                    Map map = this.f35679b;
                                    String x10 = d10.x();
                                    t.f(x10, "dispatchEvent.uniqueIdentifier");
                                    map.put(x10, Integer.valueOf(intValue + 1));
                                }
                            }
                        }
                        Event a10 = a(h10, event2);
                        wg.t.e("LaunchRulesEngine", this.f35678a, "evaluateRulesConsequence - Dispatching consequence event " + a10.x(), new Object[0]);
                        this.f35680c.c(a10);
                    } else if (c10.equals("mod")) {
                        Map e10 = e(h10, event2.o());
                        if (e10 != null) {
                            event2 = event2.n(e10);
                            t.f(event2, "processedEvent.cloneWith…ntData(modifiedEventData)");
                        }
                    } else {
                        Event a102 = a(h10, event2);
                        wg.t.e("LaunchRulesEngine", this.f35678a, "evaluateRulesConsequence - Dispatching consequence event " + a102.x(), new Object[0]);
                        this.f35680c.c(a102);
                    }
                } else if (c10.equals("add")) {
                    Map c11 = c(h10, event2.o());
                    if (c11 != null) {
                        event2 = event2.n(c11);
                        t.f(event2, "processedEvent.cloneWith…ntData(attachedEventData)");
                    }
                } else {
                    Event a1022 = a(h10, event2);
                    wg.t.e("LaunchRulesEngine", this.f35678a, "evaluateRulesConsequence - Dispatching consequence event " + a1022.x(), new Object[0]);
                    this.f35680c.c(a1022);
                }
            }
        }
        return event2;
    }
}
