package f;

import f.p;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;

/* JADX INFO: compiled from: RequestFactory.java */
/* JADX INFO: loaded from: classes.dex */
final class s {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final Method f1733a;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    final String f1735c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    @Nullable
    private final String f1736d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    @Nullable
    private final Headers f1737e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    @Nullable
    private final MediaType f1738f;
    private final boolean g;
    private final boolean h;
    private final boolean i;
    private final p<?>[] j;
    final boolean k;

    /* JADX INFO: compiled from: RequestFactory.java */
    static final class a {
        private static final Pattern x = Pattern.compile("\\{([a-zA-Z][a-zA-Z0-9_-]*)\\}");
        private static final Pattern y = Pattern.compile("[a-zA-Z][a-zA-Z0-9_-]*");

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final u f1739a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final Method f1740b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        final Annotation[] f1741c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        final Annotation[][] f1742d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        final Type[] f1743e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        boolean f1744f;
        boolean g;
        boolean h;
        boolean i;
        boolean j;
        boolean k;
        boolean l;
        boolean m;

        @Nullable
        String n;
        boolean o;
        boolean p;
        boolean q;

        @Nullable
        String r;

        @Nullable
        Headers s;

        @Nullable
        MediaType t;

        @Nullable
        Set<String> u;

        @Nullable
        p<?>[] v;
        boolean w;

        a(u uVar, Method method) {
            this.f1739a = uVar;
            this.f1740b = method;
            this.f1741c = method.getAnnotations();
            this.f1743e = method.getGenericParameterTypes();
            this.f1742d = method.getParameterAnnotations();
        }

        private static Class<?> a(Class<?> cls) {
            return Boolean.TYPE == cls ? Boolean.class : Byte.TYPE == cls ? Byte.class : Character.TYPE == cls ? Character.class : Double.TYPE == cls ? Double.class : Float.TYPE == cls ? Float.class : Integer.TYPE == cls ? Integer.class : Long.TYPE == cls ? Long.class : Short.TYPE == cls ? Short.class : cls;
        }

        private Headers c(String[] strArr) {
            Headers.Builder builder = new Headers.Builder();
            for (String str : strArr) {
                int iIndexOf = str.indexOf(58);
                if (iIndexOf == -1 || iIndexOf == 0 || iIndexOf == str.length() - 1) {
                    throw y.m(this.f1740b, "@Headers value must be in the form \"Name: Value\". Found: \"%s\"", str);
                }
                String strSubstring = str.substring(0, iIndexOf);
                String strTrim = str.substring(iIndexOf + 1).trim();
                if ("Content-Type".equalsIgnoreCase(strSubstring)) {
                    try {
                        this.t = MediaType.get(strTrim);
                    } catch (IllegalArgumentException e2) {
                        throw y.n(this.f1740b, e2, "Malformed content type: %s", strTrim);
                    }
                } else {
                    builder.add(strSubstring, strTrim);
                }
            }
            return builder.build();
        }

        private void d(String str, String str2, boolean z) {
            String str3 = this.n;
            if (str3 != null) {
                throw y.m(this.f1740b, "Only one HTTP method is allowed. Found: %s and %s.", str3, str);
            }
            this.n = str;
            this.o = z;
            if (str2.isEmpty()) {
                return;
            }
            int iIndexOf = str2.indexOf(63);
            if (iIndexOf != -1 && iIndexOf < str2.length() - 1) {
                String strSubstring = str2.substring(iIndexOf + 1);
                if (x.matcher(strSubstring).find()) {
                    throw y.m(this.f1740b, "URL query string \"%s\" must not have replace block. For dynamic query parameters use @Query.", strSubstring);
                }
            }
            this.r = str2;
            this.u = h(str2);
        }

        private void e(Annotation annotation) {
            if (annotation instanceof f.b0.b) {
                d("DELETE", ((f.b0.b) annotation).value(), false);
                return;
            }
            if (annotation instanceof f.b0.e) {
                d("GET", ((f.b0.e) annotation).value(), false);
                return;
            }
            if (annotation instanceof f.b0.f) {
                d("HEAD", ((f.b0.f) annotation).value(), false);
                return;
            }
            if (annotation instanceof f.b0.k) {
                d("PATCH", ((f.b0.k) annotation).value(), true);
                return;
            }
            if (annotation instanceof f.b0.l) {
                d("POST", ((f.b0.l) annotation).value(), true);
                return;
            }
            if (annotation instanceof f.b0.m) {
                d("PUT", ((f.b0.m) annotation).value(), true);
                return;
            }
            if (annotation instanceof f.b0.j) {
                d("OPTIONS", ((f.b0.j) annotation).value(), false);
                return;
            }
            if (annotation instanceof f.b0.g) {
                f.b0.g gVar = (f.b0.g) annotation;
                d(gVar.method(), gVar.path(), gVar.hasBody());
            } else if (annotation instanceof f.b0.i) {
                String[] strArrValue = ((f.b0.i) annotation).value();
                if (strArrValue.length == 0) {
                    throw y.m(this.f1740b, "@Headers annotation is empty.", new Object[0]);
                }
                this.s = c(strArrValue);
            }
        }

