package com.amplifyframework.api.aws;

import com.amplifyframework.AmplifyException;
import com.amplifyframework.api.ApiException;
import com.amplifyframework.api.graphql.GraphQLRequest;
import com.amplifyframework.api.graphql.GraphQLResponse;
import com.amplifyframework.api.graphql.PaginatedResult;
import com.amplifyframework.core.model.ModelReference;
import com.amplifyframework.util.Empty;
import com.amplifyframework.util.TypeMaker;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;

/* loaded from: classes4.dex */
final class GsonGraphQLResponseFactory implements GraphQLResponse.Factory {
    private final com.google.gson.d gson;
    private final AWSApiSchemaRegistry schemaRegistry;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static final class IterableDeserializer<R> implements com.google.gson.h {
        private static final String ITEMS_KEY = "items";
        private static final String NEXT_TOKEN_KEY = "nextToken";
        private final GraphQLRequest<R> request;

        IterableDeserializer(GraphQLRequest<R> graphQLRequest) {
            this.request = graphQLRequest;
        }

        private PaginatedResult<Object> buildPaginatedResult(Iterable<Object> iterable, com.google.gson.i iVar) {
            AppSyncGraphQLRequest<R> appSyncGraphQLRequest = null;
            if (iVar != null && iVar.q()) {
                String l10 = iVar.i().l();
                try {
                    GraphQLRequest<R> graphQLRequest = this.request;
                    if (graphQLRequest instanceof AppSyncGraphQLRequest) {
                        appSyncGraphQLRequest = ((AppSyncGraphQLRequest) graphQLRequest).newBuilder().variable("nextToken", "String", l10).build();
                    }
                } catch (AmplifyException e10) {
                    throw new com.google.gson.m("Failed to create requestForNextPage with nextToken variable", e10);
                }
            }
            return new PaginatedResult<>(iterable, appSyncGraphQLRequest);
        }

        private Iterable<Object> toList(com.google.gson.f fVar, Type type, com.google.gson.g gVar) {
            ArrayList arrayList = new ArrayList();
            Iterator it = fVar.iterator();
            while (it.hasNext()) {
                arrayList.add(gVar.a((com.google.gson.i) it.next(), type));
            }
            return arrayList;
        }

        @Override // com.google.gson.h
        public Iterable<Object> deserialize(com.google.gson.i iVar, Type type, com.google.gson.g gVar) throws com.google.gson.m {
            if (type instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) type;
                Type type2 = parameterizedType.getActualTypeArguments()[0];
                if (iVar.o()) {
                    com.google.gson.l g10 = iVar.g();
                    if (g10.B("items") && g10.v("items").m()) {
                        Iterable<Object> list = toList(g10.v("items").e(), type2, gVar);
                        return PaginatedResult.class.equals(parameterizedType.getRawType()) ? buildPaginatedResult(list, g10.v("nextToken")) : list;
                    }
                    throw new com.google.gson.m("Got JSON from an API call which was supposed to go with a List but is in the form of an object rather than an array. It also is not in the standard format of having an items property with the actual array of data so we do not know how to deserialize it.");
                }
                if (iVar.m()) {
                    return toList(iVar.e(), type2, gVar);
                }
                throw new com.google.gson.m("Got a JSON value that was not an object or a list. Refusing to deserialize into a Java Iterable.");
            }
            throw new com.google.gson.m("Expected a parameterized type during list deserialization.");
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public GsonGraphQLResponseFactory() {
        this(GsonFactory.instance());
    }

    public <T> GraphQLResponse<T> buildResponse(GraphQLRequest<T> graphQLRequest, String str, String str2) throws ApiException {
        if (!Empty.check(str)) {
            try {
                return (GraphQLResponse) this.gson.q().e(Iterable.class, new IterableDeserializer(graphQLRequest)).c(ModelReference.class, new ModelReferenceDeserializer(str2, this.schemaRegistry)).d(new ModelPostProcessingTypeAdapter(str2, this.schemaRegistry)).b().k(str, TypeMaker.getParameterizedType(GraphQLResponse.class, graphQLRequest.getResponseType()));
            } catch (com.google.gson.m e10) {
                throw new ApiException("Amplify encountered an error while deserializing an object.", e10, "Sorry, we don’t have a recovery suggestion for this error.");
            }
        }
        throw new ApiException("Amplify encountered an error while deserializing an object.", new com.google.gson.m("Empty response."), "Sorry, we don’t have a recovery suggestion for this error.");
    }

    GsonGraphQLResponseFactory(com.google.gson.d dVar) {
        this.schemaRegistry = new AWSApiSchemaRegistry();
        this.gson = dVar;
    }

    @Override // com.amplifyframework.api.graphql.GraphQLResponse.Factory
    @Deprecated
    public <T> GraphQLResponse<T> buildResponse(GraphQLRequest<T> graphQLRequest, String str) throws ApiException {
        return buildResponse(graphQLRequest, str, null);
    }
}
