package B6;

import com.google.android.gms.internal.measurement.C1;
import java.util.AbstractCollection;
import java.util.AbstractList;
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.ListIterator;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
import java.util.Set;
import n4.u0;

/* JADX INFO: loaded from: classes.dex */
public abstract class k extends q {
    public static Set A0(Collection collection) {
        P6.g.e(collection, "<this>");
        v vVar = v.f1034q;
        int size = collection.size();
        if (size == 0) {
            return vVar;
        }
        if (size != 1) {
            LinkedHashSet linkedHashSet = new LinkedHashSet(w.l0(collection.size()));
            w0(collection, linkedHashSet);
            return linkedHashSet;
        }
        Set setSingleton = Collections.singleton(collection instanceof List ? ((List) collection).get(0) : collection.iterator().next());
        P6.g.d(setSingleton, "singleton(...)");
        return setSingleton;
    }

    public static List e0(List list) {
        P6.g.e(list, "<this>");
        int size = list.size() - 1;
        if (size <= 0) {
            return t.f1032q;
        }
        if (size == 1) {
            return C1.C(n0(list));
        }
        ArrayList arrayList = new ArrayList(size);
        if (list instanceof RandomAccess) {
            int size2 = list.size();
            for (int i = 1; i < size2; i++) {
                arrayList.add(list.get(i));
            }
        } else {
            ListIterator listIterator = list.listIterator(1);
            while (listIterator.hasNext()) {
                arrayList.add(listIterator.next());
            }
        }
        return arrayList;
    }

    public static ArrayList f0(List list) {
        ArrayList arrayList = new ArrayList();
        for (Object obj : list) {
            if (obj != null) {
                arrayList.add(obj);
            }
        }
        return arrayList;
    }

    public static Object g0(Collection collection) {
        P6.g.e(collection, "<this>");
        if (collection instanceof List) {
            return h0((List) collection);
        }
        Iterator it = collection.iterator();
        if (it.hasNext()) {
            return it.next();
        }
        throw new NoSuchElementException("Collection is empty.");
    }

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

    public static Object i0(List list) {
        P6.g.e(list, "<this>");
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public static Object j0(int i, List list) {
        if (i < 0 || i >= list.size()) {
            return null;
        }
        return list.get(i);
    }

    public static final void k0(Iterable iterable, StringBuilder sb, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, CharSequence charSequence4, O6.l lVar) {
        P6.g.e(iterable, "<this>");
        sb.append(charSequence2);
        int i = 0;
        for (Object obj : iterable) {
            i++;
            if (i > 1) {
                sb.append(charSequence);
            }
            u0.b(sb, obj, lVar);
        }
        sb.append(charSequence3);
    }

    public static String m0(Iterable iterable, String str, String str2, String str3, O6.l lVar, int i) {
        if ((i & 1) != 0) {
            str = ", ";
        }
        String str4 = str;
        String str5 = (i & 2) != 0 ? "" : str2;
        String str6 = (i & 4) != 0 ? "" : str3;
        if ((i & 32) != 0) {
            lVar = null;
        }
        P6.g.e(iterable, "<this>");
        StringBuilder sb = new StringBuilder();
        k0(iterable, sb, str4, str5, str6, "...", lVar);
        return sb.toString();
    }

    public static Object n0(List list) {
        P6.g.e(list, "<this>");
        if (list.isEmpty()) {
            throw new NoSuchElementException("List is empty.");
        }
        return list.get(l.V(list));
    }

    public static Object o0(List list) {
        P6.g.e(list, "<this>");
        if (list.isEmpty()) {
            return null;
        }
        return list.get(list.size() - 1);
    }

    public static Comparable p0(ArrayList arrayList) {
        Iterator it = arrayList.iterator();
        if (!it.hasNext()) {
            return null;
        }
        Comparable comparable = (Comparable) it.next();
        while (it.hasNext()) {
            Comparable comparable2 = (Comparable) it.next();
            if (comparable.compareTo(comparable2) < 0) {
                comparable = comparable2;
            }
        }
        return comparable;
    }

    public static ArrayList q0(Collection collection, Iterable iterable) {
        P6.g.e(collection, "<this>");
        Collection collection2 = (Collection) iterable;
        ArrayList arrayList = new ArrayList(collection2.size() + collection.size());
        arrayList.addAll(collection);
        arrayList.addAll(collection2);
        return arrayList;
    }

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

    public static List s0(Iterable iterable) {
        P6.g.e(iterable, "<this>");
        if ((iterable instanceof Collection) && ((Collection) iterable).size() <= 1) {
            return x0(iterable);
        }
        List listZ0 = z0(iterable);
        Collections.reverse(listZ0);
        return listZ0;
    }

    public static List t0(AbstractList abstractList) {
        P6.g.e(abstractList, "<this>");
        if (abstractList.size() <= 1) {
            return x0(abstractList);
        }
        Object[] array = abstractList.toArray(new Comparable[0]);
        Comparable[] comparableArr = (Comparable[]) array;
        P6.g.e(comparableArr, "<this>");
        if (comparableArr.length > 1) {
            Arrays.sort(comparableArr);
        }
        return j.D(array);
    }

    public static List u0(Collection collection, Comparator comparator) {
        P6.g.e(collection, "<this>");
        if (collection.size() <= 1) {
            return x0(collection);
        }
        Object[] array = collection.toArray(new Object[0]);
        P6.g.e(array, "<this>");
        if (array.length > 1) {
            Arrays.sort(array, comparator);
        }
        return j.D(array);
    }

    public static List v0(int i, List list) {
        if (i < 0) {
            throw new IllegalArgumentException(I1.a.k(i, "Requested element count ", " is less than zero.").toString());
        }
        if (i == 0) {
            return t.f1032q;
        }
        if (i >= list.size()) {
            return x0(list);
        }
        if (i == 1) {
            return C1.C(g0(list));
        }
        ArrayList arrayList = new ArrayList(i);
        Iterator it = list.iterator();
        int i5 = 0;
        while (it.hasNext()) {
            arrayList.add(it.next());
            i5++;
            if (i5 == i) {
                break;
            }
        }
        return l.Y(arrayList);
    }

    public static final void w0(Iterable iterable, AbstractCollection abstractCollection) {
        P6.g.e(iterable, "<this>");
        Iterator it = iterable.iterator();
        while (it.hasNext()) {
            abstractCollection.add(it.next());
        }
    }

    public static List x0(Iterable iterable) {
        P6.g.e(iterable, "<this>");
        if (!(iterable instanceof Collection)) {
            return l.Y(z0(iterable));
        }
        Collection collection = (Collection) iterable;
        int size = collection.size();
        if (size == 0) {
            return t.f1032q;
        }
        if (size != 1) {
            return y0(collection);
        }
        return C1.C(iterable instanceof List ? ((List) iterable).get(0) : collection.iterator().next());
    }

    public static ArrayList y0(Collection collection) {
        P6.g.e(collection, "<this>");
        return new ArrayList(collection);
    }

    public static final List z0(Iterable iterable) {
        P6.g.e(iterable, "<this>");
        if (iterable instanceof Collection) {
            return y0((Collection) iterable);
        }
        ArrayList arrayList = new ArrayList();
        w0(iterable, arrayList);
        return arrayList;
    }
}
