package j;

import java.io.IOException;
import javax.annotation.Nullable;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/* JADX INFO: compiled from: RequestBuilder.java */
/* JADX INFO: loaded from: classes.dex */
final class l {

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private static final char[] f4356k = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    private final String a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final HttpUrl f4357b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    @Nullable
    private String f4358c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    @Nullable
    private HttpUrl.Builder f4359d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final Request.Builder f4360e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    @Nullable
    private MediaType f4361f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final boolean f4362g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    @Nullable
    private MultipartBody.Builder f4363h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    @Nullable
    private FormBody.Builder f4364i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    @Nullable
    private RequestBody f4365j;

    /* JADX INFO: compiled from: RequestBuilder.java */
    private static class a extends RequestBody {
        private final RequestBody a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final MediaType f4366b;

        a(RequestBody requestBody, MediaType mediaType) {
            this.a = requestBody;
            this.f4366b = mediaType;
        }

        @Override // okhttp3.RequestBody
        public long contentLength() throws IOException {
            return this.a.contentLength();
        }

        @Override // okhttp3.RequestBody
        public MediaType contentType() {
            return this.f4366b;
        }

        @Override // okhttp3.RequestBody
        public void writeTo(h.d dVar) throws IOException {
            this.a.writeTo(dVar);
        }
    }

    l(String str, HttpUrl httpUrl, @Nullable String str2, @Nullable Headers headers, @Nullable MediaType mediaType, boolean z, boolean z2, boolean z3) {
        this.a = str;
        this.f4357b = httpUrl;
        this.f4358c = str2;
        Request.Builder builder = new Request.Builder();
        this.f4360e = builder;
        this.f4361f = mediaType;
        this.f4362g = z;
        if (headers != null) {
            builder.headers(headers);
        }
        if (z2) {
            this.f4364i = new FormBody.Builder();
        } else if (z3) {
            MultipartBody.Builder builder2 = new MultipartBody.Builder();
            this.f4363h = builder2;
            builder2.setType(MultipartBody.FORM);
        }
    }

    private static String h(String str, boolean z) {
        int length = str.length();
        int iCharCount = 0;
        while (iCharCount < length) {
            int iCodePointAt = str.codePointAt(iCharCount);
            if (iCodePointAt < 32 || iCodePointAt >= 127 || " \"<>^`{}|\\?#".indexOf(iCodePointAt) != -1 || (!z && (iCodePointAt == 47 || iCodePointAt == 37))) {
                h.c cVar = new h.c();
                cVar.Z(str, 0, iCharCount);
                i(cVar, str, iCharCount, length, z);
                return cVar.r();
            }
            iCharCount += Character.charCount(iCodePointAt);
        }
        return str;
    }

    private static void i(h.c cVar, String str, int i2, int i3, boolean z) {
        h.c cVar2 = null;
        while (i2 < i3) {
            int iCodePointAt = str.codePointAt(i2);
            if (!z || (iCodePointAt != 9 && iCodePointAt != 10 && iCodePointAt != 12 && iCodePointAt != 13)) {
                if (iCodePointAt < 32 || iCodePointAt >= 127 || " \"<>^`{}|\\?#".indexOf(iCodePointAt) != -1 || (!z && (iCodePointAt == 47 || iCodePointAt == 37))) {
                    if (cVar2 == null) {
                        cVar2 = new h.c();
                    }
                    cVar2.b0(iCodePointAt);
                    while (!cVar2.z()) {
                        int i4 = cVar2.readByte() & 255;
                        cVar.N(37);
                        cVar.N(f4356k[(i4 >> 4) & 15]);
                        cVar.N(f4356k[i4 & 15]);
                    }
                } else {
                    cVar.b0(iCodePointAt);
                }
            }
            i2 += Character.charCount(iCodePointAt);
        }
    }

    void a(String str, String str2, boolean z) {
        if (z) {
            this.f4364i.addEncoded(str, str2);
        } else {
            this.f4364i.add(str, str2);
        }
    }

    void b(String str, String str2) {
        if (!"Content-Type".equalsIgnoreCase(str)) {
            this.f4360e.addHeader(str, str2);
            return;
        }
        MediaType mediaType = MediaType.parse(str2);
        if (mediaType != null) {
            this.f4361f = mediaType;
            return;
        }
        throw new IllegalArgumentException("Malformed content type: " + str2);
    }

    void c(Headers headers, RequestBody requestBody) {
        this.f4363h.addPart(headers, requestBody);
    }

    void d(MultipartBody.Part part) {
        this.f4363h.addPart(part);
    }

    void e(String str, String str2, boolean z) {
        String str3 = this.f4358c;
        if (str3 == null) {
            throw new AssertionError();
        }
        this.f4358c = str3.replace("{" + str + "}", h(str2, z));
    }

    void f(String str, @Nullable String str2, boolean z) {
        String str3 = this.f4358c;
        if (str3 != null) {
            HttpUrl.Builder builderNewBuilder = this.f4357b.newBuilder(str3);
            this.f4359d = builderNewBuilder;
            if (builderNewBuilder == null) {
                throw new IllegalArgumentException("Malformed URL. Base: " + this.f4357b + ", Relative: " + this.f4358c);
            }
            this.f4358c = null;
        }
        if (z) {
            this.f4359d.addEncodedQueryParameter(str, str2);
        } else {
            this.f4359d.addQueryParameter(str, str2);
        }
    }

    Request g() {
        HttpUrl httpUrlResolve;
        HttpUrl.Builder builder = this.f4359d;
        if (builder != null) {
            httpUrlResolve = builder.build();
        } else {
            httpUrlResolve = this.f4357b.resolve(this.f4358c);
            if (httpUrlResolve == null) {
                throw new IllegalArgumentException("Malformed URL. Base: " + this.f4357b + ", Relative: " + this.f4358c);
            }
        }
        RequestBody aVar = this.f4365j;
        if (aVar == null) {
            FormBody.Builder builder2 = this.f4364i;
            if (builder2 != null) {
                aVar = builder2.build();
            } else {
                MultipartBody.Builder builder3 = this.f4363h;
                if (builder3 != null) {
                    aVar = builder3.build();
                } else if (this.f4362g) {
                    aVar = RequestBody.create((MediaType) null, new byte[0]);
                }
            }
        }
        MediaType mediaType = this.f4361f;
        if (mediaType != null) {
            if (aVar != null) {
                aVar = new a(aVar, mediaType);
            } else {
                this.f4360e.addHeader("Content-Type", mediaType.toString());
            }
        }
        return this.f4360e.url(httpUrlResolve).method(this.a, aVar).build();
    }

    void j(RequestBody requestBody) {
        this.f4365j = requestBody;
    }
}
