package okhttp3.n0.http;

import io.netty.handler.codec.http.HttpHeaders;
import java.util.List;
import java.util.Objects;
import kotlin.Metadata;
import kotlin.collections.j;
import kotlin.jvm.internal.o;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.n0.c;
import okio.GzipSource;

/* JADX INFO: renamed from: y.n0.h.a, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000.\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010 \n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\u0018\u00002\u00020\u0001B\r\u0012\u0006\u0010\u0002\u001a\u00020\u0003¢\u0006\u0002\u0010\u0004J\u0016\u0010\u0005\u001a\u00020\u00062\f\u0010\u0007\u001a\b\u0012\u0004\u0012\u00020\t0\bH\u0002J\u0010\u0010\n\u001a\u00020\u000b2\u0006\u0010\f\u001a\u00020\rH\u0016R\u000e\u0010\u0002\u001a\u00020\u0003X\u0082\u0004¢\u0006\u0002\n\u0000¨\u0006\u000e"}, d2 = {"Lokhttp3/internal/http/BridgeInterceptor;", "Lokhttp3/Interceptor;", "cookieJar", "Lokhttp3/CookieJar;", "(Lokhttp3/CookieJar;)V", "cookieHeader", "", "cookies", "", "Lokhttp3/Cookie;", "intercept", "Lokhttp3/Response;", "chain", "Lokhttp3/Interceptor$Chain;", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class BridgeInterceptor implements Interceptor {
    public final CookieJar a;

    public BridgeInterceptor(CookieJar cookieJar) {
        o.f(cookieJar, "cookieJar");
        this.a = cookieJar;
    }

    @Override // okhttp3.Interceptor
    public Response intercept(Interceptor.a aVar) {
        boolean z2;
        ResponseBody responseBody;
        o.f(aVar, "chain");
        RealInterceptorChain realInterceptorChain = (RealInterceptorChain) aVar;
        Request request = realInterceptorChain.f9829e;
        Objects.requireNonNull(request);
        Request.a aVar2 = new Request.a(request);
        RequestBody requestBody = request.f9623d;
        if (requestBody != null) {
            MediaType f9557j = requestBody.getF9557j();
            if (f9557j != null) {
                aVar2.b("Content-Type", f9557j.f9546d);
            }
            long jContentLength = requestBody.contentLength();
            if (jContentLength != -1) {
                aVar2.b("Content-Length", String.valueOf(jContentLength));
                aVar2.e(HttpHeaders.Names.TRANSFER_ENCODING);
            } else {
                aVar2.b(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
                aVar2.e("Content-Length");
            }
        }
        int i2 = 0;
        if (request.b("Host") == null) {
            aVar2.b("Host", c.y(request.a, false));
        }
        if (request.b("Connection") == null) {
            aVar2.b("Connection", "Keep-Alive");
        }
        if (request.b("Accept-Encoding") == null && request.b("Range") == null) {
            aVar2.b("Accept-Encoding", "gzip");
            z2 = true;
        } else {
            z2 = false;
        }
        List<Cookie> listB = this.a.b(request.a);
        if (!listB.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (Object obj : listB) {
                int i3 = i2 + 1;
                if (i2 < 0) {
                    j.e0();
                    throw null;
                }
                Cookie cookie = (Cookie) obj;
                if (i2 > 0) {
                    sb.append("; ");
                }
                sb.append(cookie.f10068f);
                sb.append('=');
                sb.append(cookie.f10069g);
                i2 = i3;
            }
            String string = sb.toString();
            o.e(string, "StringBuilder().apply(builderAction).toString()");
            aVar2.b(HttpHeaders.Names.COOKIE, string);
        }
        if (request.b("User-Agent") == null) {
            aVar2.b("User-Agent", "okhttp/4.11.0");
        }
        Response responseA = realInterceptorChain.a(aVar2.a());
        e.d(this.a, request.a, responseA.f9646q);
        Response.a aVar3 = new Response.a(responseA);
        aVar3.h(request);
        if (z2 && kotlin.text.j.g("gzip", Response.a(responseA, "Content-Encoding", null, 2), true) && e.a(responseA) && (responseBody = responseA.f9647r) != null) {
            GzipSource gzipSource = new GzipSource(responseBody.getF9836o());
            Headers.a aVarD = responseA.f9646q.d();
            aVarD.c("Content-Encoding");
            aVarD.c("Content-Length");
            aVar3.e(aVarD.b());
            aVar3.f9659g = new RealResponseBody(Response.a(responseA, "Content-Type", null, 2), -1L, kotlin.reflect.w.internal.y0.n.c2.c.m(gzipSource));
        }
        return aVar3.b();
    }
}
