package 㿎;

import com.facebook.GraphRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import okio.ByteString;
import ᨨ.AbstractC1483;
import ᨨ.C1492;
import 㿎.ሽ.C2540;

/* renamed from: 㿎.㴪  reason: contains not printable characters */
/* compiled from: MultipartBody */
public final class C2657 extends AbstractC2651 {

    /* renamed from: ඓ  reason: contains not printable characters */
    public static final C2628 f9029 = C2628.m6937("multipart/mixed");

    /* renamed from: ᨨ  reason: contains not printable characters */
    public static final byte[] f9030 = {45, 45};

    /* renamed from: ⁱ  reason: contains not printable characters */
    public static final byte[] f9031 = {58, 32};

    /* renamed from: 㴹  reason: contains not printable characters */
    public static final C2628 f9032 = C2628.m6937("multipart/form-data");

    /* renamed from: 㿎  reason: contains not printable characters */
    public static final byte[] f9033 = {13, 10};

    /* renamed from: ʬ  reason: contains not printable characters */
    public final List<C2658> f9034;

    /* renamed from: ῲ  reason: contains not printable characters */
    public final C2628 f9035;

    /* renamed from: K  reason: contains not printable characters */
    public final ByteString f9036;

    /* renamed from: 㸾  reason: contains not printable characters */
    public long f9037 = -1;

    /* renamed from: 㿎.㴪$ῲ  reason: contains not printable characters */
    /* compiled from: MultipartBody */
    public static final class C2658 {

        /* renamed from: ῲ  reason: contains not printable characters */
        public final AbstractC2651 f9038;

        /* renamed from: K  reason: contains not printable characters */
        public final C2667 f9039;

        public C2658(C2667 r1, AbstractC2651 r2) {
            this.f9039 = r1;
            this.f9038 = r2;
        }

        /* renamed from: K  reason: contains not printable characters */
        public static C2658 m6984(C2667 r1, AbstractC2651 r2) {
            if (r2 == null) {
                throw new NullPointerException("body == null");
            } else if (r1 != null && r1.m6996(GraphRequest.CONTENT_TYPE_HEADER) != null) {
                throw new IllegalArgumentException("Unexpected header: Content-Type");
            } else if (r1 == null || r1.m6996("Content-Length") == null) {
                return new C2658(r1, r2);
            } else {
                throw new IllegalArgumentException("Unexpected header: Content-Length");
            }
        }
    }

    static {
        C2628.m6937("multipart/alternative");
        C2628.m6937("multipart/digest");
        C2628.m6937("multipart/parallel");
    }

    public C2657(ByteString byteString, C2628 r4, List<C2658> list) {
        this.f9036 = byteString;
        this.f9035 = C2628.m6937(r4 + "; boundary=" + byteString.utf8());
        this.f9034 = C2540.m6646(list);
    }

    @Override // 㿎.AbstractC2651
    public long contentLength() throws IOException {
        long j = this.f9037;
        if (j != -1) {
            return j;
        }
        long r0 = m6983((AbstractC1483) null, true);
        this.f9037 = r0;
        return r0;
    }

    @Override // 㿎.AbstractC2651
    public C2628 contentType() {
        return this.f9035;
    }

    @Override // 㿎.AbstractC2651
    public void writeTo(AbstractC1483 r2) throws IOException {
        m6983(r2, false);
    }

    /* JADX DEBUG: Multi-variable search result rejected for r0v0, resolved type: ᨨ.㴹 */
    /* JADX DEBUG: Multi-variable search result rejected for r0v1, resolved type: ᨨ.㴹 */
    /* JADX DEBUG: Multi-variable search result rejected for r0v2, resolved type: ᨨ.㴹 */
    /* JADX WARN: Multi-variable type inference failed */
    /* renamed from: K  reason: contains not printable characters */
    public final long m6983(AbstractC1483 r13, boolean z) throws IOException {
        C1492 r0;
        if (z) {
            r13 = new C1492();
            r0 = r13;
        } else {
            r0 = 0;
        }
        int size = this.f9034.size();
        long j = 0;
        for (int i = 0; i < size; i++) {
            C2658 r6 = this.f9034.get(i);
            C2667 r7 = r6.f9039;
            AbstractC2651 r62 = r6.f9038;
            r13.write(f9030);
            r13.m3730(this.f9036);
            r13.write(f9033);
            if (r7 != null) {
                int r8 = r7.m6993();
                for (int i2 = 0; i2 < r8; i2++) {
                    r13.m3729(r7.m6995(i2)).write(f9031).m3729(r7.m6994(i2)).write(f9033);
                }
            }
            C2628 contentType = r62.contentType();
            if (contentType != null) {
                r13.m3729("Content-Type: ").m3729(contentType.f8933).write(f9033);
            }
            long contentLength = r62.contentLength();
            if (contentLength != -1) {
                r13.m3729("Content-Length: ").m3726(contentLength).write(f9033);
            } else if (z) {
                r0.m3773();
                return -1;
            }
            r13.write(f9033);
            if (z) {
                j += contentLength;
            } else {
                r62.writeTo(r13);
            }
            r13.write(f9033);
        }
        r13.write(f9030);
        r13.m3730(this.f9036);
        r13.write(f9030);
        r13.write(f9033);
        if (!z) {
            return j;
        }
        long j2 = j + r0.f5529;
        r0.m3773();
        return j2;
    }

    /* renamed from: 㿎.㴪$K  reason: contains not printable characters */
    /* compiled from: MultipartBody */
    public static final class C2659 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public final List<C2658> f9040 = new ArrayList();

        /* renamed from: ῲ  reason: contains not printable characters */
        public C2628 f9041 = C2657.f9029;

        /* renamed from: K  reason: contains not printable characters */
        public final ByteString f9042;

        public C2659() {
            String uuid = UUID.randomUUID().toString();
            this.f9042 = ByteString.encodeUtf8(uuid);
        }

        /* renamed from: K  reason: contains not printable characters */
        public C2659 m6985(C2628 r4) {
            if (r4 == null) {
                throw new NullPointerException("type == null");
            } else if (r4.f8932.equals("multipart")) {
                this.f9041 = r4;
                return this;
            } else {
                throw new IllegalArgumentException("multipart != " + r4);
            }
        }

        /* renamed from: K  reason: contains not printable characters */
        public C2659 m6987(C2667 r1, AbstractC2651 r2) {
            m6986(C2658.m6984(r1, r2));
            return this;
        }

        /* renamed from: K  reason: contains not printable characters */
        public C2659 m6986(C2658 r2) {
            if (r2 != null) {
                this.f9040.add(r2);
                return this;
            }
            throw new NullPointerException("part == null");
        }

        /* renamed from: K  reason: contains not printable characters */
        public C2657 m6988() {
            if (!this.f9040.isEmpty()) {
                return new C2657(this.f9042, this.f9041, this.f9040);
            }
            throw new IllegalStateException("Multipart body must have at least one part.");
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public static StringBuilder m6982(StringBuilder sb, String str) {
        sb.append('\"');
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char charAt = str.charAt(i);
            if (charAt == '\n') {
                sb.append("%0A");
            } else if (charAt == '\r') {
                sb.append("%0D");
            } else if (charAt != '\"') {
                sb.append(charAt);
            } else {
                sb.append("%22");
            }
        }
        sb.append('\"');
        return sb;
    }
}
