package o; import android.R; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; /* loaded from: classes6.dex */ public final class fVW extends fUX { private fSS> a; private boolean d; public fVW(InterfaceC13169fSq interfaceC13169fSq, fSS> fss, boolean z) { super(interfaceC13169fSq); this.a = fss; this.d = z; } @Override // o.AbstractC13163fSk public final void subscribeActual(InterfaceC13174fSv interfaceC13174fSv) { this.c.subscribe(new LWm(interfaceC13174fSv, this.a, this.d)); } /* loaded from: classes6.dex */ static final class LWm extends AtomicInteger implements InterfaceC13174fSv, fSJ { final boolean a; final InterfaceC13174fSv e; fSJ h; private volatile boolean i; private fSS> j; final fSC g = new fSC(); final C13355fYI d = new C13355fYI(); final AtomicInteger c = new AtomicInteger(1); final AtomicReference> b = new AtomicReference<>(); LWm(InterfaceC13174fSv interfaceC13174fSv, fSS> fss, boolean z) { this.e = interfaceC13174fSv; this.j = fss; this.a = z; } @Override // o.InterfaceC13174fSv public final void onSubscribe(fSJ fsj) { if (EnumC13192fTf.d(this.h, fsj)) { this.h = fsj; this.e.onSubscribe(this); } } @Override // o.InterfaceC13174fSv public final void onNext(T t) { try { InterfaceC13160fSh interfaceC13160fSh = (InterfaceC13160fSh) Objects.requireNonNull(this.j.a(t), "The mapper returned a null MaybeSource"); this.c.getAndIncrement(); Sts sts = new Sts(this); if (this.i || !this.g.e(sts)) { return; } interfaceC13160fSh.c(sts); } catch (Throwable th) { C13147fSI.c(th); this.h.dispose(); onError(th); } } @Override // o.InterfaceC13174fSv public final void onError(Throwable th) { this.c.decrementAndGet(); if (fYR.c(this.d, th)) { if (!this.a) { this.g.dispose(); } if (getAndIncrement() == 0) { e(); return; } return; } C13418fZu.c(th); } @Override // o.InterfaceC13174fSv public final void onComplete() { this.c.decrementAndGet(); if (getAndIncrement() == 0) { e(); } } @Override // o.fSJ public final void dispose() { this.i = true; this.h.dispose(); this.g.dispose(); Throwable b = fYR.b(this.d); if (b == null || b == fYR.b) { return; } C13418fZu.c(b); } final void e() { InterfaceC13174fSv interfaceC13174fSv = this.e; AtomicInteger atomicInteger = this.c; AtomicReference> atomicReference = this.b; int i = 1; while (!this.i) { if (this.a || this.d.get() == null) { boolean z = atomicInteger.get() == 0; C13416fZs c13416fZs = atomicReference.get(); R.color b = c13416fZs != null ? c13416fZs.b() : null; boolean z2 = b == null; if (z && z2) { Throwable b2 = fYR.b(this.d); if (b2 == null) { interfaceC13174fSv.onComplete(); return; } else { if (b2 != fYR.b) { interfaceC13174fSv.onError(b2); return; } return; } } if (!z2) { interfaceC13174fSv.onNext(b); } else { i = addAndGet(-i); if (i == 0) { return; } } } else { C13416fZs c13416fZs2 = this.b.get(); if (c13416fZs2 != null) { c13416fZs2.e(); } Throwable b3 = fYR.b(this.d); if (b3 == null) { interfaceC13174fSv.onComplete(); return; } else { if (b3 != fYR.b) { interfaceC13174fSv.onError(b3); return; } return; } } } C13416fZs c13416fZs3 = this.b.get(); if (c13416fZs3 != null) { c13416fZs3.e(); } } /* loaded from: classes6.dex */ final class Sts extends AtomicReference implements InterfaceC13161fSi, fSJ { private LWm d; Sts(LWm lWm) { this.d = lWm; } @Override // o.InterfaceC13161fSi public final void onSubscribe(fSJ fsj) { EnumC13192fTf.a(this, fsj); } @Override // o.InterfaceC13161fSi public final void e(R r) { LWm lWm = this.d; lWm.g.b(this); if (lWm.get() == 0) { if (lWm.compareAndSet(0, 1)) { lWm.e.onNext(r); boolean z = lWm.c.decrementAndGet() == 0; C13416fZs c13416fZs = lWm.b.get(); if (z && (c13416fZs == null || c13416fZs.b.get() == c13416fZs.e.get())) { C13355fYI c13355fYI = lWm.d; InterfaceC13174fSv interfaceC13174fSv = lWm.e; Throwable b = fYR.b(c13355fYI); if (b == null) { interfaceC13174fSv.onComplete(); return; } else { if (b != fYR.b) { interfaceC13174fSv.onError(b); return; } return; } } if (lWm.decrementAndGet() == 0) { return; } lWm.e(); } } C13416fZs c13416fZs2 = lWm.b.get(); if (c13416fZs2 == null) { c13416fZs2 = new C13416fZs<>(AbstractC13163fSk.bufferSize()); AtomicReference> atomicReference = lWm.b; while (true) { if (atomicReference.compareAndSet(null, c13416fZs2)) { break; } else if (atomicReference.get() != null) { c13416fZs2 = lWm.b.get(); break; } } } synchronized (c13416fZs2) { c13416fZs2.b(r); } lWm.c.decrementAndGet(); if (lWm.getAndIncrement() != 0) { return; } lWm.e(); } @Override // o.InterfaceC13161fSi public final void onError(Throwable th) { LWm lWm = this.d; lWm.g.b(this); if (fYR.c(lWm.d, th)) { if (!lWm.a) { lWm.h.dispose(); lWm.g.dispose(); } lWm.c.decrementAndGet(); if (lWm.getAndIncrement() == 0) { lWm.e(); return; } return; } C13418fZu.c(th); } @Override // o.InterfaceC13161fSi public final void onComplete() { LWm lWm = this.d; lWm.g.b(this); if (lWm.get() == 0) { if (lWm.compareAndSet(0, 1)) { boolean z = lWm.c.decrementAndGet() == 0; C13416fZs c13416fZs = lWm.b.get(); if (z && (c13416fZs == null || c13416fZs.b.get() == c13416fZs.e.get())) { C13355fYI c13355fYI = lWm.d; InterfaceC13174fSv interfaceC13174fSv = lWm.e; Throwable b = fYR.b(c13355fYI); if (b == null) { interfaceC13174fSv.onComplete(); return; } else { if (b != fYR.b) { interfaceC13174fSv.onError(b); return; } return; } } if (lWm.decrementAndGet() != 0) { lWm.e(); return; } return; } } lWm.c.decrementAndGet(); if (lWm.getAndIncrement() == 0) { lWm.e(); } } @Override // o.fSJ public final boolean isDisposed() { return EnumC13192fTf.c(get()); } @Override // o.fSJ public final void dispose() { EnumC13192fTf.e(this); } } @Override // o.fSJ public final boolean isDisposed() { return this.i; } } }