package c.l;

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 r extends q {

    /* JADX INFO: Add missing generic type declarations: [T] */
    /* JADX INFO: compiled from: Sequences.kt */
    public static final class a<T> implements c.t.c<T> {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final /* synthetic */ Iterable f1421a;

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

        @Override // c.t.c
        public Iterator<T> iterator() {
            return this.f1421a.iterator();
        }
    }

    public static <T> List<T> A(Collection<? extends T> collection) {
        c.q.d.j.c(collection, "$this$toMutableList");
        return new ArrayList(collection);
    }

    public static <T> Set<T> B(Iterable<? extends T> iterable) {
        c.q.d.j.c(iterable, "$this$toSet");
        if (!(iterable instanceof Collection)) {
            LinkedHashSet linkedHashSet = new LinkedHashSet();
            x(iterable, linkedHashSet);
            return e0.c(linkedHashSet);
        }
        Collection collection = (Collection) iterable;
        int size = collection.size();
        if (size == 0) {
            return e0.b();
        }
        if (size == 1) {
            return d0.a(iterable instanceof List ? ((List) iterable).get(0) : iterable.iterator().next());
        }
        LinkedHashSet linkedHashSet2 = new LinkedHashSet(a0.b(collection.size()));
        x(iterable, linkedHashSet2);
        return linkedHashSet2;
    }

    public static <T> c.t.c<T> q(Iterable<? extends T> iterable) {
        c.q.d.j.c(iterable, "$this$asSequence");
        return new a(iterable);
    }

    public static <T> T r(List<? extends T> list) {
        c.q.d.j.c(list, "$this$firstOrNull");
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public static final <T, A extends Appendable> A s(Iterable<? extends T> iterable, A a2, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i, CharSequence charSequence4, c.q.c.b<? super T, ? extends CharSequence> bVar) throws IOException {
        c.q.d.j.c(iterable, "$this$joinTo");
        c.q.d.j.c(a2, "buffer");
        c.q.d.j.c(charSequence, "separator");
        c.q.d.j.c(charSequence2, "prefix");
        c.q.d.j.c(charSequence3, "postfix");
        c.q.d.j.c(charSequence4, "truncated");
        a2.append(charSequence2);
        int i2 = 0;
        for (T t : iterable) {
            i2++;
            if (i2 > 1) {
                a2.append(charSequence);
            }
            if (i >= 0 && i2 > i) {
                break;
            }
            c.u.k.e(a2, t, bVar);
        }
        if (i >= 0 && i2 > i) {
            a2.append(charSequence4);
        }
        a2.append(charSequence3);
        return a2;
    }

    public static /* synthetic */ Appendable t(Iterable iterable, Appendable appendable, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i, CharSequence charSequence4, c.q.c.b bVar, int i2, Object obj) throws IOException {
        s(iterable, appendable, (i2 & 2) != 0 ? ", " : charSequence, (i2 & 4) != 0 ? "" : charSequence2, (i2 & 8) == 0 ? charSequence3 : "", (i2 & 16) != 0 ? -1 : i, (i2 & 32) != 0 ? "..." : charSequence4, (i2 & 64) != 0 ? null : bVar);
        return appendable;
    }

    public static <T> List<T> u(Collection<? extends T> collection, Iterable<? extends T> iterable) {
        c.q.d.j.c(collection, "$this$plus");
        c.q.d.j.c(iterable, "elements");
        if (!(iterable instanceof Collection)) {
            ArrayList arrayList = new ArrayList(collection);
            o.o(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> T v(Iterable<? extends T> iterable) {
        c.q.d.j.c(iterable, "$this$single");
        if (iterable instanceof List) {
            return (T) w((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 w(List<? extends T> list) {
        c.q.d.j.c(list, "$this$single");
        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, C extends Collection<? super T>> C x(Iterable<? extends T> iterable, C c2) {
        c.q.d.j.c(iterable, "$this$toCollection");
        c.q.d.j.c(c2, "destination");
        Iterator<? extends T> it = iterable.iterator();
        while (it.hasNext()) {
            c2.add(it.next());
        }
        return c2;
    }

    public static <T> List<T> y(Iterable<? extends T> iterable) {
        c.q.d.j.c(iterable, "$this$toList");
        if (!(iterable instanceof Collection)) {
            return j.i(z(iterable));
        }
        Collection collection = (Collection) iterable;
        int size = collection.size();
        if (size == 0) {
            return j.f();
        }
        if (size != 1) {
            return A(collection);
        }
        return i.b(iterable instanceof List ? ((List) iterable).get(0) : iterable.iterator().next());
    }

    public static final <T> List<T> z(Iterable<? extends T> iterable) {
        c.q.d.j.c(iterable, "$this$toMutableList");
        if (iterable instanceof Collection) {
            return A((Collection) iterable);
        }
        ArrayList arrayList = new ArrayList();
        x(iterable, arrayList);
        return arrayList;
    }
}
