package com.revenuecat.purchases.subscriberattributes;

import J4.c;
import J4.f;
import com.revenuecat.purchases.PurchasesError;
import com.revenuecat.purchases.PurchasesErrorCode;
import com.revenuecat.purchases.common.BackendHelper;
import com.revenuecat.purchases.common.Delay;
import com.revenuecat.purchases.common.networking.Endpoint;
import com.revenuecat.purchases.common.networking.RCHTTPStatusCodes;
import java.util.Map;
import kotlin.jvm.internal.o;
import kotlin.jvm.internal.p;
import org.json.JSONException;
import org.json.JSONObject;
import w4.C3137A;
import w4.k;
import x4.AbstractC3229F;
import x4.y;

/* JADX INFO: loaded from: classes3.dex */
public final class SubscriberAttributesPoster {
    private final BackendHelper backendHelper;

    /* JADX INFO: renamed from: com.revenuecat.purchases.subscriberattributes.SubscriberAttributesPoster$postSubscriberAttributes$1, reason: invalid class name */
    public static final class AnonymousClass1 extends p implements c {
        final /* synthetic */ f $onErrorHandler;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public AnonymousClass1(f fVar) {
            super(1);
            this.$onErrorHandler = fVar;
        }

        @Override // J4.c
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            invoke((PurchasesError) obj);
            return C3137A.f25453a;
        }

        public final void invoke(PurchasesError error) {
            o.h(error, "error");
            this.$onErrorHandler.invoke(error, Boolean.FALSE, y.f26065a);
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.subscriberattributes.SubscriberAttributesPoster$postSubscriberAttributes$2, reason: invalid class name */
    public static final class AnonymousClass2 extends p implements f {
        final /* synthetic */ f $onErrorHandler;
        final /* synthetic */ J4.a $onSuccessHandler;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public AnonymousClass2(J4.a aVar, f fVar) {
            super(3);
            this.$onSuccessHandler = aVar;
            this.$onErrorHandler = fVar;
        }

        @Override // J4.f
        public /* bridge */ /* synthetic */ Object invoke(Object obj, Object obj2, Object obj3) throws JSONException {
            invoke((PurchasesError) obj, ((Number) obj2).intValue(), (JSONObject) obj3);
            return C3137A.f25453a;
        }

        public final void invoke(PurchasesError purchasesError, int i6, JSONObject body) throws JSONException {
            C3137A c3137a;
            o.h(body, "body");
            if (purchasesError != null) {
                f fVar = this.$onErrorHandler;
                boolean zIsServerError = RCHTTPStatusCodes.INSTANCE.isServerError(i6);
                boolean z6 = false;
                boolean z7 = i6 == 404;
                if (!zIsServerError && !z7) {
                    z6 = true;
                }
                Object attributeErrors = y.f26065a;
                if (purchasesError.getCode() == PurchasesErrorCode.InvalidSubscriberAttributesError) {
                    attributeErrors = BackendHelpersKt.getAttributeErrors(body);
                }
                fVar.invoke(purchasesError, Boolean.valueOf(z6), attributeErrors);
                c3137a = C3137A.f25453a;
            } else {
                c3137a = null;
            }
            if (c3137a == null) {
                this.$onSuccessHandler.invoke();
            }
        }
    }

    public SubscriberAttributesPoster(BackendHelper backendHelper) {
        o.h(backendHelper, "backendHelper");
        this.backendHelper = backendHelper;
    }

    public final void postSubscriberAttributes(Map<String, ? extends Map<String, ? extends Object>> attributes, String appUserID, J4.a onSuccessHandler, f onErrorHandler) {
        o.h(attributes, "attributes");
        o.h(appUserID, "appUserID");
        o.h(onSuccessHandler, "onSuccessHandler");
        o.h(onErrorHandler, "onErrorHandler");
        this.backendHelper.performRequest(new Endpoint.PostAttributes(appUserID), AbstractC3229F.v(new k("attributes", attributes)), null, Delay.DEFAULT, new AnonymousClass1(onErrorHandler), new AnonymousClass2(onSuccessHandler, onErrorHandler));
    }
}
