package j.k0;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import okhttp3.HttpUrl;

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

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

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

        @Override // j.t0.c
        public Iterator<T> iterator() {
            return this.a.iterator();
        }
    }

    /* JADX INFO: Add missing generic type declarations: [T] */
    /* JADX INFO: compiled from: _Collections.kt */
    static final class b<T> extends j.p0.d.s implements j.p0.c.a<Iterator<? extends T>> {
        final /* synthetic */ Iterable<T> b;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        /* JADX WARN: Multi-variable type inference failed */
        b(Iterable<? extends T> iterable) {
            super(0);
            this.b = iterable;
        }

        @Override // j.p0.c.a
        /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] */
        public final Iterator<T> invoke() {
            return this.b.iterator();
        }
    }

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

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

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

    public static <T> T D(List<? extends T> list) {
        j.p0.d.r.f(list, "<this>");
        if (list.isEmpty()) {
            throw new NoSuchElementException("List is empty.");
        }
        return list.get(o.h(list));
    }

    public static <T> T E(List<? extends T> list) {
        j.p0.d.r.f(list, "<this>");
        if (list.isEmpty()) {
            return null;
        }
        return list.get(list.size() - 1);
    }

    public static <T extends Comparable<? super T>> T F(Iterable<? extends T> iterable) {
        j.p0.d.r.f(iterable, "<this>");
        Iterator<? extends T> it = iterable.iterator();
        if (!it.hasNext()) {
            return null;
        }
        T next = it.next();
        while (it.hasNext()) {
            T next2 = it.next();
            if (next.compareTo(next2) > 0) {
                next = next2;
            }
        }
        return next;
    }

    public static <T> List<T> G(Collection<? extends T> collection, Iterable<? extends T> iterable) {
        j.p0.d.r.f(collection, "<this>");
        j.p0.d.r.f(iterable, "elements");
        if (!(iterable instanceof Collection)) {
            ArrayList arrayList = new ArrayList(collection);
            t.t(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 H(Iterable<? extends T> iterable) {
        j.p0.d.r.f(iterable, "<this>");
        if (iterable instanceof List) {
            return (T) I((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 I(List<? extends T> list) {
        j.p0.d.r.f(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 <T> T J(List<? extends T> list) {
        j.p0.d.r.f(list, "<this>");
        if (list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static <T> List<T> K(Iterable<? extends T> iterable, Comparator<? super T> comparator) {
        j.p0.d.r.f(iterable, "<this>");
        j.p0.d.r.f(comparator, "comparator");
        if (!(iterable instanceof Collection)) {
            List<T> listP = P(iterable);
            s.s(listP, comparator);
            return listP;
        }
        Collection collection = (Collection) iterable;
        if (collection.size() <= 1) {
            return O(iterable);
        }
        Object[] array = collection.toArray(new Object[0]);
        j.p0.d.r.d(array, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.toTypedArray>");
        j.p0.d.r.d(array, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.CollectionsKt___CollectionsKt.sortedWith>");
        i.m(array, comparator);
        return i.c(array);
    }

    public static boolean[] L(Collection<Boolean> collection) {
        j.p0.d.r.f(collection, "<this>");
        boolean[] zArr = new boolean[collection.size()];
        Iterator<Boolean> it = collection.iterator();
        int i2 = 0;
        while (it.hasNext()) {
            zArr[i2] = it.next().booleanValue();
            i2++;
        }
        return zArr;
    }

    public static final <T, C extends Collection<? super T>> C M(Iterable<? extends T> iterable, C c) {
        j.p0.d.r.f(iterable, "<this>");
        j.p0.d.r.f(c, "destination");
        Iterator<? extends T> it = iterable.iterator();
        while (it.hasNext()) {
            c.add(it.next());
        }
        return c;
    }

    public static <T> HashSet<T> N(Iterable<? extends T> iterable) {
        j.p0.d.r.f(iterable, "<this>");
        HashSet<T> hashSet = new HashSet<>(j0.b(p.p(iterable, 12)));
        M(iterable, hashSet);
        return hashSet;
    }

    public static <T> List<T> O(Iterable<? extends T> iterable) {
        j.p0.d.r.f(iterable, "<this>");
        if (!(iterable instanceof Collection)) {
            return o.l(P(iterable));
        }
        Collection collection = (Collection) iterable;
        int size = collection.size();
        if (size == 0) {
            return o.g();
        }
        if (size != 1) {
            return Q(collection);
        }
        return n.b(iterable instanceof List ? ((List) iterable).get(0) : iterable.iterator().next());
    }

    public static final <T> List<T> P(Iterable<? extends T> iterable) {
        j.p0.d.r.f(iterable, "<this>");
        if (iterable instanceof Collection) {
            return Q((Collection) iterable);
        }
        ArrayList arrayList = new ArrayList();
        M(iterable, arrayList);
        return arrayList;
    }

    public static <T> List<T> Q(Collection<? extends T> collection) {
        j.p0.d.r.f(collection, "<this>");
        return new ArrayList(collection);
    }

    public static <T> Set<T> R(Iterable<? extends T> iterable) {
        j.p0.d.r.f(iterable, "<this>");
        if (!(iterable instanceof Collection)) {
            LinkedHashSet linkedHashSet = new LinkedHashSet();
            M(iterable, linkedHashSet);
            return p0.c(linkedHashSet);
        }
        Collection collection = (Collection) iterable;
        int size = collection.size();
        if (size == 0) {
            return p0.b();
        }
        if (size == 1) {
            return o0.a(iterable instanceof List ? ((List) iterable).get(0) : iterable.iterator().next());
        }
        LinkedHashSet linkedHashSet2 = new LinkedHashSet(j0.b(collection.size()));
        M(iterable, linkedHashSet2);
        return linkedHashSet2;
    }

    public static <T> Iterable<b0<T>> S(Iterable<? extends T> iterable) {
        j.p0.d.r.f(iterable, "<this>");
        return new c0(new b(iterable));
    }

    public static <T> j.t0.c<T> w(Iterable<? extends T> iterable) {
        j.p0.d.r.f(iterable, "<this>");
        return new a(iterable);
    }

    public static <T> T x(List<? extends T> list) {
        j.p0.d.r.f(list, "<this>");
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public static <T> int y(List<? extends T> list, T t) {
        j.p0.d.r.f(list, "<this>");
        return list.indexOf(t);
    }

    public static final <T, A extends Appendable> A z(Iterable<? extends T> iterable, A a2, CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3, int i2, CharSequence charSequence4, j.p0.c.l<? super T, ? extends CharSequence> lVar) throws IOException {
        j.p0.d.r.f(iterable, "<this>");
        j.p0.d.r.f(a2, "buffer");
        j.p0.d.r.f(charSequence, "separator");
        j.p0.d.r.f(charSequence2, "prefix");
        j.p0.d.r.f(charSequence3, "postfix");
        j.p0.d.r.f(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;
            }
            j.u0.i.a(a2, t, lVar);
        }
        if (i2 >= 0 && i3 > i2) {
            a2.append(charSequence4);
        }
        a2.append(charSequence3);
        return a2;
    }
}
