package h.s;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

/* JADX INFO: Access modifiers changed from: package-private */
/* JADX INFO: compiled from: _Collections.kt */
/* JADX INFO: loaded from: classes.dex */
public class u extends t {

    /* JADX INFO: Add missing generic type declarations: [T] */
    /* JADX INFO: compiled from: Sequences.kt */
    public static final class a<T> implements h.b0.d<T> {
        final /* synthetic */ Iterable a;

        public a(Iterable iterable) {
            this.a = iterable;
        }

        @Override // h.b0.d
        public Iterator<T> iterator() {
            return this.a.iterator();
        }
    }

    public static /* synthetic */ String A(Iterable iterable, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i2, CharSequence charSequence4, h.x.c.l lVar, int i3, Object obj) {
        if ((i3 & 1) != 0) {
            charSequence = ", ";
        }
        CharSequence charSequence5 = (i3 & 2) != 0 ? "" : charSequence2;
        CharSequence charSequence6 = (i3 & 4) == 0 ? charSequence3 : "";
        int i4 = (i3 & 8) != 0 ? -1 : i2;
        if ((i3 & 16) != 0) {
            charSequence4 = "...";
        }
        CharSequence charSequence7 = charSequence4;
        if ((i3 & 32) != 0) {
            lVar = null;
        }
        return z(iterable, charSequence, charSequence5, charSequence6, i4, charSequence7, lVar);
    }

    public static <T> T B(List<? extends T> list) {
        h.x.d.k.d(list, "<this>");
        if (list.isEmpty()) {
            throw new NoSuchElementException("List is empty.");
        }
        return list.get(m.g(list));
    }

    public static <T> List<T> C(Collection<? extends T> collection, Iterable<? extends T> iterable) {
        h.x.d.k.d(collection, "<this>");
        h.x.d.k.d(iterable, "elements");
        if (!(iterable instanceof Collection)) {
            ArrayList arrayList = new ArrayList(collection);
            r.q(arrayList, iterable);
            return arrayList;
        }
        Collection collection2 = (Collection) iterable;
        ArrayList arrayList2 = new ArrayList(collection.size() + collection2.size());
        arrayList2.addAll(collection);
        arrayList2.addAll(collection2);
        return arrayList2;
    }

    public static <T> List<T> D(Collection<? extends T> collection, T t) {
        h.x.d.k.d(collection, "<this>");
        ArrayList arrayList = new ArrayList(collection.size() + 1);
        arrayList.addAll(collection);
        arrayList.add(t);
        return arrayList;
    }

    public static <T> T E(Iterable<? extends T> iterable) {
        h.x.d.k.d(iterable, "<this>");
        if (iterable instanceof List) {
            return (T) F((List) iterable);
        }
        Iterator<? extends T> it = iterable.iterator();
        if (!it.hasNext()) {
            throw new NoSuchElementException("Collection is empty.");
        }
        T next = it.next();
        if (it.hasNext()) {
            throw new IllegalArgumentException("Collection has more than one element.");
        }
        return next;
    }

    public static final <T> T F(List<? extends T> list) {
        h.x.d.k.d(list, "<this>");
        int size = list.size();
        if (size == 0) {
            throw new NoSuchElementException("List is empty.");
        }
        if (size == 1) {
            return list.get(0);
        }
        throw new IllegalArgumentException("List has more than one element.");
    }

    public static final <T> List<T> G(Iterable<? extends T> iterable, int i2) {
        h.x.d.k.d(iterable, "<this>");
        int i3 = 0;
        if (!(i2 >= 0)) {
            throw new IllegalArgumentException(("Requested element count " + i2 + " is less than zero.").toString());
        }
        if (i2 == 0) {
            return m.f();
        }
        if (iterable instanceof Collection) {
            if (i2 >= ((Collection) iterable).size()) {
                return I(iterable);
            }
            if (i2 == 1) {
                return l.b(u(iterable));
            }
        }
        ArrayList arrayList = new ArrayList(i2);
        Iterator<? extends T> it = iterable.iterator();
        while (it.hasNext()) {
            arrayList.add(it.next());
            i3++;
            if (i3 == i2) {
                break;
            }
        }
        return m.k(arrayList);
    }

    public static final <T, C extends Collection<? super T>> C H(Iterable<? extends T> iterable, C c2) {
        h.x.d.k.d(iterable, "<this>");
        h.x.d.k.d(c2, "destination");
        Iterator<? extends T> it = iterable.iterator();
        while (it.hasNext()) {
            c2.add(it.next());
        }
        return c2;
    }

    public static <T> List<T> I(Iterable<? extends T> iterable) {
        h.x.d.k.d(iterable, "<this>");
        if (!(iterable instanceof Collection)) {
            return m.k(J(iterable));
        }
        Collection collection = (Collection) iterable;
        int size = collection.size();
        if (size == 0) {
            return m.f();
        }
        if (size != 1) {
            return K(collection);
        }
        return l.b(iterable instanceof List ? ((List) iterable).get(0) : iterable.iterator().next());
    }