        @Nullable
        private p<?> f(int i, Type type, @Nullable Annotation[] annotationArr, boolean z) {
            p<?> pVar;
            if (annotationArr != null) {
                pVar = null;
                for (Annotation annotation : annotationArr) {
                    p<?> pVarG = g(i, type, annotationArr, annotation);
                    if (pVarG != null) {
                        if (pVar != null) {
                            throw y.o(this.f1740b, i, "Multiple Retrofit annotations found, only one allowed.", new Object[0]);
                        }
                        pVar = pVarG;
                    }
                }
            } else {
                pVar = null;
            }
            if (pVar != null) {
                return pVar;
            }
            if (z) {
                try {
                    if (y.h(type) == c.n.a.class) {
                        this.w = true;
                        return null;
                    }
                } catch (NoClassDefFoundError unused) {
                }
            }
            throw y.o(this.f1740b, i, "No Retrofit annotation found.", new Object[0]);
        }

        @Nullable
        private p<?> g(int i, Type type, Annotation[] annotationArr, Annotation annotation) {
            if (annotation instanceof f.b0.p) {
                j(i, type);
                if (this.j) {
                    throw y.o(this.f1740b, i, "A @Path parameter must not come after a @Query.", new Object[0]);
                }
                if (this.k) {
                    throw y.o(this.f1740b, i, "A @Path parameter must not come after a @QueryName.", new Object[0]);
                }
                if (this.l) {
                    throw y.o(this.f1740b, i, "A @Path parameter must not come after a @QueryMap.", new Object[0]);
                }
                if (this.m) {
                    throw y.o(this.f1740b, i, "@Path parameters may not be used with @Url.", new Object[0]);
                }
                if (this.r == null) {
                    throw y.o(this.f1740b, i, "@Path can only be used with relative url on @%s", this.n);
                }
                this.i = true;
                f.b0.p pVar = (f.b0.p) annotation;
                String strValue = pVar.value();
                i(i, strValue);
                return new p.i(this.f1740b, i, strValue, this.f1739a.i(type, annotationArr), pVar.encoded());
            }
            if (annotation instanceof f.b0.q) {
                j(i, type);
                f.b0.q qVar = (f.b0.q) annotation;
                String strValue2 = qVar.value();
                boolean zEncoded = qVar.encoded();
                Class<?> clsH = y.h(type);
                this.j = true;
                if (!Iterable.class.isAssignableFrom(clsH)) {
                    return clsH.isArray() ? new p.j(strValue2, this.f1739a.i(a(clsH.getComponentType()), annotationArr), zEncoded).b() : new p.j(strValue2, this.f1739a.i(type, annotationArr), zEncoded);
                }
                if (type instanceof ParameterizedType) {
                    return new p.j(strValue2, this.f1739a.i(y.g(0, (ParameterizedType) type), annotationArr), zEncoded).c();
                }
                throw y.o(this.f1740b, i, clsH.getSimpleName() + " must include generic type (e.g., " + clsH.getSimpleName() + "<String>)", new Object[0]);
            }
            if (annotation instanceof f.b0.s) {
                j(i, type);
                boolean zEncoded2 = ((f.b0.s) annotation).encoded();
                Class<?> clsH2 = y.h(type);
                this.k = true;
                if (!Iterable.class.isAssignableFrom(clsH2)) {
                    return clsH2.isArray() ? new p.l(this.f1739a.i(a(clsH2.getComponentType()), annotationArr), zEncoded2).b() : new p.l(this.f1739a.i(type, annotationArr), zEncoded2);
                }
                if (type instanceof ParameterizedType) {
                    return new p.l(this.f1739a.i(y.g(0, (ParameterizedType) type), annotationArr), zEncoded2).c();
                }
                throw y.o(this.f1740b, i, clsH2.getSimpleName() + " must include generic type (e.g., " + clsH2.getSimpleName() + "<String>)", new Object[0]);
            }
            if (annotation instanceof f.b0.r) {
                j(i, type);
                Class<?> clsH3 = y.h(type);
                this.l = true;
                if (!Map.class.isAssignableFrom(clsH3)) {
                    throw y.o(this.f1740b, i, "@QueryMap parameter type must be Map.", new Object[0]);
                }
                Type typeI = y.i(type, clsH3, Map.class);
                if (!(typeI instanceof ParameterizedType)) {
                    throw y.o(this.f1740b, i, "Map must include generic types (e.g., Map<String, String>)", new Object[0]);
                }
                ParameterizedType parameterizedType = (ParameterizedType) typeI;
                Type typeG = y.g(0, parameterizedType);
                if (String.class == typeG) {
                    return new p.k(this.f1740b, i, this.f1739a.i(y.g(1, parameterizedType), annotationArr), ((f.b0.r) annotation).encoded());
                }
                throw y.o(this.f1740b, i, "@QueryMap keys must be of type String: " + typeG, new Object[0]);
            }
            if (annotation instanceof f.b0.h) {
                j(i, type);
                String strValue3 = ((f.b0.h) annotation).value();
                Class<?> clsH4 = y.h(type);
                if (!Iterable.class.isAssignableFrom(clsH4)) {
                    return clsH4.isArray() ? new p.f(strValue3, this.f1739a.i(a(clsH4.getComponentType()), annotationArr)).b() : new p.f(strValue3, this.f1739a.i(type, annotationArr));
                }
                if (type instanceof ParameterizedType) {
                    return new p.f(strValue3, this.f1739a.i(y.g(0, (ParameterizedType) type), annotationArr)).c();
                }
                throw y.o(this.f1740b, i, clsH4.getSimpleName() + " must include generic type (e.g., " + clsH4.getSimpleName() + "<String>)", new Object[0]);
            }
            if (annotation instanceof f.b0.c) {
                j(i, type);
                if (!this.p) {
                    throw y.o(this.f1740b, i, "@Field parameters can only be used with form encoding.", new Object[0]);
                }
                f.b0.c cVar = (f.b0.c) annotation;
                String strValue4 = cVar.value();
                boolean zEncoded3 = cVar.encoded();
                this.f1744f = true;
                Class<?> clsH5 = y.h(type);
                if (!Iterable.class.isAssignableFrom(clsH5)) {
                    return clsH5.isArray() ? new p.d(strValue4, this.f1739a.i(a(clsH5.getComponentType()), annotationArr), zEncoded3).b() : new p.d(strValue4, this.f1739a.i(type, annotationArr), zEncoded3);
                }
                if (type instanceof ParameterizedType) {
                    return new p.d(strValue4, this.f1739a.i(y.g(0, (ParameterizedType) type), annotationArr), zEncoded3).c();
                }
                throw y.o(this.f1740b, i, clsH5.getSimpleName() + " must include generic type (e.g., " + clsH5.getSimpleName() + "<String>)", new Object[0]);
            }
            if (annotation instanceof f.b0.d) {
                j(i, type);
                if (!this.p) {
                    throw y.o(this.f1740b, i, "@FieldMap parameters can only be used with form encoding.", new Object[0]);
                }
                Class<?> clsH6 = y.h(type);
                if (!Map.class.isAssignableFrom(clsH6)) {
                    throw y.o(this.f1740b, i, "@FieldMap parameter type must be Map.", new Object[0]);
                }
                Type typeI2 = y.i(type, clsH6, Map.class);
                if (!(typeI2 instanceof ParameterizedType)) {
                    throw y.o(this.f1740b, i, "Map must include generic types (e.g., Map<String, String>)", new Object[0]);
                }
                ParameterizedType parameterizedType2 = (ParameterizedType) typeI2;
                Type typeG2 = y.g(0, parameterizedType2);
                if (String.class == typeG2) {
                    h hVarI = this.f1739a.i(y.g(1, parameterizedType2), annotationArr);
                    this.f1744f = true;
                    return new p.e(this.f1740b, i, hVarI, ((f.b0.d) annotation).encoded());
                }
                throw y.o(this.f1740b, i, "@FieldMap keys must be of type String: " + typeG2, new Object[0]);
            }
            if (!(annotation instanceof f.b0.n)) {
                if (!(annotation instanceof f.b0.o)) {
                    if (!(annotation instanceof f.b0.a)) {
                        return null;
                    }
                    j(i, type);
                    if (this.p || this.q) {
                        throw y.o(this.f1740b, i, "@Body parameters cannot be used with form or multi-part encoding.", new Object[0]);
                    }
                    if (this.h) {
                        throw y.o(this.f1740b, i, "Multiple @Body method annotations found.", new Object[0]);
                    }
                    try {
                        h hVarG = this.f1739a.g(type, annotationArr, this.f1741c);
                        this.h = true;
                        return new p.c(this.f1740b, i, hVarG);
                    } catch (RuntimeException e2) {
                        throw y.p(this.f1740b, e2, i, "Unable to create @Body converter for %s", type);
                    }
                }
                j(i, type);
                if (!this.q) {
                    throw y.o(this.f1740b, i, "@PartMap parameters can only be used with multipart encoding.", new Object[0]);
                }
                this.g = true;
                Class<?> clsH7 = y.h(type);
                if (!Map.class.isAssignableFrom(clsH7)) {
                    throw y.o(this.f1740b, i, "@PartMap parameter type must be Map.", new Object[0]);
                }
                Type typeI3 = y.i(type, clsH7, Map.class);
                if (!(typeI3 instanceof ParameterizedType)) {
                    throw y.o(this.f1740b, i, "Map must include generic types (e.g., Map<String, String>)", new Object[0]);
                }
                ParameterizedType parameterizedType3 = (ParameterizedType) typeI3;
                Type typeG3 = y.g(0, parameterizedType3);
                if (String.class == typeG3) {
                    Type typeG4 = y.g(1, parameterizedType3);
                    if (MultipartBody.Part.class.isAssignableFrom(y.h(typeG4))) {
                        throw y.o(this.f1740b, i, "@PartMap values cannot be MultipartBody.Part. Use @Part List<Part> or a different value type instead.", new Object[0]);
                    }
                    return new p.h(this.f1740b, i, this.f1739a.g(typeG4, annotationArr, this.f1741c), ((f.b0.o) annotation).encoding());
                }
                throw y.o(this.f1740b, i, "@PartMap keys must be of type String: " + typeG3, new Object[0]);
            }
            j(i, type);
            if (!this.q) {
                throw y.o(this.f1740b, i, "@Part parameters can only be used with multipart encoding.", new Object[0]);
            }
            f.b0.n nVar = (f.b0.n) annotation;
            this.g = true;
            String strValue5 = nVar.value();
            Class<?> clsH8 = y.h(type);
            if (strValue5.isEmpty()) {
                if (!Iterable.class.isAssignableFrom(clsH8)) {
                    if (clsH8.isArray()) {
                        if (MultipartBody.Part.class.isAssignableFrom(clsH8.getComponentType())) {
                            return p.m.f1720a.b();
                        }
                        throw y.o(this.f1740b, i, "@Part annotation must supply a name or use MultipartBody.Part parameter type.", new Object[0]);
                    }
                    if (MultipartBody.Part.class.isAssignableFrom(clsH8)) {
                        return p.m.f1720a;
                    }
                    throw y.o(this.f1740b, i, "@Part annotation must supply a name or use MultipartBody.Part parameter type.", new Object[0]);
                }
                if (type instanceof ParameterizedType) {
                    if (MultipartBody.Part.class.isAssignableFrom(y.h(y.g(0, (ParameterizedType) type)))) {
                        return p.m.f1720a.c();
                    }
                    throw y.o(this.f1740b, i, "@Part annotation must supply a name or use MultipartBody.Part parameter type.", new Object[0]);
                }
                throw y.o(this.f1740b, i, clsH8.getSimpleName() + " must include generic type (e.g., " + clsH8.getSimpleName() + "<String>)", new Object[0]);
            }
            Headers headersOf = Headers.of("Content-Disposition", "form-data; name=\"" + strValue5 + "\"", "Content-Transfer-Encoding", nVar.encoding());
            if (!Iterable.class.isAssignableFrom(clsH8)) {
                if (!clsH8.isArray()) {
                    if (MultipartBody.Part.class.isAssignableFrom(clsH8)) {
                        throw y.o(this.f1740b, i, "@Part parameters using the MultipartBody.Part must not include a part name in the annotation.", new Object[0]);
                    }
                    return new p.g(this.f1740b, i, headersOf, this.f1739a.g(type, annotationArr, this.f1741c));
                }
                Class<?> clsA = a(clsH8.getComponentType());
                if (MultipartBody.Part.class.isAssignableFrom(clsA)) {
                    throw y.o(this.f1740b, i, "@Part parameters using the MultipartBody.Part must not include a part name in the annotation.", new Object[0]);
                }
                return new p.g(this.f1740b, i, headersOf, this.f1739a.g(clsA, annotationArr, this.f1741c)).b();
            }
            if (type instanceof ParameterizedType) {
                Type typeG5 = y.g(0, (ParameterizedType) type);
                if (MultipartBody.Part.class.isAssignableFrom(y.h(typeG5))) {
                    throw y.o(this.f1740b, i, "@Part parameters using the MultipartBody.Part must not include a part name in the annotation.", new Object[0]);
                }
                return new p.g(this.f1740b, i, headersOf, this.f1739a.g(typeG5, annotationArr, this.f1741c)).c();
            }
            throw y.o(this.f1740b, i, clsH8.getSimpleName() + " must include generic type (e.g., " + clsH8.getSimpleName() + "<String>)", new Object[0]);
        }

