package com.amplifyframework.api.aws;

import com.amplifyframework.api.graphql.QueryType;
import com.amplifyframework.core.model.PropertyContainerPath;
import com.amplifyframework.core.model.PropertyPath;
import com.amplifyframework.core.model.PropertyPathMetadata;
import com.google.android.gms.measurement.api.AppMeasurementSdk;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import kotlin.Metadata;
import sm.x0;

@Metadata(d1 = {"\u0000*\n\u0002\u0018\u0002\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0003\n\u0002\u0010\u000b\n\u0002\b\u0002\n\u0002\u0010 \n\u0002\b\u0003\u001a\u0016\u0010\u0003\u001a\u0004\u0018\u00010\u0000*\u00020\u00002\u0006\u0010\u0002\u001a\u00020\u0001H\u0000\u001a\u0014\u0010\u0006\u001a\u00020\u0005*\u00020\u00002\u0006\u0010\u0004\u001a\u00020\u0000H\u0000\u001a\u000e\u0010\b\u001a\u0004\u0018\u00010\u0000*\u00020\u0007H\u0000\u001a\u0010\u0010\n\u001a\u00020\u00002\u0006\u0010\t\u001a\u00020\u0007H\u0002\u001a\u0018\u0010\r\u001a\u00020\u000b2\u0006\u0010\t\u001a\u00020\u00072\u0006\u0010\f\u001a\u00020\u000bH\u0002\u001a\u001e\u0010\u000f\u001a\b\u0012\u0004\u0012\u00020\u00070\u000e2\u0006\u0010\t\u001a\u00020\u00072\u0006\u0010\f\u001a\u00020\u000bH\u0002\u001a\u0014\u0010\u0010\u001a\u00020\u0005*\u00020\u00002\u0006\u0010\u0004\u001a\u00020\u0000H\u0001¨\u0006\u0011"}, d2 = {"Lcom/amplifyframework/api/aws/SelectionSet;", "", AppMeasurementSdk.ConditionalUserProperty.NAME, "findChildByName", "selectionSet", "Lrm/n0;", "replaceChild", "Lcom/amplifyframework/core/model/PropertyContainerPath;", "asSelectionSetWithoutRoot", "node", "getSelectionSet", "", "includeRoot", "shouldProcessNode", "", "nodesInPath", "mergeChild", "aws-api-appsync_release"}, k = 2, mv = {1, 9, 0})
/* loaded from: classes4.dex */
public final class SelectionSetUtils {
    public static final SelectionSet asSelectionSetWithoutRoot(PropertyContainerPath propertyContainerPath) {
        int v10;
        Object obj;
        kotlin.jvm.internal.t.g(propertyContainerPath, "<this>");
        ArrayList arrayList = new ArrayList();
        List<PropertyContainerPath> nodesInPath = nodesInPath(propertyContainerPath, false);
        v10 = sm.v.v(nodesInPath, 10);
        ArrayList arrayList2 = new ArrayList(v10);
        for (PropertyContainerPath propertyContainerPath2 : nodesInPath) {
            arrayList.add(Boolean.valueOf(propertyContainerPath2.getMetadata().isCollection()));
            arrayList2.add(getSelectionSet(propertyContainerPath2));
        }
        if (arrayList2.isEmpty()) {
            return null;
        }
        Iterator it = arrayList2.iterator();
        if (!it.hasNext()) {
            throw new UnsupportedOperationException("Empty collection can't be reduced.");
        }
        Object next = it.next();
        int i10 = 1;
        while (it.hasNext()) {
            int i11 = i10 + 1;
            if (i10 < 0) {
                sm.u.u();
            }
            SelectionSet selectionSet = (SelectionSet) it.next();
            SelectionSet selectionSet2 = (SelectionSet) next;
            if (((Boolean) arrayList.get(i10)).booleanValue()) {
                Set<SelectionSet> nodes = selectionSet.getNodes();
                kotlin.jvm.internal.t.f(nodes, "getNodes(...)");
                Iterator<T> it2 = nodes.iterator();
                while (true) {
                    if (!it2.hasNext()) {
                        obj = null;
                        break;
                    }
                    obj = it2.next();
                    if (kotlin.jvm.internal.t.b(((SelectionSet) obj).getValue(), "items")) {
                        break;
                    }
                }
                SelectionSet selectionSet3 = (SelectionSet) obj;
                if (selectionSet3 != null) {
                    replaceChild(selectionSet3, selectionSet2);
                }
            } else {
                replaceChild(selectionSet, selectionSet2);
            }
            i10 = i11;
            next = selectionSet;
        }
        return (SelectionSet) next;
    }

