package ng;

import ca.canadapost.core.data.model.CoreConstants;
import com.adobe.marketing.mobile.Event;
import com.adobe.marketing.mobile.ExtensionApi;
import com.amplifyframework.storage.ObjectMetadata;
import com.fasterxml.jackson.core.JsonPointer;
import java.util.Map;
import kotlin.jvm.internal.n0;
import kotlin.jvm.internal.o0;
import kotlin.text.y;
import rm.c0;
import sm.p;
import sm.p0;
import sm.q0;
import wg.i0;
import wg.o;
import wg.r;
import wg.t;
import wg.w;
import wg.x;
import wg.z;

/* loaded from: classes4.dex */
public final class e implements wg.l {

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

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

    /* renamed from: b, reason: collision with root package name */
    private long f29963b;

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

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

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

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

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

    /* loaded from: classes4.dex */
    static final class b implements w {

        /* renamed from: b, reason: collision with root package name */
        final /* synthetic */ wg.m f29968b;

        /* renamed from: c, reason: collision with root package name */
        final /* synthetic */ String f29969c;

        /* renamed from: d, reason: collision with root package name */
        final /* synthetic */ o0 f29970d;

        /* renamed from: e, reason: collision with root package name */
        final /* synthetic */ String f29971e;

        /* renamed from: f, reason: collision with root package name */
        final /* synthetic */ n0 f29972f;

        b(wg.m mVar, String str, o0 o0Var, String str2, n0 n0Var) {
            this.f29968b = mVar;
            this.f29969c = str;
            this.f29970d = o0Var;
            this.f29971e = str2;
            this.f29972f = n0Var;
        }

        @Override // wg.w
        public final void a(o oVar) {
            boolean N;
            Map l10;
            Map l11;
            boolean z10 = false;
            if (oVar == null) {
                t.f("Analytics", "AnalyticsHitProcessor", "processHit - Retrying Analytics hit, there is currently no network connectivity", new Object[0]);
                this.f29968b.a(false);
                return;
            }
            int d10 = oVar.d();
            if (d10 == 200) {
                t.a("Analytics", "AnalyticsHitProcessor", "processHit - Analytics hit request with url (" + this.f29969c + ") and payload (" + ((String) this.f29970d.f26199p) + ") sent successfully", new Object[0]);
                l10 = q0.l(c0.a("ETag", oVar.b("ETag")), c0.a("Server", oVar.b("Server")), c0.a(ObjectMetadata.CONTENT_TYPE, oVar.b(ObjectMetadata.CONTENT_TYPE)));
                l11 = q0.l(c0.a("analyticsserverresponse", ch.i.a(oVar.c())), c0.a("headers", l10), c0.a("hitHost", this.f29969c), c0.a("hitUrl", (String) this.f29970d.f26199p), c0.a("requestEventIdentifier", this.f29971e));
                if (this.f29972f.f26198p > e.this.f29965d.l()) {
                    t.a("Analytics", "AnalyticsHitProcessor", "processHit - Dispatching Analytics hit response for request event id %s.", this.f29971e);
                    e.this.f29966e.c(new Event.Builder("AnalyticsResponse", "com.adobe.eventType.analytics", "com.adobe.eventSource.responseContent").d(l11).a());
                } else {
                    t.a("Analytics", "AnalyticsHitProcessor", "processHit - Ignoring response for request event id %s as it was received while processing a resetIdentities event.", this.f29971e);
                }
                e.this.f29963b = this.f29972f.f26198p;
            } else {
                N = p.N(new Integer[]{408, 504, 503, -1}, Integer.valueOf(d10));
                if (N) {
                    t.f("Analytics", "AnalyticsHitProcessor", "processHit - Retrying Analytics hit, request with url " + this.f29969c + " failed with recoverable status code " + oVar.d(), new Object[0]);
                    oVar.close();
                    this.f29968b.a(z10);
                }
                t.f("Analytics", "AnalyticsHitProcessor", "processHit - Dropping Analytics hit, request with url " + this.f29969c + " failed with error and unrecoverable status code " + oVar.d() + ": " + ch.i.a(oVar.a()), new Object[0]);
            }
            z10 = true;
            oVar.close();
            this.f29968b.a(z10);
        }
    }

    public e(h analyticsState, ExtensionApi extensionApi) {
        kotlin.jvm.internal.t.g(analyticsState, "analyticsState");
        kotlin.jvm.internal.t.g(extensionApi, "extensionApi");
        this.f29965d = analyticsState;
        this.f29966e = extensionApi;
        i0 f10 = i0.f();
        kotlin.jvm.internal.t.f(f10, "ServiceProvider.getInstance()");
        z h10 = f10.h();
        kotlin.jvm.internal.t.f(h10, "ServiceProvider.getInstance().networkService");
        this.f29962a = h10;
        this.f29964c = j.b(j.f30008a, null, null, 3, null);
    }

