package com.amplifyframework.core.model;

import com.amplifyframework.AmplifyException;
import com.amplifyframework.core.model.ModelIdentifier;
import com.amplifyframework.util.Immutable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/* loaded from: classes4.dex */
public final class SerializedModel implements Model {
    private final Serializable modelId;
    private final ModelSchema modelSchema;
    private final Map<String, Object> serializedData;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes4.dex */
    public static final class Builder implements BuilderSteps.SerializedDataStep, BuilderSteps.ModelSchemaStep, BuilderSteps.BuildStep {

        /* renamed from: id, reason: collision with root package name */
        private String f12460id;
        private ModelSchema modelSchema;
        private final Map<String, Object> serializedData;

        @Override // com.amplifyframework.core.model.SerializedModel.BuilderSteps.BuildStep
        public SerializedModel build() {
            return new SerializedModel(this.f12460id, Immutable.of(this.serializedData), this.modelSchema);
        }

        @Override // com.amplifyframework.core.model.SerializedModel.BuilderSteps.ModelSchemaStep
        public BuilderSteps.SerializedDataStep modelSchema(ModelSchema modelSchema) {
            this.modelSchema = modelSchema;
            return this;
        }

        @Override // com.amplifyframework.core.model.SerializedModel.BuilderSteps.SerializedDataStep
        public BuilderSteps.BuildStep serializedData(Map<String, Object> map) {
            Map<String, Object> map2 = this.serializedData;
            Objects.requireNonNull(map);
            map2.putAll(map);
            this.f12460id = ModelIdentifier.Helper.getUniqueKey(this.modelSchema, map);
            return this;
        }

        private Builder() {
            this.serializedData = new HashMap();
        }
    }

    /* loaded from: classes4.dex */
    public interface BuilderSteps {

        /* loaded from: classes4.dex */
        public interface BuildStep {
            SerializedModel build();
        }

        /* loaded from: classes4.dex */
        public interface ModelSchemaStep {
            SerializedDataStep modelSchema(ModelSchema modelSchema);
        }

        /* loaded from: classes4.dex */
        public interface SerializedDataStep {
            BuildStep serializedData(Map<String, Object> map);
        }
    }

    public static BuilderSteps.ModelSchemaStep builder() {
        return new Builder();
    }

    public static <T extends Model> SerializedModel create(T t10, ModelSchema modelSchema) throws AmplifyException {
        return builder().modelSchema(modelSchema).serializedData(ModelConverter.toMap(t10, modelSchema)).build();
    }

    public static <T extends Model> SerializedModel difference(T t10, T t11, ModelSchema modelSchema) throws AmplifyException {
        if (t11 == null) {
            return create(t10, modelSchema);
        }
        Map<String, Object> map = ModelConverter.toMap(t10, modelSchema);
        Map<String, Object> map2 = ModelConverter.toMap(t11, modelSchema);
        HashMap hashMap = new HashMap();
        for (String str : map.keySet()) {
            HashSet hashSet = new HashSet();
            hashSet.add(PrimaryKey.fieldName());
            hashSet.addAll(modelSchema.getPrimaryIndexFields());
            if (hashSet.contains(str) || !s3.d.a(map2.get(str), map.get(str))) {
                hashMap.put(str, map.get(str));
            }
        }
        return builder().modelSchema(modelSchema).serializedData(hashMap).build();
    }

    public static SerializedModel merge(SerializedModel serializedModel, SerializedModel serializedModel2, ModelSchema modelSchema) {
        HashMap hashMap = new HashMap(serializedModel.serializedData);
        for (String str : serializedModel2.getSerializedData().keySet()) {
            if (!hashMap.containsKey(str)) {
                hashMap.put(str, serializedModel2.getSerializedData().get(str));
            }
        }
        return builder().modelSchema(modelSchema).serializedData(hashMap).build();
    }

    public static Map<String, Object> parseSerializedData(Map<String, Object> map, String str, SchemaRegistry schemaRegistry) {
        HashMap hashMap = new HashMap();
        for (Map.Entry<String, ModelField> entry : schemaRegistry.getModelSchemaForModelClass(str).getFields().entrySet()) {
            String key = entry.getKey();
            ModelField value = entry.getValue();
            if (map.containsKey(key)) {
                Object obj = map.get(key);
                if (obj == null) {
                    hashMap.put(key, null);
                } else if (value.isModel()) {
                    ModelSchema modelSchemaForModelClass = schemaRegistry.getModelSchemaForModelClass(value.getTargetType());
                    Map<String, Object> map2 = (Map) map.get(key);
                    if (map2 != null) {
                        hashMap.put(key, builder().modelSchema(modelSchemaForModelClass).serializedData(map2).build());
                    }
                } else if (!value.isCustomType()) {
                    hashMap.put(key, obj);
                } else if (value.isArray()) {
                    List list = (List) obj;
                    ArrayList arrayList = new ArrayList();
                    if (!list.isEmpty()) {
                        Iterator it = list.iterator();
                        while (it.hasNext()) {
                            arrayList.add(SerializedCustomType.builder().serializedData(SerializedCustomType.parseSerializedData((Map) it.next(), value.getTargetType(), schemaRegistry)).customTypeSchema(schemaRegistry.getCustomTypeSchemaForCustomTypeClass(value.getTargetType())).build());
                        }
                    }
                    hashMap.put(key, arrayList);
                } else {
                    hashMap.put(key, SerializedCustomType.builder().serializedData(SerializedCustomType.parseSerializedData((Map) obj, value.getTargetType(), schemaRegistry)).customTypeSchema(schemaRegistry.getCustomTypeSchemaForCustomTypeClass(value.getTargetType())).build());
                }
            }
        }
        return hashMap;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || SerializedModel.class != obj.getClass()) {
            return false;
        }
        SerializedModel serializedModel = (SerializedModel) obj;
        return s3.d.a(this.modelId, serializedModel.modelId) && s3.d.a(this.serializedData, serializedModel.serializedData) && s3.d.a(this.modelSchema, serializedModel.modelSchema);
    }

    @Override // com.amplifyframework.core.model.Model
    public String getModelName() {
        ModelSchema modelSchema = this.modelSchema;
        if (modelSchema == null) {
            return null;
        }
        return modelSchema.getName();
    }

    public ModelSchema getModelSchema() {
        return this.modelSchema;
    }

    public Map<String, Object> getSerializedData() {
        return this.serializedData;
    }

    public Object getValue(ModelField modelField) {
        return this.serializedData.get(modelField.getName());
    }

    public int hashCode() {
        return s3.d.b(this.modelId, this.serializedData, this.modelSchema);
    }

    @Override // com.amplifyframework.core.model.Model
    public Serializable resolveIdentifier() {
        return this.modelId;
    }

    public String toString() {
        return "SerializedModel{id='" + this.modelId + "', serializedData=" + this.serializedData + ", modelName=" + getModelName() + '}';
    }

    private SerializedModel(Serializable serializable, Map<String, Object> map, ModelSchema modelSchema) {
        this.modelId = serializable;
        this.serializedData = map;
        this.modelSchema = modelSchema;
    }
}
