package kotlin.collections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

/* JADX INFO: loaded from: classes.dex */
public abstract class n extends l {
    public static boolean c1(Iterable iterable, Object obj) {
        int iIndexOf;
        nb.g.i(iterable, "<this>");
        if (iterable instanceof Collection) {
            return ((Collection) iterable).contains(obj);
        }
        if (!(iterable instanceof List)) {
            int i10 = 0;
            for (Object obj2 : iterable) {
                if (i10 < 0) {
                    hb.a.C0();
                    throw null;
                }
                if (nb.g.c(obj, obj2)) {
                    iIndexOf = i10;
                } else {
                    i10++;
                }
            }
            return false;
        }
        iIndexOf = ((List) iterable).indexOf(obj);
        return iIndexOf >= 0;
    }

    public static Object d1(final int i10, Iterable iterable) {
        nb.g.i(iterable, "<this>");
        boolean z10 = iterable instanceof List;
        if (z10) {
            return ((List) iterable).get(i10);
        }
        sd.l lVar = new sd.l() { // from class: kotlin.collections.CollectionsKt___CollectionsKt$elementAt$1
            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            {
                super(1);
            }

            @Override // sd.l
            public final Object invoke(Object obj) {
                ((Number) obj).intValue();
                throw new IndexOutOfBoundsException(defpackage.e.l(new StringBuilder("Collection doesn't contain element at index "), i10, '.'));
            }
        };
        if (z10) {
            List list = (List) iterable;
            if (i10 >= 0 && i10 <= hb.a.b0(list)) {
                return list.get(i10);
            }
            lVar.invoke(Integer.valueOf(i10));
            throw null;
        }
        if (i10 < 0) {
            lVar.invoke(Integer.valueOf(i10));
            throw null;
        }
        int i11 = 0;
        for (Object obj : iterable) {
            int i12 = i11 + 1;
            if (i10 == i11) {
                return obj;
            }
            i11 = i12;
        }
        lVar.invoke(Integer.valueOf(i10));
        throw null;
    }

    public static Object e1(List list) {
        nb.g.i(list, "<this>");
        if (list.isEmpty()) {
            throw new NoSuchElementException("List is empty.");
        }
        return list.get(0);
    }

