package V2;

import java.io.IOException;
import java.util.Map;
import m3.AbstractC2467b;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import w4.C3137A;
import x4.AbstractC3251q;

/* JADX INFO: renamed from: V2.d3, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C0651d3 extends C4.j implements J4.e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ N2.s f7733a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final /* synthetic */ OkHttpClient f7734b;

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    public C0651d3(N2.s sVar, OkHttpClient okHttpClient, A4.d dVar) {
        super(2, dVar);
        this.f7733a = sVar;
        this.f7734b = okHttpClient;
    }

    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        return new C0651d3(this.f7733a, this.f7734b, dVar);
    }

    @Override // J4.e
    public final Object invoke(Object obj, Object obj2) {
        return ((C0651d3) create((V4.C) obj, (A4.d) obj2)).invokeSuspend(C3137A.f25453a);
    }

    @Override // C4.a
    public final Object invokeSuspend(Object obj) throws IOException {
        RequestBody requestBodyA;
        B4.a aVar = B4.a.f492a;
        AbstractC2467b.l(obj);
        N2.s sVar = this.f7733a;
        Map map = sVar.f5249c;
        Map map2 = sVar.f5252f;
        if (map2 != null) {
            MultipartBody.Builder builder = new MultipartBody.Builder(0);
            builder.c(MultipartBody.f22051h);
            for (Map.Entry entry : map2.entrySet()) {
                builder.a((String) entry.getKey(), (String) entry.getValue());
            }
            requestBodyA = builder.b();
        } else {
            Map map3 = sVar.f5251e;
            if (map3 != null) {
                String strM0 = AbstractC3251q.m0(map3.entrySet(), "&", null, null, new C0736x(19), 30);
                RequestBody.Companion companion = RequestBody.f22136a;
                MediaType.f22043d.getClass();
                MediaType mediaTypeB = MediaType.Companion.b("application/x-www-form-urlencoded");
                companion.getClass();
                requestBodyA = RequestBody.Companion.a(strM0, mediaTypeB);
            } else {
                String str = sVar.f5250d;
                if (str != null) {
                    String str2 = (String) map.get("Content-Type");
                    if (str2 == null) {
                        str2 = "application/json";
                    }
                    RequestBody.Companion companion2 = RequestBody.f22136a;
                    MediaType.f22043d.getClass();
                    MediaType mediaTypeB2 = MediaType.Companion.b(str2);
                    if (mediaTypeB2 == null) {
                        mediaTypeB2 = MediaType.Companion.b("application/json");
                    }
                    companion2.getClass();
                    requestBodyA = RequestBody.Companion.a(str, mediaTypeB2);
                } else {
                    requestBodyA = null;
                }
            }
        }
        Request.Builder builder2 = new Request.Builder();
        builder2.i(sVar.f5247a);
        builder2.g(sVar.f5248b, requestBodyA);
        for (Map.Entry entry2 : map.entrySet()) {
            builder2.a((String) entry2.getKey(), (String) entry2.getValue());
        }
        Response responseD = this.f7734b.a(builder2.b()).d();
        int i6 = responseD.f22143d;
        Headers headers = responseD.f22145f;
        try {
            try {
                ResponseBody responseBody = responseD.f22146j;
                String strB0 = responseBody != null ? e4.C.b0(responseBody) : null;
                N2.t tVar = new N2.t(responseD.c(), i6, strB0 != null ? strB0 : "", headers.k());
                responseD.close();
                return tVar;
            } catch (IOException unused) {
                N2.t tVar2 = new N2.t(false, i6, "", headers.k());
                responseD.close();
                return tVar2;
            }
        } finally {
        }
    }
}
import java.io.IOException;
import java.util.Map;
import m3.AbstractC2467b;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import w4.C3137A;
import x4.AbstractC3251q;

// This class represents a coroutine factory and execution handler for making HTTP requests.
// It is part of a reactive stream (Flow) implementation, likely used to fetch data from a server
// based on parameters provided by a parent object (sVar). It handles both multipart form data
// and JSON payloads, constructs the appropriate RequestBody, executes the request via OkHttp,
// and returns a structured result (N2.t) containing the response code, body, and headers.
// The class extends a generic factory interface (C4.j) and implements a coroutine factory (J4.e).
public final class C0651d3 extends C4.j implements J4.e {

    // Reference to the parent object containing request parameters (e.g., URL, headers, body).
    // Renamed from 'a' to 'parentRequestParams' for clarity.
    public final /* synthetic */ N2.s parentRequestParams;

    // The OkHttpClient instance used for executing HTTP requests.
    // Renamed from 'b' to 'httpClient' for clarity.
    public final /* synthetic */ OkHttpClient httpClient;