    public static final <T> List<T> J(Iterable<? extends T> iterable) {
        h.x.d.k.d(iterable, "<this>");
        if (iterable instanceof Collection) {
            return K((Collection) iterable);
        }
        ArrayList arrayList = new ArrayList();
        H(iterable, arrayList);
        return arrayList;
    }

    public static <T> List<T> K(Collection<? extends T> collection) {
        h.x.d.k.d(collection, "<this>");
        return new ArrayList(collection);
    }

    public static <T> Set<T> L(Iterable<? extends T> iterable) {
        h.x.d.k.d(iterable, "<this>");
        if (!(iterable instanceof Collection)) {
            LinkedHashSet linkedHashSet = new LinkedHashSet();
            H(iterable, linkedHashSet);
            return i0.c(linkedHashSet);
        }
        Collection collection = (Collection) iterable;
        int size = collection.size();
        if (size == 0) {
            return i0.b();
        }
        if (size == 1) {
            return h0.a(iterable instanceof List ? ((List) iterable).get(0) : iterable.iterator().next());
        }
        LinkedHashSet linkedHashSet2 = new LinkedHashSet(d0.a(collection.size()));
        H(iterable, linkedHashSet2);
        return linkedHashSet2;
    }

    public static <T> h.b0.d<T> s(Iterable<? extends T> iterable) {
        h.x.d.k.d(iterable, "<this>");
        return new a(iterable);
    }

    public static <T> List<T> t(List<? extends T> list, int i2) {
        h.x.d.k.d(list, "<this>");
        if (i2 >= 0) {
            return G(list, h.z.n.b(list.size() - i2, 0));
        }
        throw new IllegalArgumentException(("Requested element count " + i2 + " is less than zero.").toString());
    }

    public static final <T> T u(Iterable<? extends T> iterable) {
        h.x.d.k.d(iterable, "<this>");
        if (iterable instanceof List) {
            return (T) k.v((List) iterable);
        }
        Iterator<? extends T> it = iterable.iterator();
        if (it.hasNext()) {
            return it.next();
        }
        throw new NoSuchElementException("Collection is empty.");
    }

    public static <T> T v(List<? extends T> list) {
        h.x.d.k.d(list, "<this>");
        if (list.isEmpty()) {
            throw new NoSuchElementException("List is empty.");
        }
        return list.get(0);
    }

    public static <T> T w(List<? extends T> list) {
        h.x.d.k.d(list, "<this>");
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public static final <T, A extends Appendable> A x(Iterable<? extends T> iterable, A a2, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i2, CharSequence charSequence4, h.x.c.l<? super T, ? extends CharSequence> lVar) throws IOException {
        h.x.d.k.d(iterable, "<this>");
        h.x.d.k.d(a2, "buffer");
        h.x.d.k.d(charSequence, "separator");
        h.x.d.k.d(charSequence2, "prefix");
        h.x.d.k.d(charSequence3, "postfix");
        h.x.d.k.d(charSequence4, "truncated");
        a2.append(charSequence2);
        int i3 = 0;
        for (T t : iterable) {
            i3++;
            if (i3 > 1) {
                a2.append(charSequence);
            }
            if (i2 >= 0 && i3 > i2) {
                break;
            }
            h.c0.h.a(a2, t, lVar);
        }
        if (i2 >= 0 && i3 > i2) {
            a2.append(charSequence4);
        }
        a2.append(charSequence3);
        return a2;
    }

    public static /* synthetic */ Appendable y(Iterable iterable, Appendable appendable, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i2, CharSequence charSequence4, h.x.c.l lVar, int i3, Object obj) throws IOException {
        x(iterable, appendable, (i3 & 2) != 0 ? ", " : charSequence, (i3 & 4) != 0 ? "" : charSequence2, (i3 & 8) == 0 ? charSequence3 : "", (i3 & 16) != 0 ? -1 : i2, (i3 & 32) != 0 ? "..." : charSequence4, (i3 & 64) != 0 ? null : lVar);
        return appendable;
    }

    public static final <T> String z(Iterable<? extends T> iterable, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i2, CharSequence charSequence4, h.x.c.l<? super T, ? extends CharSequence> lVar) throws IOException {
        h.x.d.k.d(iterable, "<this>");
        h.x.d.k.d(charSequence, "separator");
        h.x.d.k.d(charSequence2, "prefix");
        h.x.d.k.d(charSequence3, "postfix");
        h.x.d.k.d(charSequence4, "truncated");
        StringBuilder sb = new StringBuilder();
        x(iterable, sb, charSequence, charSequence2, charSequence3, i2, charSequence4, lVar);
        String string = sb.toString();
        h.x.d.k.c(string, "joinTo(StringBuilder(), …ed, transform).toString()");
        return string;
    }
}