    public static Object f1(List list) {
        nb.g.i(list, "<this>");
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public static Object g1(int i10, k1.i iVar) {
        if (i10 < 0 || i10 > hb.a.b0(iVar)) {
            return null;
        }
        return iVar.get(i10);
    }

    public static final void h1(Iterable iterable, StringBuilder sb, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i10, CharSequence charSequence4, sd.l lVar) {
        nb.g.i(iterable, "<this>");
        nb.g.i(charSequence, "separator");
        nb.g.i(charSequence2, "prefix");
        nb.g.i(charSequence3, "postfix");
        nb.g.i(charSequence4, "truncated");
        sb.append(charSequence2);
        int i11 = 0;
        for (Object obj : iterable) {
            i11++;
            if (i11 > 1) {
                sb.append(charSequence);
            }
            if (i10 >= 0 && i11 > i10) {
                break;
            } else {
                nb.g.b(sb, obj, lVar);
            }
        }
        if (i10 >= 0 && i11 > i10) {
            sb.append(charSequence4);
        }
        sb.append(charSequence3);
    }

    public static String j1(Iterable iterable, String str, String str2, String str3, sd.l lVar, int i10) {
        if ((i10 & 1) != 0) {
            str = ", ";
        }
        String str4 = str;
        String str5 = (i10 & 2) != 0 ? "" : str2;
        String str6 = (i10 & 4) != 0 ? "" : str3;
        int i11 = (i10 & 8) != 0 ? -1 : 0;
        CharSequence charSequence = (i10 & 16) != 0 ? "..." : null;
        sd.l lVar2 = (i10 & 32) != 0 ? null : lVar;
        nb.g.i(iterable, "<this>");
        nb.g.i(str4, "separator");
        nb.g.i(str5, "prefix");
        nb.g.i(str6, "postfix");
        nb.g.i(charSequence, "truncated");
        StringBuilder sb = new StringBuilder();
        h1(iterable, sb, str4, str5, str6, i11, charSequence, lVar2);
        String string = sb.toString();
        nb.g.h(string, "joinTo(StringBuilder(), …ed, transform).toString()");
        return string;
    }

    public static Object k1(List list) {
        nb.g.i(list, "<this>");
        if (list.isEmpty()) {
            throw new NoSuchElementException("List is empty.");
        }
        return list.get(hb.a.b0(list));
    }

    public static ArrayList l1(Iterable iterable, Collection collection) {
        nb.g.i(collection, "<this>");
        nb.g.i(iterable, "elements");
        if (!(iterable instanceof Collection)) {
            ArrayList arrayList = new ArrayList(collection);
            k.b1(iterable, arrayList);
            return arrayList;
        }
        Collection collection2 = (Collection) iterable;
        ArrayList arrayList2 = new ArrayList(collection2.size() + collection.size());
        arrayList2.addAll(collection);
        arrayList2.addAll(collection2);
        return arrayList2;
    }

    public static ArrayList m1(Object obj, Collection collection) {
        nb.g.i(collection, "<this>");
        ArrayList arrayList = new ArrayList(collection.size() + 1);
        arrayList.addAll(collection);
        arrayList.add(obj);
        return arrayList;
    }

    public static List n1(Iterable iterable) {
        nb.g.i(iterable, "<this>");
        if (!(iterable instanceof Collection)) {
            List listS1 = s1(iterable);
            j.a1(listS1);
            return listS1;
        }
        Collection collection = (Collection) iterable;
        if (collection.size() <= 1) {
            return q1(iterable);
        }
        Object[] array = collection.toArray(new Comparable[0]);
        Comparable[] comparableArr = (Comparable[]) array;
        nb.g.i(comparableArr, "<this>");
        if (comparableArr.length > 1) {
            Arrays.sort(comparableArr);
        }
        return q.O(array);
    }

    public static List o1(Comparator comparator, Iterable iterable) {
        nb.g.i(iterable, "<this>");
        if (!(iterable instanceof Collection)) {
            List listS1 = s1(iterable);
            if (((ArrayList) listS1).size() > 1) {
                Collections.sort(listS1, comparator);
            }
            return listS1;
        }
        Collection collection = (Collection) iterable;
        if (collection.size() <= 1) {
            return q1(iterable);
        }
        Object[] array = collection.toArray(new Object[0]);
        nb.g.i(array, "<this>");
        if (array.length > 1) {
            Arrays.sort(array, comparator);
        }
        return q.O(array);
    }

    public static final void p1(Iterable iterable, java.util.AbstractCollection abstractCollection) {
        nb.g.i(iterable, "<this>");
        Iterator it = iterable.iterator();
        while (it.hasNext()) {
            abstractCollection.add(it.next());
        }
    }

    public static List q1(Iterable iterable) {
        nb.g.i(iterable, "<this>");
        if (!(iterable instanceof Collection)) {
            return hb.a.r0(s1(iterable));
        }
        Collection collection = (Collection) iterable;
        int size = collection.size();
        if (size == 0) {
            return EmptyList.f29203a;
        }
        if (size != 1) {
            return r1(collection);
        }
        return hb.a.l0(iterable instanceof List ? ((List) iterable).get(0) : iterable.iterator().next());
    }

    public static ArrayList r1(Collection collection) {
        nb.g.i(collection, "<this>");
        return new ArrayList(collection);
    }

    public static final List s1(Iterable iterable) {
        nb.g.i(iterable, "<this>");
        if (iterable instanceof Collection) {
            return r1((Collection) iterable);
        }
        ArrayList arrayList = new ArrayList();
        p1(iterable, arrayList);
        return arrayList;
    }

    public static Set t1(Iterable iterable) {
        nb.g.i(iterable, "<this>");
        if (!(iterable instanceof Collection)) {
            LinkedHashSet linkedHashSet = new LinkedHashSet();
            p1(iterable, linkedHashSet);
            int size = linkedHashSet.size();
            if (size == 0) {
                return EmptySet.f29205a;
            }
            if (size != 1) {
                return linkedHashSet;
            }
            Set setSingleton = Collections.singleton(linkedHashSet.iterator().next());
            nb.g.h(setSingleton, "singleton(element)");
            return setSingleton;
        }
        Collection collection = (Collection) iterable;
        int size2 = collection.size();
        if (size2 == 0) {
            return EmptySet.f29205a;
        }
        if (size2 != 1) {
            LinkedHashSet linkedHashSet2 = new LinkedHashSet(ua.b.D(collection.size()));
            p1(iterable, linkedHashSet2);
            return linkedHashSet2;
        }
        Set setSingleton2 = Collections.singleton(iterable instanceof List ? ((List) iterable).get(0) : iterable.iterator().next());
        nb.g.h(setSingleton2, "singleton(element)");
        return setSingleton2;
    }
}
