package com.bytedance.retrofit2;

import X.AnonymousClass8BD;
import X.C50869eP;
import com.bytedance.covode.number.Covode;
import com.bytedance.retrofit2.mime.FormUrlEncodedTypedOutput;
import com.bytedance.retrofit2.mime.MultipartTypedOutput;
import com.bytedance.retrofit2.mime.TypedOutput;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import okhttp3.Headers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public final class RequestBuilder {
    public String LIZ;
    public String LIZIZ;
    public String LIZJ;
    public String LIZLLL;
    public List<AnonymousClass8BD> LJ;
    public final MultipartTypedOutput LJFF;
    public TypedOutput LJI;
    public int LJII;
    public final String LJIIIIZZ;
    public boolean LJIIIZ;
    public int LJIIJ;
    public boolean LJIIJJI;
    public Object LJIIL;
    public final MultipartBody.Builder LJIILIIL;
    public RequestBody LJIILJJIL;
    public boolean LJIILL;
    public final C50869eP LJIILLIIL;
    public StringBuilder LJIIZILJ;
    public String LJIJ;
    public final boolean LJIJI;
    public final FormUrlEncodedTypedOutput LJIJJ;
    public final int LJIJJLI;
    public final Map<Class<?>, Object> LJIL = new LinkedHashMap();

    static {
        Covode.recordClassIndex(36965);
    }

    public static class MimeOverridingTypedOutput implements TypedOutput {
        public final TypedOutput delegate;
        public final String mimeType;

        static {
            Covode.recordClassIndex(36966);
        }

        public String mimeType() {
            return this.mimeType;
        }

        public String fileName() {
            return this.delegate.fileName();
        }

        public long length() {
            return this.delegate.length();
        }

        public String md5Stub() {
            return this.delegate.md5Stub();
        }

        public void writeTo(OutputStream outputStream) {
            this.delegate.writeTo(outputStream);
        }

        public MimeOverridingTypedOutput(TypedOutput typedOutput, String str) {
            this.delegate = typedOutput;
            this.mimeType = str;
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:36:0x0084, code lost:
        if (r1 != null) goto L_0x0086;
     */
    /* JADX WARNING: Failed to insert additional move for type inference */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final com.bytedance.retrofit2.client.Request LIZ() {
        /*
            r15 = this;
            com.bytedance.retrofit2.mime.MultipartTypedOutput r0 = r15.LJFF
            if (r0 == 0) goto L_0x000e
            int r0 = r0.getPartCount()
            if (r0 != 0) goto L_0x000e
            boolean r0 = r15.LJIILL
            if (r0 == 0) goto L_0x0198
        L_0x000e:
            X.9eP r0 = r15.LJIILLIIL
            java.lang.String r2 = r0.LIZ()
            boolean r0 = X.C54809kl.LIZIZ
            java.lang.String r6 = "/"
            if (r0 == 0) goto L_0x0108
            okhttp3.HttpUrl r5 = okhttp3.HttpUrl.parse(r2)
            java.lang.String r4 = ", Relative: "
            java.lang.String r3 = "Malformed URL. Base: "
            if (r5 == 0) goto L_0x017e
            java.lang.String r0 = r5.encodedPath()
            if (r0 == 0) goto L_0x00f5
            java.lang.String r0 = r5.encodedPath()
            int r0 = r0.length()
            if (r0 <= 0) goto L_0x00f5
            java.lang.String r0 = r15.LIZJ
            boolean r0 = r6.equals(r0)
            if (r0 == 0) goto L_0x00f5
            java.lang.String r0 = r15.LIZJ
            java.lang.StringBuilder r1 = r15.LIZIZ(r2, r0)
        L_0x0042:
            java.lang.StringBuilder r4 = r15.LJIIZILJ
            r3 = 0
            if (r4 == 0) goto L_0x0062
            char r0 = r4.charAt(r3)
            r2 = 63
            if (r2 != r0) goto L_0x005f
            java.lang.String r0 = r15.LIZJ
            if (r0 == 0) goto L_0x005f
            int r2 = r0.indexOf(r2)
            r0 = -1
            if (r2 == r0) goto L_0x005f
            r0 = 38
            r4.setCharAt(r3, r0)
        L_0x005f:
            r1.append(r4)
        L_0x0062:
            java.lang.String r0 = r15.LIZLLL
            if (r0 == 0) goto L_0x0069
            r1.append(r0)
        L_0x0069:
            java.lang.String r0 = r1.toString()
            r15.LIZIZ = r0
            com.bytedance.retrofit2.mime.TypedOutput r5 = r15.LJI
            java.util.List<X.8BD> r4 = r15.LJ
            okhttp3.RequestBody r1 = r15.LJIILJJIL
            boolean r0 = r15.LJIILL
            r6 = 0
            if (r0 == 0) goto L_0x00d2
            if (r1 != 0) goto L_0x0086
            okhttp3.MultipartBody$Builder r0 = r15.LJIILIIL
            if (r0 == 0) goto L_0x00c5
            okhttp3.MultipartBody r1 = r0.build()
        L_0x0084:
            if (r1 == 0) goto L_0x00d0
        L_0x0086:
            java.lang.String r0 = r15.LJIJ
            if (r0 == 0) goto L_0x00d0
            X.9eA r6 = new X.9eA
            java.lang.String r0 = r15.LJIJ
            r6.<init>(r1, r0)
        L_0x0091:
            if (r5 != 0) goto L_0x00ab
            java.lang.String r0 = r15.LIZ
            boolean r0 = X.PE8.LIZ(r0)
            if (r0 == 0) goto L_0x00ab
            boolean r0 = r15.LJIILL
            if (r0 != 0) goto L_0x00ab
            com.bytedance.retrofit2.mime.FormUrlEncodedTypedOutput r5 = new com.bytedance.retrofit2.mime.FormUrlEncodedTypedOutput
            r5.<init>()
            java.lang.String r1 = "body"
            java.lang.String r0 = "null"
            r5.addField(r1, r0)
        L_0x00ab:
            com.bytedance.retrofit2.client.Request r1 = new com.bytedance.retrofit2.client.Request
            java.lang.String r2 = r15.LIZ
            java.lang.String r3 = r15.LIZIZ
            int r7 = r15.LJII
            int r8 = r15.LJIJJLI
            boolean r9 = r15.LJIIIZ
            int r10 = r15.LJIIJ
            boolean r11 = r15.LJIIJJI
            java.lang.Object r12 = r15.LJIIL
            java.lang.String r13 = r15.LJIIIIZZ
            java.util.Map<java.lang.Class<?>, java.lang.Object> r14 = r15.LJIL
            r1.<init>(r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14)
            return r1
        L_0x00c5:
            boolean r0 = r15.LJIJI
            if (r0 == 0) goto L_0x00d0
            byte[] r0 = new byte[r3]
            okhttp3.RequestBody r1 = okhttp3.RequestBody.create((okhttp3.MediaType) r6, (byte[]) r0)
            goto L_0x0084
        L_0x00d0:
            r6 = r1
            goto L_0x0091
        L_0x00d2:
            java.lang.String r0 = r15.LJIJ
            if (r0 == 0) goto L_0x0091
            if (r5 == 0) goto L_0x00e1
            com.bytedance.retrofit2.RequestBuilder$MimeOverridingTypedOutput r1 = new com.bytedance.retrofit2.RequestBuilder$MimeOverridingTypedOutput
            java.lang.String r0 = r15.LJIJ
            r1.<init>(r5, r0)
            r5 = r1
            goto L_0x0091
        L_0x00e1:
            X.8BD r2 = new X.8BD
            java.lang.String r1 = r15.LJIJ
            java.lang.String r0 = "Content-Type"
            r2.<init>(r0, r1)
            if (r4 != 0) goto L_0x00f1
            java.util.List r4 = java.util.Collections.singletonList(r2)
            goto L_0x0091
        L_0x00f1:
            r4.add(r2)
            goto L_0x0091
        L_0x00f5:
            java.lang.String r0 = r15.LIZJ
            okhttp3.HttpUrl r0 = r5.resolve(r0)
            if (r0 == 0) goto L_0x0164
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = r0.toString()
            r1.<init>(r0)
            goto L_0x0042
        L_0x0108:
            java.net.URI r1 = java.net.URI.create(r2)     // Catch:{ all -> 0x013d }
            java.lang.String r0 = r1.getPath()     // Catch:{ all -> 0x013d }
            if (r0 == 0) goto L_0x012c
            java.lang.String r0 = r1.getPath()     // Catch:{ all -> 0x013d }
            int r0 = r0.length()     // Catch:{ all -> 0x013d }
            if (r0 <= 0) goto L_0x012c
            java.lang.String r0 = r15.LIZJ     // Catch:{ all -> 0x013d }
            boolean r0 = r6.equals(r0)     // Catch:{ all -> 0x013d }
            if (r0 == 0) goto L_0x012c
            java.lang.String r0 = r15.LIZJ     // Catch:{ all -> 0x013d }
            java.lang.StringBuilder r1 = r15.LIZIZ(r2, r0)     // Catch:{ all -> 0x013d }
            goto L_0x0042
        L_0x012c:
            java.lang.String r0 = r15.LIZJ     // Catch:{ all -> 0x013d }
            java.net.URI r0 = r1.resolve(r0)     // Catch:{ all -> 0x013d }
            java.lang.StringBuilder r1 = new java.lang.StringBuilder     // Catch:{ all -> 0x013d }
            java.lang.String r0 = r0.toString()     // Catch:{ all -> 0x013d }
            r1.<init>(r0)     // Catch:{ all -> 0x013d }
            goto L_0x0042
        L_0x013d:
            java.lang.String r1 = r15.LIZJ
            if (r1 == 0) goto L_0x015c
            java.lang.String r0 = "http://"
            boolean r0 = r1.startsWith(r0)
            if (r0 != 0) goto L_0x0153
            java.lang.String r1 = r15.LIZJ
            java.lang.String r0 = "https://"
            boolean r0 = r1.startsWith(r0)
            if (r0 == 0) goto L_0x015c
        L_0x0153:
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = r15.LIZJ
            r1.<init>(r0)
            goto L_0x0042
        L_0x015c:
            java.lang.String r0 = r15.LIZJ
            java.lang.StringBuilder r1 = r15.LIZIZ(r2, r0)
            goto L_0x0042
        L_0x0164:
            java.lang.IllegalArgumentException r2 = new java.lang.IllegalArgumentException
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            r1.<init>(r3)
            r1.append(r5)
            r1.append(r4)
            java.lang.String r0 = r15.LIZJ
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            r2.<init>(r0)
            throw r2
        L_0x017e:
            java.lang.IllegalArgumentException r2 = new java.lang.IllegalArgumentException
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            r1.<init>(r3)
            r1.append(r5)
            r1.append(r4)
            java.lang.String r0 = r15.LIZJ
            r1.append(r0)
            java.lang.String r0 = r1.toString()
            r2.<init>(r0)
            throw r2
        L_0x0198:
            java.lang.IllegalStateException r1 = new java.lang.IllegalStateException
            java.lang.String r0 = "Multipart requests must contain at least one part."
            r1.<init>(r0)
            throw r1
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.retrofit2.RequestBuilder.LIZ():com.bytedance.retrofit2.client.Request");
    }

    public final void LIZ(Headers headers, RequestBody requestBody) {
        this.LJIILIIL.addPart(headers, requestBody);
    }

    public final <T> void LIZ(Class<? super T> cls, T t) {
        this.LJIL.put(cls, cls.cast(t));
    }

    private StringBuilder LIZIZ(String str, String str2) {
        StringBuilder sb = new StringBuilder(str);
        if (str.endsWith("/")) {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append(str2);
        return sb;
    }

    public final void LIZ(String str, String str2) {
        if (str == null) {
            throw new IllegalArgumentException("Header name must not be null.");
        } else if ("Content-Type".equalsIgnoreCase(str)) {
            this.LJIJ = str2;
        } else {
            List list = this.LJ;
            if (list == null) {
                list = new ArrayList(2);
                this.LJ = list;
            }
            list.add(new AnonymousClass8BD(str, str2));
        }
    }

    public final void LIZIZ(String str, String str2, boolean z) {
        this.LJIJJ.addField(str, z, str2, z);
    }

    public final void LIZ(String str, String str2, boolean z) {
        char c;
        if (str != null) {
            try {
                StringBuilder sb = this.LJIIZILJ;
                if (sb == null) {
                    sb = new StringBuilder();
                    this.LJIIZILJ = sb;
                }
                if (sb.length() > 0) {
                    c = '&';
                } else {
                    c = '?';
                }
                sb.append(c);
                if (z) {
                    str = URLEncoder.encode(str, "UTF-8");
                    str2 = URLEncoder.encode(str2, "UTF-8");
                }
                if (str2 == null || str2.isEmpty()) {
                    sb.append(str);
                    return;
                }
                sb.append(str);
                sb.append('=');
                sb.append(str2);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("Unable to convert query parameter \"" + str + "\" value to UTF-8: " + str2, e);
            }
        } else {
            throw new IllegalArgumentException("Query param name must not be null.");
        }
    }

    public RequestBuilder(String str, C50869eP r5, String str2, List<AnonymousClass8BD> list, String str3, int i, int i2, boolean z, int i3, boolean z2, Object obj, boolean z3, boolean z4, boolean z5, String str4) {
        this.LIZ = str;
        this.LJIILLIIL = r5;
        this.LIZJ = str2;
        this.LJIJ = str3;
        this.LJII = i;
        this.LJIJJLI = i2;
        this.LJIIIZ = z;
        this.LJIIJ = i3;
        this.LJIIJJI = z2;
        this.LJIIL = obj;
        this.LJIJI = z3;
        this.LJ = list;
        this.LJIIIIZZ = str4;
        if (z4) {
            FormUrlEncodedTypedOutput formUrlEncodedTypedOutput = new FormUrlEncodedTypedOutput();
            this.LJIJJ = formUrlEncodedTypedOutput;
            this.LJFF = null;
            this.LJI = formUrlEncodedTypedOutput;
            this.LJIILIIL = null;
        } else if (z5) {
            this.LJIJJ = null;
            MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput();
            this.LJFF = multipartTypedOutput;
            this.LJI = multipartTypedOutput;
            MultipartBody.Builder builder = new MultipartBody.Builder();
            this.LJIILIIL = builder;
            builder.setType(MultipartBody.FORM);
        } else {
            this.LJIJJ = null;
            this.LJFF = null;
            this.LJIILIIL = null;
        }
    }
}