        static Set<String> h(String str) {
            Matcher matcher = x.matcher(str);
            LinkedHashSet linkedHashSet = new LinkedHashSet();
            while (matcher.find()) {
                linkedHashSet.add(matcher.group(1));
            }
            return linkedHashSet;
        }

        private void i(int i, String str) {
            if (!y.matcher(str).matches()) {
                throw y.o(this.f1740b, i, "@Path parameter name must match %s. Found: %s", x.pattern(), str);
            }
            if (!this.u.contains(str)) {
                throw y.o(this.f1740b, i, "URL \"%s\" does not contain \"{%s}\".", this.r, str);
            }
        }

        private void j(int i, Type type) {
            if (y.j(type)) {
                throw y.o(this.f1740b, i, "Parameter type must not include a type variable or wildcard: %s", type);
            }
        }

        s b() {
            for (Annotation annotation : this.f1741c) {
                e(annotation);
            }
            if (this.n == null) {
                throw y.m(this.f1740b, "HTTP method annotation is required (e.g., @GET, @POST, etc.).", new Object[0]);
            }
            if (!this.o) {
                if (this.q) {
                    throw y.m(this.f1740b, "Multipart can only be specified on HTTP methods with request body (e.g., @POST).", new Object[0]);
                }
                if (this.p) {
                    throw y.m(this.f1740b, "FormUrlEncoded can only be specified on HTTP methods with request body (e.g., @POST).", new Object[0]);
                }
            }
            int length = this.f1742d.length;
            this.v = new p[length];
            int i = length - 1;
            int i2 = 0;
            while (true) {
                boolean z = true;
                if (i2 >= length) {
                    break;
                }
                p<?>[] pVarArr = this.v;
                Type type = this.f1743e[i2];
                Annotation[] annotationArr = this.f1742d[i2];
                if (i2 != i) {
                    z = false;
                }
                pVarArr[i2] = f(i2, type, annotationArr, z);
                i2++;
            }
            if (this.r == null && !this.m) {
                throw y.m(this.f1740b, "Missing either @%s URL or @Url parameter.", this.n);
            }
            if (!this.p && !this.q && !this.o && this.h) {
                throw y.m(this.f1740b, "Non-body HTTP method cannot contain @Body.", new Object[0]);
            }
            if (this.p && !this.f1744f) {
                throw y.m(this.f1740b, "Form-encoded method must contain at least one @Field.", new Object[0]);
            }
            if (!this.q || this.g) {
                return new s(this);
            }
            throw y.m(this.f1740b, "Multipart method must contain at least one @Part.", new Object[0]);
        }
    }

