package okhttp3;

import A0.a;
import S4.w;
import e4.C;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import kotlin.jvm.internal.o;

/* JADX INFO: loaded from: classes3.dex */
public final class MediaType {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final Companion f22043d = new Companion(0);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final Pattern f22044e = Pattern.compile("([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)/([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)");

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final Pattern f22045f = Pattern.compile(";\\s*(?:([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)=(?:([a-zA-Z0-9-!#$%&'*+.^_`{|}~]+)|\"([^\"]*)\"))?");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final String f22046a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final String f22047b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final String[] f22048c;

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        public static MediaType a(String str) {
            o.h(str, "<this>");
            Matcher matcher = MediaType.f22044e.matcher(str);
            if (!matcher.lookingAt()) {
                throw new IllegalArgumentException(a.k('\"', "No subtype found for: \"", str).toString());
            }
            String strGroup = matcher.group(1);
            o.g(strGroup, "typeSubtype.group(1)");
            Locale locale = Locale.US;
            String strO = com.google.android.recaptcha.internal.a.o(locale, "US", strGroup, locale, "this as java.lang.String).toLowerCase(locale)");
            String strGroup2 = matcher.group(2);
            o.g(strGroup2, "typeSubtype.group(2)");
            o.g(strGroup2.toLowerCase(locale), "this as java.lang.String).toLowerCase(locale)");
            ArrayList arrayList = new ArrayList();
            Matcher matcher2 = MediaType.f22045f.matcher(str);
            int iEnd = matcher.end();
            while (iEnd < str.length()) {
                matcher2.region(iEnd, str.length());
                if (!matcher2.lookingAt()) {
                    StringBuilder sb = new StringBuilder("Parameter is not formatted correctly: \"");
                    String strSubstring = str.substring(iEnd);
                    o.g(strSubstring, "this as java.lang.String).substring(startIndex)");
                    sb.append(strSubstring);
                    sb.append("\" for: \"");
                    throw new IllegalArgumentException(Z0.o.r(sb, str, '\"').toString());
                }
                String strGroup3 = matcher2.group(1);
                if (strGroup3 == null) {
                    iEnd = matcher2.end();
                } else {
                    String strGroup4 = matcher2.group(2);
                    if (strGroup4 == null) {
                        strGroup4 = matcher2.group(3);
                    } else if (w.A0(strGroup4, "'", false) && w.s0(strGroup4, "'", false) && strGroup4.length() > 2) {
                        strGroup4 = strGroup4.substring(1, strGroup4.length() - 1);
                        o.g(strGroup4, "this as java.lang.String…ing(startIndex, endIndex)");
                    }
                    arrayList.add(strGroup3);
                    arrayList.add(strGroup4);
                    iEnd = matcher2.end();
                }
            }
            return new MediaType(str, strO, (String[]) arrayList.toArray(new String[0]));
        }

        public static MediaType b(String str) {
            o.h(str, "<this>");
            try {
                return a(str);
            } catch (IllegalArgumentException unused) {
                return null;
            }
        }

        private Companion() {
        }
    }

    public MediaType(String str, String str2, String[] strArr) {
        this.f22046a = str;
        this.f22047b = str2;
        this.f22048c = strArr;
    }

    public final Charset a(Charset charset) {
        String str;
        String[] strArr = this.f22048c;
        int i6 = 0;
        int iL = C.L(0, strArr.length - 1, 2);
        if (iL < 0) {
            str = null;
            break;
        }
        while (!w.t0(strArr[i6], "charset", true)) {
            if (i6 == iL) {
                str = null;
                break;
            }
            i6 += 2;
        }
        str = strArr[i6 + 1];
        if (str == null) {
            return charset;
        }
        try {
            return Charset.forName(str);
        } catch (IllegalArgumentException unused) {
            return charset;
        }
    }

    public final boolean equals(Object obj) {
        return (obj instanceof MediaType) && o.c(((MediaType) obj).f22046a, this.f22046a);
    }

    public final int hashCode() {
        return this.f22046a.hashCode();
    }

    public final String toString() {
        return this.f22046a;
    }
}
