package f.a.w.e.d;

import android.R;
import java.util.ArrayDeque;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: compiled from: ObservableFlatMap.java */
/* JADX INFO: loaded from: classes.dex */
public final class l<T, U> extends f.a.w.e.d.a<T, U> {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final f.a.v.d<? super T, ? extends f.a.l<? extends U>> f3839e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final boolean f3840f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final int f3841g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final int f3842h;

    /* JADX INFO: compiled from: ObservableFlatMap.java */
    public static final class a<T, U> extends AtomicReference<f.a.t.b> implements f.a.n<U> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final long f3843b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final b<T, U> f3844e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public volatile boolean f3845f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        public volatile f.a.w.c.d<U> f3846g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        public int f3847h;

        public a(b<T, U> bVar, long j2) {
            this.f3843b = j2;
            this.f3844e = bVar;
        }

        @Override // f.a.n
        public void a(Throwable th) {
            if (!this.f3844e.m.a(th)) {
                f.a.y.a.C0(th);
                return;
            }
            b<T, U> bVar = this.f3844e;
            if (!bVar.f3852h) {
                bVar.e();
            }
            this.f3845f = true;
            this.f3844e.f();
        }

        @Override // f.a.n
        public void b(f.a.t.b bVar) {
            if (f.a.w.a.b.setOnce(this, bVar) && (bVar instanceof f.a.w.c.a)) {
                f.a.w.c.a aVar = (f.a.w.c.a) bVar;
                int iRequestFusion = aVar.requestFusion(7);
                if (iRequestFusion == 1) {
                    this.f3847h = iRequestFusion;
                    this.f3846g = aVar;
                    this.f3845f = true;
                    this.f3844e.f();
                    return;
                }
                if (iRequestFusion == 2) {
                    this.f3847h = iRequestFusion;
                    this.f3846g = aVar;
                }
            }
        }

        @Override // f.a.n
        public void c(U u) {
            if (this.f3847h != 0) {
                this.f3844e.f();
                return;
            }
            b<T, U> bVar = this.f3844e;
            if (bVar.get() == 0 && bVar.compareAndSet(0, 1)) {
                bVar.f3850f.c(u);
                if (bVar.decrementAndGet() == 0) {
                    return;
                }
            } else {
                f.a.w.c.d cVar = this.f3846g;
                if (cVar == null) {
                    cVar = new f.a.w.f.c(bVar.f3854j);
                    this.f3846g = cVar;
                }
                cVar.offer(u);
                if (bVar.getAndIncrement() != 0) {
                    return;
                }
            }
            bVar.g();
        }