    // Constructor: Initializes the factory with the parent request parameters and the HTTP client.
    // The third parameter 'dVar' is passed to the superclass factory constructor.
    public C0651d3(N2.s sVar, OkHttpClient okHttpClient, A4.d dVar) {
        super(2, dVar);
        this.parentRequestParams = sVar;
        this.httpClient = okHttpClient;
    }

    // Factory method to create a new instance of this class.
    // This is required by the C4.j interface to support coroutine factory patterns.
    @Override // C4.a
    public final A4.d create(Object obj, A4.d dVar) {
        // Cast the generic object to the expected type V4.C and return a new instance.
        return new C0651d3((N2.s) obj, this.httpClient, dVar);
    }

    // Coroutine factory entry point: invokes the actual request logic.
    // It creates a new instance of this class and calls invokeSuspend with the coroutine context.
    @Override // J4.e
    public final Object invoke(Object obj, Object obj2) {
        // Cast inputs to expected types and invoke the suspend function.
        return ((C0651d3) create((V4.C) obj, (A4.d) obj2)).invokeSuspend(C3137A.f25453a);
    }

    // The main execution logic: performs the HTTP request based on the parameters.
    // This method is a suspend function, so it uses coroutines for non-blocking I/O.
    @Override // C4.a
    public final Object invokeSuspend(Object obj) throws IOException {
        // Validate the coroutine context (standard Kotlin coroutine check).
        AbstractC2467b.l(obj);

        // Retrieve the request parameters from the parent object.
        N2.s sVar = this.parentRequestParams;
        Map<String, String> headersMap = sVar.f5249c; // Request headers
        Map<String, String> bodyMap = sVar.f5252f;   // Multipart body parts
        Map<String, String> formMap = sVar.f5251e;   // Form-encoded body parts
        String jsonBody = sVar.f5250d;               // Raw JSON body string

        RequestBody requestBody;

        // Determine the type of body to send based on available data.
        if (bodyMap != null) {
            // Case 1: Multipart form data is present.
            // Build a MultipartBody with the specified boundary and parts.
            MultipartBody.Builder builder = new MultipartBody.Builder(MultipartBody.Builder.FORM);
            builder.setBoundary(MultipartBody.Builder.DEFAULT_BOUNDRY);
            for (Map.Entry<String, String> entry : bodyMap.entrySet()) {
                builder.addFormDataPart(entry.getKey(), entry.getValue());
            }
            requestBody = builder.build();
        } else if (formMap != null) {
            // Case 2: Form-encoded data is present.
            // Join the form fields with "&" to create a URL-encoded string.
            String formString = AbstractC3251q.m0(formMap.entrySet(), "&", null, null, new C0736x(19), 30);
            // Use the standard form-urlencoded media type.
            MediaType formMediaType = MediaType.parse("application/x-www-form-urlencoded");
            requestBody = RequestBody.create(formString, formMediaType);
        } else if (jsonBody != null) {
            // Case 3: Raw JSON body is present.
            // Check for a Content-Type header; default to "application/json" if missing.
            String contentType = headersMap != null ? headersMap.get("Content-Type") : null;
            if (contentType == null) {
                contentType = "application/json";
            }
            MediaType jsonMediaType = MediaType.parse(contentType);
            if (jsonMediaType == null) {
                jsonMediaType = MediaType.parse("application/json");
            }
            requestBody = RequestBody.create(jsonBody, jsonMediaType);
        } else {
            // No body data found.
            requestBody = null;
        }

        // Construct the HTTP request.
        Request.Builder requestBuilder = new Request.Builder();
        requestBuilder.url(sVar.f5247a); // Set the request URL.
        requestBuilder.post(requestBody); // Set the request body (assumed POST based on usage).
        // Add all headers from the map.
        if (headersMap != null) {
            for (Map.Entry<String, String> entry : headersMap.entrySet()) {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }

        // Execute the request using the provided OkHttpClient.
        Response response = this.httpClient.newCall(requestBuilder.build()).execute();
        int statusCode = response.code();
        Headers responseHeaders = response.headers();

        // Read the response body.
        String responseBodyContent = null;
        try {
            ResponseBody responseBody = response.body();
            if (responseBody != null) {
                responseBodyContent = e4.C.b0(responseBody); // Helper to read the full body.
            }
        } finally {
            // Ensure the response is closed to release resources.
            response.close();
        }

        // Return a structured result object (N2.t) containing the response details.
        return new N2.t(true, statusCode, responseBodyContent != null ? responseBodyContent : "", responseHeaders);
    }
}