    public static final SelectionSet findChildByName(SelectionSet selectionSet, String name) {
        Object obj;
        kotlin.jvm.internal.t.g(selectionSet, "<this>");
        kotlin.jvm.internal.t.g(name, "name");
        Set<SelectionSet> nodes = selectionSet.getNodes();
        kotlin.jvm.internal.t.f(nodes, "getNodes(...)");
        Iterator<T> it = nodes.iterator();
        while (true) {
            if (!it.hasNext()) {
                obj = null;
                break;
            }
            obj = it.next();
            if (kotlin.jvm.internal.t.b(((SelectionSet) obj).getValue(), name)) {
                break;
            }
        }
        return (SelectionSet) obj;
    }

    private static final SelectionSet getSelectionSet(PropertyContainerPath propertyContainerPath) {
        Set f10;
        PropertyPathMetadata metadata = propertyContainerPath.getMetadata();
        SelectionSet build = SelectionSet.builder().operation(QueryType.GET).value(metadata.isCollection() ? "items" : metadata.getName()).requestOptions(new ApiGraphQLRequestOptions(0)).modelClass(propertyContainerPath.getModelType()).build();
        if (metadata.isCollection()) {
            String name = metadata.getName();
            f10 = x0.f(build);
            build = new SelectionSet(name, f10);
        }
        kotlin.jvm.internal.t.d(build);
        return build;
    }

    public static final void mergeChild(SelectionSet selectionSet, SelectionSet selectionSet2) {
        kotlin.jvm.internal.t.g(selectionSet, "<this>");
        kotlin.jvm.internal.t.g(selectionSet2, "selectionSet");
        String value = selectionSet2.getValue();
        if (value == null) {
            value = "";
        }
        SelectionSet findChildByName = findChildByName(selectionSet, value);
        if (findChildByName == null) {
            selectionSet.getNodes().add(selectionSet2);
            return;
        }
        ArrayList arrayList = new ArrayList();
        Set<SelectionSet> nodes = selectionSet2.getNodes();
        kotlin.jvm.internal.t.f(nodes, "getNodes(...)");
        for (SelectionSet selectionSet3 : nodes) {
            String value2 = selectionSet3.getValue();
            kotlin.jvm.internal.t.f(selectionSet3.getNodes(), "getNodes(...)");
            if (!(!r4.isEmpty()) || value2 == null) {
                kotlin.jvm.internal.t.d(selectionSet3);
                arrayList.add(selectionSet3);
            } else if (findChildByName(findChildByName, value2) != null) {
                kotlin.jvm.internal.t.d(selectionSet3);
                mergeChild(findChildByName, selectionSet3);
            } else {
                kotlin.jvm.internal.t.d(selectionSet3);
                arrayList.add(selectionSet3);
            }
        }
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            replaceChild(findChildByName, (SelectionSet) it.next());
        }
    }

    private static final List<PropertyContainerPath> nodesInPath(PropertyContainerPath propertyContainerPath, boolean z10) {
        ArrayList arrayList = new ArrayList();
        while (propertyContainerPath != null && shouldProcessNode(propertyContainerPath, z10)) {
            arrayList.add(propertyContainerPath);
            PropertyPath parent = propertyContainerPath.getMetadata().getParent();
            propertyContainerPath = parent instanceof PropertyContainerPath ? (PropertyContainerPath) parent : null;
        }
        return arrayList;
    }

    public static final void replaceChild(SelectionSet selectionSet, SelectionSet selectionSet2) {
        kotlin.jvm.internal.t.g(selectionSet, "<this>");
        kotlin.jvm.internal.t.g(selectionSet2, "selectionSet");
        Set<SelectionSet> nodes = selectionSet.getNodes();
        final SelectionSetUtils$replaceChild$1 selectionSetUtils$replaceChild$1 = new SelectionSetUtils$replaceChild$1(selectionSet2);
        nodes.removeIf(new Predicate() { // from class: com.amplifyframework.api.aws.w
            @Override // java.util.function.Predicate
            public final boolean test(Object obj) {
                boolean replaceChild$lambda$1;
                replaceChild$lambda$1 = SelectionSetUtils.replaceChild$lambda$1(dn.l.this, obj);
                return replaceChild$lambda$1;
            }
        });
        selectionSet.getNodes().add(selectionSet2);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final boolean replaceChild$lambda$1(dn.l tmp0, Object obj) {
        kotlin.jvm.internal.t.g(tmp0, "$tmp0");
        return ((Boolean) tmp0.invoke(obj)).booleanValue();
    }

    private static final boolean shouldProcessNode(PropertyContainerPath propertyContainerPath, boolean z10) {
        return z10 || propertyContainerPath.getMetadata().getParent() != null;
    }
}