        @Override // f.a.n
        public void onComplete() {
            this.f3845f = true;
            this.f3844e.f();
        }
    }

    /* JADX INFO: compiled from: ObservableFlatMap.java */
    public static final class b<T, U> extends AtomicInteger implements f.a.t.b, f.a.n<T> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public static final a<?, ?>[] f3848b = new a[0];

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public static final a<?, ?>[] f3849e = new a[0];

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final f.a.n<? super U> f3850f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        public final f.a.v.d<? super T, ? extends f.a.l<? extends U>> f3851g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        public final boolean f3852h;

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        public final int f3853i;

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        public final int f3854j;

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        public volatile f.a.w.c.c<U> f3855k;

        /* JADX INFO: renamed from: l, reason: collision with root package name */
        public volatile boolean f3856l;
        public final f.a.w.h.b m = new f.a.w.h.b();
        public volatile boolean n;
        public final AtomicReference<a<?, ?>[]> o;
        public f.a.t.b p;
        public long q;
        public long r;
        public int s;
        public Queue<f.a.l<? extends U>> t;
        public int u;

        public b(f.a.n<? super U> nVar, f.a.v.d<? super T, ? extends f.a.l<? extends U>> dVar, boolean z, int i2, int i3) {
            this.f3850f = nVar;
            this.f3851g = dVar;
            this.f3852h = z;
            this.f3853i = i2;
            this.f3854j = i3;
            if (i2 != Integer.MAX_VALUE) {
                this.t = new ArrayDeque(i2);
            }
            this.o = new AtomicReference<>(f3848b);
        }

        @Override // f.a.n
        public void a(Throwable th) {
            if (this.f3856l) {
                f.a.y.a.C0(th);
            } else if (!this.m.a(th)) {
                f.a.y.a.C0(th);
            } else {
                this.f3856l = true;
                f();
            }
        }

        @Override // f.a.n
        public void b(f.a.t.b bVar) {
            if (f.a.w.a.b.validate(this.p, bVar)) {
                this.p = bVar;
                this.f3850f.b(this);
            }
        }

        @Override // f.a.n
        public void c(T t) {
            if (this.f3856l) {
                return;
            }
            try {
                f.a.l<? extends U> lVarApply = this.f3851g.apply(t);
                Objects.requireNonNull(lVarApply, "The mapper returned a null ObservableSource");
                f.a.l<? extends U> lVar = lVarApply;
                if (this.f3853i != Integer.MAX_VALUE) {
                    synchronized (this) {
                        int i2 = this.u;
                        if (i2 == this.f3853i) {
                            this.t.offer(lVar);
                            return;
                        }
                        this.u = i2 + 1;
                    }
                }
                i(lVar);
            } catch (Throwable th) {
                d.i.c.a.L(th);
                this.p.dispose();
                a(th);
            }
        }

        public boolean d() {
            if (this.n) {
                return true;
            }
            Throwable th = this.m.get();
            if (this.f3852h || th == null) {
                return false;
            }
            e();
            Throwable thB = this.m.b();
            if (thB != f.a.w.h.c.a) {
                this.f3850f.a(thB);
            }
            return true;
        }

        @Override // f.a.t.b
        public void dispose() {
            Throwable thB;
            if (this.n) {
                return;
            }
            this.n = true;
            if (!e() || (thB = this.m.b()) == null || thB == f.a.w.h.c.a) {
                return;
            }
            f.a.y.a.C0(thB);
        }

        public boolean e() {
            a<?, ?>[] andSet;
            this.p.dispose();
            a<?, ?>[] aVarArr = this.o.get();
            a<?, ?>[] aVarArr2 = f3849e;
            if (aVarArr == aVarArr2 || (andSet = this.o.getAndSet(aVarArr2)) == aVarArr2) {
                return false;
            }
            for (a<?, ?> aVar : andSet) {
                Objects.requireNonNull(aVar);
                f.a.w.a.b.dispose(aVar);
            }
            return true;
        }

        public void f() {
            if (getAndIncrement() == 0) {
                g();
            }
        }

        /* JADX WARN: Code restructure failed: missing block: B:128:0x0004, code lost:
        
            continue;
         */
        /* JADX WARN: Multi-variable type inference failed */
        /* JADX WARN: Removed duplicated region for block: B:120:0x00ea A[SYNTHETIC] */
        /* JADX WARN: Removed duplicated region for block: B:133:0x00f2 A[SYNTHETIC] */
        /* JADX WARN: Removed duplicated region for block: B:82:0x00eb  */
        /* JADX WARN: Removed duplicated region for block: B:85:0x00f1 A[PHI: r4
          0x00f1: PHI (r4v10 int) = (r4v8 int), (r4v11 int) binds: [B:72:0x00d0, B:84:0x00ef] A[DONT_GENERATE, DONT_INLINE]] */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public void g() {
            /*
                Method dump skipped, instruction units count: 299
                To view this dump add '--comments-level debug' option
            */
            throw new UnsupportedOperationException("Method not decompiled: f.a.w.e.d.l.b.g():void");
        }

        /* JADX WARN: Multi-variable type inference failed */
        public void h(a<T, U> aVar) {
            a<?, ?>[] aVarArr;
            a<?, ?>[] aVarArr2;
            do {
                aVarArr = this.o.get();
                int length = aVarArr.length;
                if (length == 0) {
                    return;
                }
                int i2 = 0;
                while (true) {
                    if (i2 >= length) {
                        i2 = -1;
                        break;
                    } else if (aVarArr[i2] == aVar) {
                        break;
                    } else {
                        i2++;
                    }
                }
                if (i2 < 0) {
                    return;
                }
                if (length == 1) {
                    aVarArr2 = f3848b;
                } else {
                    a<?, ?>[] aVarArr3 = new a[length - 1];
                    System.arraycopy(aVarArr, 0, aVarArr3, 0, i2);
                    System.arraycopy(aVarArr, i2 + 1, aVarArr3, i2, (length - i2) - 1);
                    aVarArr2 = aVarArr3;
                }
            } while (!this.o.compareAndSet(aVarArr, aVarArr2));
        }

        /* JADX WARN: Multi-variable type inference failed */
        /* JADX WARN: Type inference failed for: r3v22 */
        /* JADX WARN: Type inference failed for: r3v23 */
        /* JADX WARN: Type inference failed for: r3v8, types: [f.a.w.c.d] */
        public void i(f.a.l<? extends U> lVar) {
            boolean z;
            boolean z2;
            Object objCall;
            do {
                z = false;
                if (!(lVar instanceof Callable)) {
                    long j2 = this.q;
                    this.q = 1 + j2;
                    a<?, ?> aVar = new a<>(this, j2);
                    while (true) {
                        a<?, ?>[] aVarArr = this.o.get();
                        if (aVarArr == f3849e) {
                            f.a.w.a.b.dispose(aVar);
                            break;
                        }
                        int length = aVarArr.length;
                        a<?, ?>[] aVarArr2 = new a[length + 1];
                        System.arraycopy(aVarArr, 0, aVarArr2, 0, length);
                        aVarArr2[length] = aVar;
                        if (this.o.compareAndSet(aVarArr, aVarArr2)) {
                            z = true;
                            break;
                        }
                    }
                    if (z) {
                        lVar.d(aVar);
                        return;
                    }
                    return;
                }
                try {
                    objCall = ((Callable) lVar).call();
                } catch (Throwable th) {
                    d.i.c.a.L(th);
                    this.m.a(th);
                    f();
                }
                if (objCall == null) {
                    z2 = true;
                } else if (get() == 0 && compareAndSet(0, 1)) {
                    this.f3850f.c(objCall);
                    if (decrementAndGet() != 0) {
                        g();
                    }
                    z2 = true;
                } else {
                    f.a.w.c.c<U> cVar = this.f3855k;
                    ?? r3 = cVar;
                    if (cVar == false) {
                        f.a.w.c.c<U> cVar2 = this.f3853i == Integer.MAX_VALUE ? new f.a.w.f.c(this.f3854j) : new f.a.w.f.b(this.f3853i);
                        this.f3855k = cVar2;
                        r3 = cVar2;
                    }
                    if (r3.offer(objCall)) {
                        if (getAndIncrement() != 0) {
                            z2 = false;
                        }
                        g();
                        z2 = true;
                    } else {
                        a(new IllegalStateException("Scalar queue full?!"));
                        z2 = true;
                    }
                }
                if (!z2 || this.f3853i == Integer.MAX_VALUE) {
                    return;
                }
                synchronized (this) {
                    lVar = this.t.poll();
                    if (lVar == null) {
                        this.u--;
                        z = true;
                    }
                }
            } while (!z);
            f();
        }

        @Override // f.a.n
        public void onComplete() {
            if (this.f3856l) {
                return;
            }
            this.f3856l = true;
            f();
        }
    }

    public l(f.a.l<T> lVar, f.a.v.d<? super T, ? extends f.a.l<? extends U>> dVar, boolean z, int i2, int i3) {
        super(lVar);
        this.f3839e = dVar;
        this.f3840f = z;
        this.f3841g = i2;
        this.f3842h = i3;
    }

    @Override // f.a.i
    public void v(f.a.n<? super U> nVar) {
        boolean z;
        f.a.l<T> lVar = this.f3744b;
        f.a.v.d<? super T, ? extends f.a.l<? extends U>> dVar = this.f3839e;
        if (lVar instanceof Callable) {
            z = true;
            try {
                R.attr attrVar = (Object) ((Callable) lVar).call();
                if (attrVar == null) {
                    f.a.w.a.c.complete(nVar);
                } else {
                    try {
                        f.a.l<? extends U> lVarApply = dVar.apply(attrVar);
                        Objects.requireNonNull(lVarApply, "The mapper returned a null ObservableSource");
                        f.a.l<? extends U> lVar2 = lVarApply;
                        if (lVar2 instanceof Callable) {
                            try {
                                Object objCall = ((Callable) lVar2).call();
                                if (objCall == null) {
                                    f.a.w.a.c.complete(nVar);
                                } else {
                                    b0 b0Var = new b0(nVar, objCall);
                                    nVar.b(b0Var);
                                    b0Var.run();
                                }
                            } catch (Throwable th) {
                                d.i.c.a.L(th);
                                f.a.w.a.c.error(th, nVar);
                            }
                        } else {
                            lVar2.d(nVar);
                        }
                    } catch (Throwable th2) {
                        d.i.c.a.L(th2);
                        f.a.w.a.c.error(th2, nVar);
                    }
                }
            } catch (Throwable th3) {
                d.i.c.a.L(th3);
                f.a.w.a.c.error(th3, nVar);
            }
        } else {
            z = false;
        }
        if (z) {
            return;
        }
        this.f3744b.d(new b(nVar, this.f3839e, this.f3840f, this.f3841g, this.f3842h));
    }
}
