package com.amplifyframework.api.aws;

import com.amplifyframework.core.model.LoadedModelReferenceImpl;
import com.amplifyframework.core.model.Model;
import com.amplifyframework.core.model.ModelReference;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.List;
import kotlin.Metadata;
import sm.p0;

@Metadata(d1 = {"\u00006\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0005\b\u0000\u0018\u0000*\b\b\u0000\u0010\u0002*\u00020\u00012\u000e\u0012\n\u0012\b\u0012\u0004\u0012\u00028\u00000\u00040\u0003B\u0019\u0012\b\u0010\r\u001a\u0004\u0018\u00010\f\u0012\u0006\u0010\u0012\u001a\u00020\u0011¢\u0006\u0004\b\u0014\u0010\u0015J&\u0010\u000b\u001a\b\u0012\u0004\u0012\u00028\u00000\u00042\u0006\u0010\u0006\u001a\u00020\u00052\u0006\u0010\b\u001a\u00020\u00072\u0006\u0010\n\u001a\u00020\tH\u0016R\u0019\u0010\r\u001a\u0004\u0018\u00010\f8\u0006¢\u0006\f\n\u0004\b\r\u0010\u000e\u001a\u0004\b\u000f\u0010\u0010R\u0014\u0010\u0012\u001a\u00020\u00118\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0012\u0010\u0013¨\u0006\u0016"}, d2 = {"Lcom/amplifyframework/api/aws/ModelReferenceDeserializer;", "Lcom/amplifyframework/core/model/Model;", "M", "Lcom/google/gson/h;", "Lcom/amplifyframework/core/model/ModelReference;", "Lcom/google/gson/i;", "json", "Ljava/lang/reflect/Type;", "typeOfT", "Lcom/google/gson/g;", "context", "deserialize", "", "apiName", "Ljava/lang/String;", "getApiName", "()Ljava/lang/String;", "Lcom/amplifyframework/api/aws/AWSApiSchemaRegistry;", "schemaRegistry", "Lcom/amplifyframework/api/aws/AWSApiSchemaRegistry;", "<init>", "(Ljava/lang/String;Lcom/amplifyframework/api/aws/AWSApiSchemaRegistry;)V", "aws-api_release"}, k = 1, mv = {1, 9, 0})
/* loaded from: classes4.dex */
public final class ModelReferenceDeserializer<M extends Model> implements com.google.gson.h {
    private final String apiName;
    private final AWSApiSchemaRegistry schemaRegistry;

    public ModelReferenceDeserializer(String str, AWSApiSchemaRegistry schemaRegistry) {
        kotlin.jvm.internal.t.g(schemaRegistry, "schemaRegistry");
        this.apiName = str;
        this.schemaRegistry = schemaRegistry;
    }

    public final String getApiName() {
        return this.apiName;
    }

    @Override // com.google.gson.h
    public ModelReference<M> deserialize(com.google.gson.i json, Type typeOfT, com.google.gson.g context) throws com.google.gson.m {
        com.google.gson.l jsonObject;
        int v10;
        int d10;
        int d11;
        kotlin.jvm.internal.t.g(json, "json");
        kotlin.jvm.internal.t.g(typeOfT, "typeOfT");
        kotlin.jvm.internal.t.g(context, "context");
        ParameterizedType parameterizedType = typeOfT instanceof ParameterizedType ? (ParameterizedType) typeOfT : null;
        if (parameterizedType != null) {
            Type type = parameterizedType.getActualTypeArguments()[0];
            kotlin.jvm.internal.t.e(type, "null cannot be cast to non-null type java.lang.Class<M of com.amplifyframework.api.aws.ModelReferenceDeserializer>");
            Class cls = (Class) type;
            jsonObject = LazyTypeDeserializersKt.getJsonObject(json);
            List<String> primaryIndexFields = this.schemaRegistry.getModelSchemaForModelClass(cls).getPrimaryIndexFields();
            kotlin.jvm.internal.t.f(primaryIndexFields, "getPrimaryIndexFields(...)");
            List<String> list = primaryIndexFields;
            v10 = sm.v.v(list, 10);
            d10 = p0.d(v10);
            d11 = jn.o.d(d10, 16);
            LinkedHashMap linkedHashMap = new LinkedHashMap(d11);
            for (Object obj : list) {
                linkedHashMap.put(obj, jsonObject.v((String) obj));
            }
            if (jsonObject.size() > linkedHashMap.size()) {
                try {
                    return new LoadedModelReferenceImpl((Model) context.a(json, cls));
                } catch (Exception unused) {
                }
            }
            return new ApiLazyModelReference(cls, linkedHashMap, this.apiName, null, 8, null);
        }
        throw new com.google.gson.m("Expected a parameterized type during list deserialization.");
    }
}
