package ng;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import kotlin.jvm.internal.t;
import sm.q0;
import wg.d0;
import wg.i0;
import wg.n;

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

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

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

    /* renamed from: b, reason: collision with root package name */
    private final wg.e f29948b;

    /* renamed from: c, reason: collision with root package name */
    private final wg.e f29949c;

    /* renamed from: d, reason: collision with root package name */
    private boolean f29950d;

    /* renamed from: e, reason: collision with root package name */
    private boolean f29951e;

    /* renamed from: f, reason: collision with root package name */
    private Map f29952f;

    /* renamed from: g, reason: collision with root package name */
    private final wg.l f29953g;

    /* renamed from: h, reason: collision with root package name */
    private final h f29954h;

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

        public /* synthetic */ a(kotlin.jvm.internal.k kVar) {
            this();
        }
    }

    /* renamed from: ng.b$b, reason: collision with other inner class name */
    /* loaded from: classes4.dex */
    public enum EnumC0687b {
        REFERRER,
        LIFECYCLE
    }

    public b(wg.l processor, h analyticsState) {
        Map i10;
        t.g(processor, "processor");
        t.g(analyticsState, "analyticsState");
        this.f29953g = processor;
        this.f29954h = analyticsState;
        i10 = q0.i();
        this.f29952f = i10;
        i0 f10 = i0.f();
        t.f(f10, "ServiceProvider.getInstance()");
        wg.e mainDataQueue = f10.c().a("com.adobe.module.analytics");
        i0 f11 = i0.f();
        t.f(f11, "ServiceProvider.getInstance()");
        wg.e reorderDataQueue = f11.c().a("com.adobe.module.analyticsreorderqueue");
        t.f(mainDataQueue, "mainDataQueue");
        this.f29948b = mainDataQueue;
        t.f(reorderDataQueue, "reorderDataQueue");
        this.f29949c = reorderDataQueue;
        this.f29947a = new d0(mainDataQueue, processor);
        g();
    }

    private final wg.d a(Map map, wg.d dVar) {
        d a10 = d.f29957d.a(dVar);
        if (map == null) {
            throw new NullPointerException("null cannot be cast to non-null type kotlin.collections.Map<kotlin.String, kotlin.String>");
        }
        String payload = l.b(map, a10.b());
        t.f(payload, "payload");
        return new wg.d(new d(payload, a10.c(), a10.a()).d().a());
    }

    private final void g() {
        int count = this.f29949c.count();
        if (count <= 0) {
            wg.t.e("Analytics", "AnalyticsDatabase", "moveHitsFromReorderQueue - No hits in reorder queue", new Object[0]);
            return;
        }
        wg.t.e("Analytics", "AnalyticsDatabase", "moveHitsFromReorderQueue - Moving queued hits " + count + " from reorder queue -> main queue", new Object[0]);
        List b10 = this.f29949c.b(count);
        if (b10 != null) {
            Iterator it = b10.iterator();
            while (it.hasNext()) {
                this.f29948b.a((wg.d) it.next());
            }
        }
        this.f29949c.clear();
    }

    private final boolean k() {
        return this.f29951e || this.f29950d;
    }

    public final void b(EnumC0687b dataType) {
        t.g(dataType, "dataType");
        wg.t.a("Analytics", "AnalyticsDatabase", "cancelWaitForAdditionalData - " + dataType, new Object[0]);
        f(dataType, null);
    }

    public final int c() {
        return this.f29948b.count() + this.f29949c.count();
    }

    public final boolean d() {
        return this.f29949c.count() > 0;
    }

    public final void e(boolean z10) {
        wg.t.e("Analytics", "AnalyticsDatabase", "Kick - ignoreBatchLimit " + z10 + '.', new Object[0]);
        if (!this.f29954h.s()) {
            wg.t.e("Analytics", "AnalyticsDatabase", "Kick - Failed to kick database hits (Analytics is not configured).", new Object[0]);
            return;
        }
        if (!this.f29954h.x()) {
            wg.t.e("Analytics", "AnalyticsDatabase", "Kick - Failed to kick database hits (Privacy status is not opted-in).", new Object[0]);
            return;
        }
        if ((!this.f29954h.w() || this.f29948b.count() > this.f29954h.i()) || z10) {
            wg.t.e("Analytics", "AnalyticsDatabase", "Kick - Begin processing database hits", new Object[0]);
            this.f29947a.a();
        }
    }

    public final void f(EnumC0687b dataType, Map map) {
        Map i10;
        wg.d c10;
        Map o10;
        t.g(dataType, "dataType");
        if (k()) {
            wg.t.a("Analytics", "AnalyticsDatabase", "KickWithAdditionalData - " + dataType + " - " + map, new Object[0]);
            int i11 = c.f29956b[dataType.ordinal()];
            if (i11 == 1) {
                this.f29951e = false;
            } else if (i11 == 2) {
                this.f29950d = false;
            }
            if (map != null) {
                o10 = q0.o(this.f29952f, map);
                this.f29952f = o10;
            }
            if (!k()) {
                wg.t.a("Analytics", "AnalyticsDatabase", "KickWithAdditionalData - done waiting for additional data", new Object[0]);
                if (d() && (c10 = this.f29949c.c()) != null) {
                    this.f29948b.a(a(this.f29952f, c10));
                    this.f29949c.remove();
                }
                g();
                i10 = q0.i();
                this.f29952f = i10;
            }
            e(false);
        }
    }

    public final void h(String payload, long j10, String eventIdentifier, boolean z10) {
        t.g(payload, "payload");
        t.g(eventIdentifier, "eventIdentifier");
        wg.t.a("Analytics", "AnalyticsDatabase", "queueHit - " + payload + " isBackdateHit:" + z10, new Object[0]);
        String a10 = new d(payload, j10, eventIdentifier).d().a();
        if (a10 == null) {
            wg.t.a("Analytics", "AnalyticsDatabase", "queueHit - Dropping Analytics hit, failed to encode AnalyticsHit", new Object[0]);
            return;
        }
        wg.d dVar = new wg.d(a10);
        if (z10) {
            if (k()) {
                wg.t.a("Analytics", "AnalyticsDatabase", "queueHit - Queueing backdated hit", new Object[0]);
                this.f29948b.a(dVar);
            } else {
                wg.t.a("Analytics", "AnalyticsDatabase", "queueHit - Dropping backdate hit, as processing has begun for this current session", new Object[0]);
            }
        } else if (k()) {
            wg.t.a("Analytics", "AnalyticsDatabase", "queueHit - Queueing hit in reorder queue as a previous hit is waiting for additional data", new Object[0]);
            this.f29949c.a(dVar);
        } else {
            wg.t.a("Analytics", "AnalyticsDatabase", "queueHit - Queueing hit in main queue", new Object[0]);
            this.f29948b.a(dVar);
        }
        e(false);
    }

    public final void i() {
        Map i10;
        this.f29947a.f();
        this.f29948b.clear();
        this.f29949c.clear();
        i10 = q0.i();
        this.f29952f = i10;
        this.f29950d = false;
        this.f29951e = false;
    }

    public final void j(EnumC0687b dataType) {
        t.g(dataType, "dataType");
        wg.t.a("Analytics", "AnalyticsDatabase", "waitForAdditionalData - " + dataType, new Object[0]);
        int i10 = c.f29955a[dataType.ordinal()];
        if (i10 == 1) {
            this.f29951e = true;
        } else {
            if (i10 != 2) {
                return;
            }
            this.f29950d = true;
        }
    }
}