    s(a aVar) {
        this.f1733a = aVar.f1740b;
        this.f1734b = aVar.f1739a.f1749c;
        this.f1735c = aVar.n;
        this.f1736d = aVar.r;
        this.f1737e = aVar.s;
        this.f1738f = aVar.t;
        this.g = aVar.o;
        this.h = aVar.p;
        this.i = aVar.q;
        this.j = aVar.v;
        this.k = aVar.w;
    }

    static s b(u uVar, Method method) {
        return new a(uVar, method).b();
    }

    Request a(Object[] objArr) throws IOException {
        p<?>[] pVarArr = this.j;
        int length = objArr.length;
        if (length != pVarArr.length) {
            throw new IllegalArgumentException("Argument count (" + length + ") doesn't match expected count (" + pVarArr.length + ")");
        }
        r rVar = new r(this.f1735c, this.f1734b, this.f1736d, this.f1737e, this.f1738f, this.g, this.h, this.i);
        if (this.k) {
            length--;
        }
        ArrayList arrayList = new ArrayList(length);
        for (int i = 0; i < length; i++) {
            arrayList.add(objArr[i]);
            pVarArr[i].a(rVar, objArr[i]);
        }
        return rVar.i().tag(l.class, new l(this.f1733a, arrayList)).build();
    }
}