    private final String f(h hVar) {
        int n10;
        if (!hVar.s()) {
            return null;
        }
        StringBuilder sb2 = new StringBuilder();
        sb2.append("https://");
        sb2.append(hVar.k());
        sb2.append("/b/ss/");
        String q10 = hVar.q();
        if (q10 == null) {
            q10 = "";
        }
        sb2.append(q10);
        sb2.append(JsonPointer.SEPARATOR);
        sb2.append(g(hVar));
        sb2.append(JsonPointer.SEPARATOR);
        sb2.append(this.f29964c);
        sb2.append("/s");
        n10 = jn.o.n(new jn.i(0, 100000000), hn.c.f21873p);
        sb2.append(n10);
        String sb3 = sb2.toString();
        if (ch.m.a(sb3)) {
            return sb3;
        }
        t.a("Analytics", "AnalyticsHitProcessor", "getAnalyticsBaseUrl - Error building Analytics base URL, returning null", new Object[0]);
        return null;
    }

    private final String g(h hVar) {
        return hVar.t() ? "10" : "0";
    }

    private final String h(String str, long j10, long j11) {
        String L;
        L = y.L(str, "&ts=" + j10, "&ts=" + j11, false, 4, null);
        return L;
    }

    @Override // wg.l
    public int a(wg.d dataEntity) {
        kotlin.jvm.internal.t.g(dataEntity, "dataEntity");
        return 30;
    }

    @Override // wg.l
    public void b(wg.d entity, wg.m processingResult) {
        String str;
        Map e10;
        kotlin.jvm.internal.t.g(entity, "entity");
        kotlin.jvm.internal.t.g(processingResult, "processingResult");
        d a10 = d.f29957d.a(entity);
        String a11 = a10.a();
        o0 o0Var = new o0();
        o0Var.f26199p = a10.b();
        n0 n0Var = new n0();
        n0Var.f26198p = a10.c();
        if (((String) o0Var.f26199p).length() == 0) {
            t.a("Analytics", "AnalyticsHitProcessor", "processHit - Dropping Analytics hit, payload is empty.", new Object[0]);
            processingResult.a(true);
            return;
        }
        if (n0Var.f26198p < this.f29965d.l()) {
            t.a("Analytics", "AnalyticsHitProcessor", "processHit - Dropping Analytics hit, resetIdentities API was called after this request.", new Object[0]);
            processingResult.a(true);
            return;
        }
        if (this.f29965d.w()) {
            str = "AnalyticsHitProcessor";
        } else {
            if (n0Var.f26198p < ch.k.h() - 60) {
                t.a("Analytics", "AnalyticsHitProcessor", "processHit - Dropping Analytics hit, timestamp exceeds offline disabled wait threshold", new Object[0]);
                processingResult.a(true);
                return;
            }
            str = "AnalyticsHitProcessor";
        }
        if (this.f29965d.w()) {
            long j10 = n0Var.f26198p;
            long j11 = this.f29963b;
            if (j10 - j11 < 0) {
                long j12 = j11 + 1;
                t.a("Analytics", str, "processHit - Adjusting out of order hit timestamp " + a10 + ".timestamp -> " + j12, new Object[0]);
                o0Var.f26199p = h((String) o0Var.f26199p, n0Var.f26198p, j12);
                n0Var.f26198p = j12;
            }
        }
        String f10 = f(this.f29965d);
        if (f10 == null) {
            t.a("Analytics", str, "processHit - Retrying Analytics hit, error generating base url.", new Object[0]);
            processingResult.a(false);
            return;
        }
        if (this.f29965d.u()) {
            o0Var.f26199p = ((String) o0Var.f26199p) + "&p.&debug=true&.p";
        }
        e10 = p0.e(c0.a(ObjectMetadata.CONTENT_TYPE, CoreConstants.OAUTH_CONTENT_TYPE));
        r rVar = r.POST;
        byte[] bytes = ((String) o0Var.f26199p).getBytes(kotlin.text.d.f26484b);
        kotlin.jvm.internal.t.f(bytes, "this as java.lang.String).getBytes(charset)");
        this.f29962a.a(new x(f10, rVar, bytes, e10, 5, 5), new b(processingResult, f10, o0Var, a11, n0Var));
    }
}
