package com.amplifyframework.api.aws.utils;

import android.net.Uri;
import com.amplifyframework.api.rest.HttpMethod;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Objects;
import uq.b0;
import uq.c0;
import uq.u;
import uq.v;

/* loaded from: classes4.dex */
public final class RestRequestFactory {

    /* renamed from: com.amplifyframework.api.aws.utils.RestRequestFactory$1, reason: invalid class name */
    /* loaded from: classes4.dex */
    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$amplifyframework$api$rest$HttpMethod;

        static {
            int[] iArr = new int[HttpMethod.values().length];
            $SwitchMap$com$amplifyframework$api$rest$HttpMethod = iArr;
            try {
                iArr[HttpMethod.GET.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$com$amplifyframework$api$rest$HttpMethod[HttpMethod.PUT.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$com$amplifyframework$api$rest$HttpMethod[HttpMethod.POST.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$com$amplifyframework$api$rest$HttpMethod[HttpMethod.HEAD.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$com$amplifyframework$api$rest$HttpMethod[HttpMethod.PATCH.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                $SwitchMap$com$amplifyframework$api$rest$HttpMethod[HttpMethod.DELETE.ordinal()] = 6;
            } catch (NoSuchFieldError unused6) {
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public interface BodyCreationStrategy {
        void buildRequest(b0.a aVar, byte[] bArr);
    }

    private RestRequestFactory() {
    }

    public static b0 createRequest(URL url, byte[] bArr, Map<String, String> map, HttpMethod httpMethod) {
        Objects.requireNonNull(url);
        Objects.requireNonNull(httpMethod);
        b0.a z10 = new b0.a().z(url);
        switch (AnonymousClass1.$SwitchMap$com$amplifyframework$api$rest$HttpMethod[httpMethod.ordinal()]) {
            case 1:
                z10.f();
                break;
            case 2:
                populateBody(z10, bArr, new BodyCreationStrategy() { // from class: com.amplifyframework.api.aws.utils.a
                    @Override // com.amplifyframework.api.aws.utils.RestRequestFactory.BodyCreationStrategy
                    public final void buildRequest(b0.a aVar, byte[] bArr2) {
                        RestRequestFactory.lambda$createRequest$0(aVar, bArr2);
                    }
                });
                break;
            case 3:
                populateBody(z10, bArr, new BodyCreationStrategy() { // from class: com.amplifyframework.api.aws.utils.b
                    @Override // com.amplifyframework.api.aws.utils.RestRequestFactory.BodyCreationStrategy
                    public final void buildRequest(b0.a aVar, byte[] bArr2) {
                        RestRequestFactory.lambda$createRequest$1(aVar, bArr2);
                    }
                });
                break;
            case 4:
                z10.l();
                break;
            case 5:
                populateBody(z10, bArr, new BodyCreationStrategy() { // from class: com.amplifyframework.api.aws.utils.c
                    @Override // com.amplifyframework.api.aws.utils.RestRequestFactory.BodyCreationStrategy
                    public final void buildRequest(b0.a aVar, byte[] bArr2) {
                        RestRequestFactory.lambda$createRequest$2(aVar, bArr2);
                    }
                });
                break;
            case 6:
                if (bArr == null) {
                    z10.c();
                    break;
                } else {
                    populateBody(z10, bArr, new BodyCreationStrategy() { // from class: com.amplifyframework.api.aws.utils.d
                        @Override // com.amplifyframework.api.aws.utils.RestRequestFactory.BodyCreationStrategy
                        public final void buildRequest(b0.a aVar, byte[] bArr2) {
                            RestRequestFactory.lambda$createRequest$3(aVar, bArr2);
                        }
                    });
                    break;
                }
        }
        if (map != null) {
            z10.n(u.x(map));
        }
        return z10.b();
    }

    public static URL createURL(String str, String str2, Map<String, String> map) throws MalformedURLException {
        URL url = new URL(str);
        v.a b10 = new v.a().v(url.getProtocol()).k(url.getHost()).b(stripLeadingSlashes(url.getPath()));
        if (url.getPort() != -1) {
            b10.q(url.getPort());
        }
        if (str2 != null) {
            b10.c(stripLeadingSlashes(str2));
        }
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                b10.e(entry.getKey(), entry.getValue());
            }
        }
        try {
            return new URL(Uri.decode(b10.f().u().toString()));
        } catch (MalformedURLException e10) {
            throw new MalformedURLException(e10.getMessage());
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$createRequest$0(b0.a aVar, byte[] bArr) {
        aVar.r(c0.g(bArr));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$createRequest$1(b0.a aVar, byte[] bArr) {
        aVar.q(c0.g(bArr));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$createRequest$2(b0.a aVar, byte[] bArr) {
        aVar.p(c0.g(bArr));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$createRequest$3(b0.a aVar, byte[] bArr) {
        aVar.d(c0.g(bArr));
    }

    private static void populateBody(b0.a aVar, byte[] bArr, BodyCreationStrategy bodyCreationStrategy) {
        if (bArr != null) {
            bodyCreationStrategy.buildRequest(aVar, bArr);
        }
    }

    private static String stripLeadingSlashes(String str) {
        return str.replaceAll("^[\\\\/]+", "");
    }
}
