package defpackage;

import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.upstream.CmcdData;
import defpackage.cs;
import java.lang.reflect.Array;
import java.util.AbstractList;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\u000e\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\b\u0007\u0018\u0000*\u0004\b\u0000\u0010\u00012\b\u0012\u0004\u0012\u00028\u00000\u0002:\u0001\u0003¨\u0006\u0004"}, d2 = {"Lds;", "E", "Lr0;", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "kotlin-stdlib"}, k = 1, mv = {2, 1, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@cu8
@vx8
public final class ds<E> extends r0<E> {
    public static final Object[] d = new Object[0];
    public int a;
    public Object[] b;
    public int c;

    @Metadata(d1 = {"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u0011\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u0003\b\u0080\u0003\u0018\u00002\u00020\u0001R\u001c\u0010\u0003\u001a\n\u0012\u0006\u0012\u0004\u0018\u00010\u00010\u00028\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\u0003\u0010\u0004R\u0014\u0010\u0006\u001a\u00020\u00058\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0006\u0010\u0007¨\u0006\b"}, d2 = {"Lds$a;", "", "", "emptyElementData", "[Ljava/lang/Object;", "", "defaultMinCapacity", "I", "kotlin-stdlib"}, k = 1, mv = {2, 1, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a {
    }

    public ds() {
        this.b = d;
    }

    @Override // defpackage.r0
    /* JADX INFO: renamed from: a, reason: from getter */
    public final int getB() {
        return this.c;
    }

    @Override // java.util.AbstractList, java.util.List
    public final void add(int i, Object obj) {
        int length;
        cs.a.c(i, this.c);
        if (i == this.c) {
            addLast(obj);
            return;
        }
        if (i == 0) {
            addFirst(obj);
            return;
        }
        j();
        d(this.c + 1);
        int i2 = i(this.a + i);
        int i3 = this.c;
        if (i < ((i3 + 1) >> 1)) {
            if (i2 == 0) {
                Object[] objArr = this.b;
                cr4.g(objArr, "<this>");
                i2 = objArr.length;
            }
            int i4 = i2 - 1;
            int i5 = this.a;
            if (i5 == 0) {
                Object[] objArr2 = this.b;
                cr4.g(objArr2, "<this>");
                length = objArr2.length - 1;
            } else {
                length = i5 - 1;
            }
            int i6 = this.a;
            if (i4 >= i6) {
                Object[] objArr3 = this.b;
                objArr3[length] = objArr3[i6];
                st.n(objArr3, i6, objArr3, i6 + 1, i4 + 1);
            } else {
                Object[] objArr4 = this.b;
                st.n(objArr4, i6 - 1, objArr4, i6, objArr4.length);
                Object[] objArr5 = this.b;
                objArr5[objArr5.length - 1] = objArr5[0];
                st.n(objArr5, 0, objArr5, 1, i4 + 1);
            }
            this.b[i4] = obj;
            this.a = length;
        } else {
            int i7 = i(i3 + this.a);
            if (i2 < i7) {
                Object[] objArr6 = this.b;
                st.n(objArr6, i2 + 1, objArr6, i2, i7);
            } else {
                Object[] objArr7 = this.b;
                st.n(objArr7, 1, objArr7, 0, i7);
                Object[] objArr8 = this.b;
                objArr8[0] = objArr8[objArr8.length - 1];
                st.n(objArr8, i2 + 1, objArr8, i2, objArr8.length - 1);
            }
            this.b[i2] = obj;
        }
        this.c++;
    }

    @Override // java.util.AbstractList, java.util.List
    public final boolean addAll(int i, Collection collection) {
        cr4.g(collection, "elements");
        cs.a.c(i, this.c);
        if (collection.isEmpty()) {
            return false;
        }
        if (i == this.c) {
            return addAll(collection);
        }
        j();
        d(collection.size() + this.c);
        int i2 = i(this.c + this.a);
        int i3 = i(this.a + i);
        int size = collection.size();
        if (i >= ((this.c + 1) >> 1)) {
            int i4 = i3 + size;
            if (i3 < i2) {
                int i5 = size + i2;
                Object[] objArr = this.b;
                if (i5 <= objArr.length) {
                    st.n(objArr, i4, objArr, i3, i2);
                } else if (i4 >= objArr.length) {
                    st.n(objArr, i4 - objArr.length, objArr, i3, i2);
                } else {
                    int length = i2 - (i5 - objArr.length);
                    st.n(objArr, 0, objArr, length, i2);
                    Object[] objArr2 = this.b;
                    st.n(objArr2, i4, objArr2, i3, length);
                }
            } else {
                Object[] objArr3 = this.b;
                st.n(objArr3, size, objArr3, 0, i2);
                Object[] objArr4 = this.b;
                if (i4 >= objArr4.length) {
                    st.n(objArr4, i4 - objArr4.length, objArr4, i3, objArr4.length);
                } else {
                    st.n(objArr4, 0, objArr4, objArr4.length - size, objArr4.length);
                    Object[] objArr5 = this.b;
                    st.n(objArr5, i4, objArr5, i3, objArr5.length - size);
                }
            }
            c(i3, collection);
            return true;
        }
        int i6 = this.a;
        int length2 = i6 - size;
        if (i3 < i6) {
            Object[] objArr6 = this.b;
            st.n(objArr6, length2, objArr6, i6, objArr6.length);
            if (size >= i3) {
                Object[] objArr7 = this.b;
                st.n(objArr7, objArr7.length - size, objArr7, 0, i3);
            } else {
                Object[] objArr8 = this.b;
                st.n(objArr8, objArr8.length - size, objArr8, 0, size);
                Object[] objArr9 = this.b;
                st.n(objArr9, 0, objArr9, size, i3);
            }
        } else if (length2 >= 0) {
            Object[] objArr10 = this.b;
            st.n(objArr10, length2, objArr10, i6, i3);
        } else {
            Object[] objArr11 = this.b;
            length2 += objArr11.length;
            int i7 = i3 - i6;
            int length3 = objArr11.length - length2;
            if (length3 >= i7) {
                st.n(objArr11, length2, objArr11, i6, i3);
            } else {
                st.n(objArr11, length2, objArr11, i6, i6 + length3);
                Object[] objArr12 = this.b;
                st.n(objArr12, 0, objArr12, this.a + length3, i3);
            }
        }
        this.a = length2;
        c(g(i3 - size), collection);
        return true;
    }

    public final void addFirst(Object obj) {
        j();
        d(this.c + 1);
        int length = this.a;
        if (length == 0) {
            Object[] objArr = this.b;
            cr4.g(objArr, "<this>");
            length = objArr.length;
        }
        int i = length - 1;
        this.a = i;
        this.b[i] = obj;
        this.c++;
    }

    public final void addLast(Object obj) {
        j();
        d(getB() + 1);
        this.b[i(getB() + this.a)] = obj;
        this.c = getB() + 1;
    }

    @Override // defpackage.r0
    public final Object b(int i) {
        cs.a.b(i, this.c);
        if (i == y41.ae(this)) {
            return removeLast();
        }
        if (i == 0) {
            return removeFirst();
        }
        j();
        int i2 = i(this.a + i);
        Object[] objArr = this.b;
        Object obj = objArr[i2];
        if (i < (this.c >> 1)) {
            int i3 = this.a;
            if (i2 >= i3) {
                st.n(objArr, i3 + 1, objArr, i3, i2);
            } else {
                st.n(objArr, 1, objArr, 0, i2);
                Object[] objArr2 = this.b;
                objArr2[0] = objArr2[objArr2.length - 1];
                int i4 = this.a;
                st.n(objArr2, i4 + 1, objArr2, i4, objArr2.length - 1);
            }
            Object[] objArr3 = this.b;
            int i5 = this.a;
            objArr3[i5] = null;
            this.a = e(i5);
        } else {
            int i6 = i(y41.ae(this) + this.a);
            if (i2 <= i6) {
                Object[] objArr4 = this.b;
                st.n(objArr4, i2, objArr4, i2 + 1, i6 + 1);
            } else {
                Object[] objArr5 = this.b;
                st.n(objArr5, i2, objArr5, i2 + 1, objArr5.length);
                Object[] objArr6 = this.b;
                objArr6[objArr6.length - 1] = objArr6[0];
                st.n(objArr6, 0, objArr6, 1, i6 + 1);
            }
            this.b[i6] = null;
        }
        this.c--;
        return obj;
    }

    public final void c(int i, Collection collection) {
        Iterator<E> it = collection.iterator();
        int length = this.b.length;
        while (i < length && it.hasNext()) {
            this.b[i] = it.next();
            i++;
        }
        int i2 = this.a;
        for (int i3 = 0; i3 < i2 && it.hasNext(); i3++) {
            this.b[i3] = it.next();
        }
        this.c = collection.size() + this.c;
    }

    @Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.util.List
    public final void clear() {
        if (!isEmpty()) {
            j();
            h(this.a, i(getB() + this.a));
        }
        this.a = 0;
        this.c = 0;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean contains(Object obj) {
        return indexOf(obj) != -1;
    }

    public final void d(int i) {
        if (i < 0) {
            throw new IllegalStateException("Deque is too big.");
        }
        Object[] objArr = this.b;
        if (i <= objArr.length) {
            return;
        }
        if (objArr == d) {
            if (i < 10) {
                i = 10;
            }
            this.b = new Object[i];
            return;
        }
        Object[] objArr2 = new Object[cs.a.e(objArr.length, i)];
        Object[] objArr3 = this.b;
        st.n(objArr3, 0, objArr2, this.a, objArr3.length);
        Object[] objArr4 = this.b;
        int length = objArr4.length;
        int i2 = this.a;
        st.n(objArr4, length - i2, objArr2, 0, i2);
        this.a = 0;
        this.b = objArr2;
    }

    public final int e(int i) {
        cr4.g(this.b, "<this>");
        if (i == r0.length - 1) {
            return 0;
        }
        return i + 1;
    }

    public final Object f() {
        if (isEmpty()) {
            return null;
        }
        return this.b[i(y41.ae(this) + this.a)];
    }

    public final Object first() {
        if (isEmpty()) {
            throw new NoSuchElementException("ArrayDeque is empty.");
        }
        return this.b[this.a];
    }

    public final int g(int i) {
        return i < 0 ? i + this.b.length : i;
    }

    @Override // java.util.AbstractList, java.util.List
    public final Object get(int i) {
        cs.a.b(i, this.c);
        return this.b[i(this.a + i)];
    }

    public final void h(int i, int i2) {
        if (i < i2) {
            st.s(this.b, null, i, i2);
            return;
        }
        Object[] objArr = this.b;
        st.s(objArr, null, i, objArr.length);
        st.s(this.b, null, 0, i2);
    }

    public final int i(int i) {
        Object[] objArr = this.b;
        return i >= objArr.length ? i - objArr.length : i;
    }

    @Override // java.util.AbstractList, java.util.List
    public final int indexOf(Object obj) {
        int i;
        int i2 = i(getB() + this.a);
        int length = this.a;
        if (length < i2) {
            while (length < i2) {
                if (cr4.b(obj, this.b[length])) {
                    i = this.a;
                } else {
                    length++;
                }
            }
            return -1;
        }
        if (length < i2) {
            return -1;
        }
        int length2 = this.b.length;
        while (true) {
            if (length >= length2) {
                for (int i3 = 0; i3 < i2; i3++) {
                    if (cr4.b(obj, this.b[i3])) {
                        length = i3 + this.b.length;
                        i = this.a;
                    }
                }
                return -1;
            }
            if (cr4.b(obj, this.b[length])) {
                i = this.a;
                break;
            }
            length++;
        }
        return length - i;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean isEmpty() {
        return getB() == 0;
    }

    public final void j() {
        ((AbstractList) this).modCount++;
    }

    public final Object last() {
        if (isEmpty()) {
            throw new NoSuchElementException("ArrayDeque is empty.");
        }
        return this.b[i(y41.ae(this) + this.a)];
    }

    @Override // java.util.AbstractList, java.util.List
    public final int lastIndexOf(Object obj) {
        int length;
        int i;
        int i2 = i(this.c + this.a);
        int i3 = this.a;
        if (i3 < i2) {
            length = i2 - 1;
            if (i3 <= length) {
                while (!cr4.b(obj, this.b[length])) {
                    if (length != i3) {
                        length--;
                    }
                }
                i = this.a;
                return length - i;
            }
            return -1;
        }
        if (i3 > i2) {
            int i4 = i2 - 1;
            while (true) {
                if (-1 >= i4) {
                    Object[] objArr = this.b;
                    cr4.g(objArr, "<this>");
                    length = objArr.length - 1;
                    int i5 = this.a;
                    if (i5 <= length) {
                        while (!cr4.b(obj, this.b[length])) {
                            if (length != i5) {
                                length--;
                            }
                        }
                        i = this.a;
                    }
                } else {
                    if (cr4.b(obj, this.b[i4])) {
                        length = i4 + this.b.length;
                        i = this.a;
                        break;
                    }
                    i4--;
                }
            }
        }
        return -1;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean remove(Object obj) {
        int iIndexOf = indexOf(obj);
        if (iIndexOf == -1) {
            return false;
        }
        b(iIndexOf);
        return true;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean removeAll(Collection collection) {
        int i;
        cr4.g(collection, "elements");
        boolean z = false;
        z = false;
        z = false;
        if (!isEmpty() && this.b.length != 0) {
            int i2 = i(getB() + this.a);
            int i3 = this.a;
            if (i3 < i2) {
                i = i3;
                while (i3 < i2) {
                    Object obj = this.b[i3];
                    if (collection.contains(obj)) {
                        z = true;
                    } else {
                        this.b[i] = obj;
                        i++;
                    }
                    i3++;
                }
                st.s(this.b, null, i, i2);
            } else {
                int length = this.b.length;
                boolean z2 = false;
                int i4 = i3;
                while (i3 < length) {
                    Object[] objArr = this.b;
                    Object obj2 = objArr[i3];
                    objArr[i3] = null;
                    if (collection.contains(obj2)) {
                        z2 = true;
                    } else {
                        this.b[i4] = obj2;
                        i4++;
                    }
                    i3++;
                }
                i = i(i4);
                for (int i5 = 0; i5 < i2; i5++) {
                    Object[] objArr2 = this.b;
                    Object obj3 = objArr2[i5];
                    objArr2[i5] = null;
                    if (collection.contains(obj3)) {
                        z2 = true;
                    } else {
                        this.b[i] = obj3;
                        i = e(i);
                    }
                }
                z = z2;
            }
            if (z) {
                j();
                this.c = g(i - this.a);
            }
        }
        return z;
    }

    public final Object removeFirst() {
        if (isEmpty()) {
            throw new NoSuchElementException("ArrayDeque is empty.");
        }
        j();
        Object[] objArr = this.b;
        int i = this.a;
        Object obj = objArr[i];
        objArr[i] = null;
        this.a = e(i);
        this.c = getB() - 1;
        return obj;
    }

    public final Object removeLast() {
        if (isEmpty()) {
            throw new NoSuchElementException("ArrayDeque is empty.");
        }
        j();
        int i = i(y41.ae(this) + this.a);
        Object[] objArr = this.b;
        Object obj = objArr[i];
        objArr[i] = null;
        this.c = getB() - 1;
        return obj;
    }

    @Override // java.util.AbstractList
    public final void removeRange(int i, int i2) {
        cs.a.d(i, i2, this.c);
        int i3 = i2 - i;
        if (i3 == 0) {
            return;
        }
        if (i3 == this.c) {
            clear();
            return;
        }
        if (i3 == 1) {
            b(i);
            return;
        }
        j();
        if (i < this.c - i2) {
            int i4 = i((i - 1) + this.a);
            int i5 = i((i2 - 1) + this.a);
            while (i > 0) {
                int i6 = i4 + 1;
                int iMin = Math.min(i, Math.min(i6, i5 + 1));
                Object[] objArr = this.b;
                int i7 = i5 - iMin;
                int i8 = i4 - iMin;
                st.n(objArr, i7 + 1, objArr, i8 + 1, i6);
                i4 = g(i8);
                i5 = g(i7);
                i -= iMin;
            }
            int i9 = i(this.a + i3);
            h(this.a, i9);
            this.a = i9;
        } else {
            int i10 = i(this.a + i2);
            int i11 = i(this.a + i);
            int i12 = this.c;
            while (true) {
                i12 -= i2;
                if (i12 <= 0) {
                    break;
                }
                Object[] objArr2 = this.b;
                i2 = Math.min(i12, Math.min(objArr2.length - i10, objArr2.length - i11));
                Object[] objArr3 = this.b;
                int i13 = i10 + i2;
                st.n(objArr3, i11, objArr3, i10, i13);
                i10 = i(i13);
                i11 = i(i11 + i2);
            }
            int i14 = i(this.c + this.a);
            h(g(i14 - i3), i14);
        }
        this.c -= i3;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean retainAll(Collection collection) {
        int i;
        cr4.g(collection, "elements");
        boolean z = false;
        z = false;
        z = false;
        if (!isEmpty() && this.b.length != 0) {
            int i2 = i(getB() + this.a);
            int i3 = this.a;
            if (i3 < i2) {
                i = i3;
                while (i3 < i2) {
                    Object obj = this.b[i3];
                    if (collection.contains(obj)) {
                        this.b[i] = obj;
                        i++;
                    } else {
                        z = true;
                    }
                    i3++;
                }
                st.s(this.b, null, i, i2);
            } else {
                int length = this.b.length;
                boolean z2 = false;
                int i4 = i3;
                while (i3 < length) {
                    Object[] objArr = this.b;
                    Object obj2 = objArr[i3];
                    objArr[i3] = null;
                    if (collection.contains(obj2)) {
                        this.b[i4] = obj2;
                        i4++;
                    } else {
                        z2 = true;
                    }
                    i3++;
                }
                i = i(i4);
                for (int i5 = 0; i5 < i2; i5++) {
                    Object[] objArr2 = this.b;
                    Object obj3 = objArr2[i5];
                    objArr2[i5] = null;
                    if (collection.contains(obj3)) {
                        this.b[i] = obj3;
                        i = e(i);
                    } else {
                        z2 = true;
                    }
                }
                z = z2;
            }
            if (z) {
                j();
                this.c = g(i - this.a);
            }
        }
        return z;
    }

    @Override // java.util.AbstractList, java.util.List
    public final Object set(int i, Object obj) {
        cs.a.b(i, this.c);
        int i2 = i(this.a + i);
        Object[] objArr = this.b;
        Object obj2 = objArr[i2];
        objArr[i2] = obj;
        return obj2;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final Object[] toArray() {
        return toArray(new Object[getB()]);
    }

    public ds(int i) {
        Object[] objArr;
        if (i == 0) {
            objArr = d;
        } else if (i > 0) {
            objArr = new Object[i];
        } else {
            throw new IllegalArgumentException(n41.k("Illegal Capacity: ", i));
        }
        this.b = objArr;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final Object[] toArray(Object[] objArr) {
        cr4.g(objArr, "array");
        int length = objArr.length;
        int i = this.c;
        if (length < i) {
            Object objNewInstance = Array.newInstance(objArr.getClass().getComponentType(), i);
            cr4.e(objNewInstance, "null cannot be cast to non-null type kotlin.Array<T of kotlin.collections.ArraysKt__ArraysJVMKt.arrayOfNulls>");
            objArr = (Object[]) objNewInstance;
        }
        int i2 = i(this.c + this.a);
        int i3 = this.a;
        if (i3 < i2) {
            st.o(this.b, i3, objArr, i2, 2);
        } else if (!isEmpty()) {
            Object[] objArr2 = this.b;
            st.n(objArr2, 0, objArr, this.a, objArr2.length);
            Object[] objArr3 = this.b;
            st.n(objArr3, objArr3.length - this.a, objArr, 0, i2);
        }
        int i4 = this.c;
        if (i4 < objArr.length) {
            objArr[i4] = null;
        }
        return objArr;
    }

    @Override // java.util.AbstractList, java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean add(Object obj) {
        addLast(obj);
        return true;
    }

    @Override // java.util.AbstractCollection, java.util.Collection, java.util.List
    public final boolean addAll(Collection collection) {
        cr4.g(collection, "elements");
        if (collection.isEmpty()) {
            return false;
        }
        j();
        d(collection.size() + getB());
        c(i(getB() + this.a), collection);
        return true;
    }
}
