package o; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; /* loaded from: classes6.dex */ public final class fXM extends fUX> { private InterfaceC13169fSq a; private fSS> b; private int e; public fXM(InterfaceC13169fSq interfaceC13169fSq, InterfaceC13169fSq interfaceC13169fSq2, fSS> fss, int i) { super(interfaceC13169fSq); this.a = interfaceC13169fSq2; this.b = fss; this.e = i; } @Override // o.AbstractC13163fSk public final void subscribeActual(InterfaceC13174fSv> interfaceC13174fSv) { this.c.subscribe(new HBt(interfaceC13174fSv, this.a, this.b, this.e)); } /* loaded from: classes6.dex */ static final class HBt extends AtomicInteger implements InterfaceC13174fSv, fSJ, Runnable { volatile boolean b; private int f; volatile boolean g; private InterfaceC13174fSv> h; fSJ i; private fSS> j; private InterfaceC13169fSq n; /* renamed from: o, reason: collision with root package name */ private volatile boolean f9122o; final InterfaceC13408fZk e = new C13378fYq(); final fSC c = new fSC(); private List> p = new ArrayList(); private AtomicLong l = new AtomicLong(1); private AtomicBoolean k = new AtomicBoolean(); final C13355fYI d = new C13355fYI(); final LWm a = new LWm<>(this); private AtomicLong m = new AtomicLong(); HBt(InterfaceC13174fSv> interfaceC13174fSv, InterfaceC13169fSq interfaceC13169fSq, fSS> fss, int i) { this.h = interfaceC13174fSv; this.n = interfaceC13169fSq; this.j = fss; this.f = i; } @Override // o.InterfaceC13174fSv public final void onSubscribe(fSJ fsj) { if (EnumC13192fTf.d(this.i, fsj)) { this.i = fsj; this.h.onSubscribe(this); this.n.subscribe(this.a); } } @Override // o.InterfaceC13174fSv public final void onNext(T t) { this.e.b(t); e(); } @Override // o.fSJ public final void dispose() { if (this.k.compareAndSet(false, true)) { if (this.l.decrementAndGet() == 0) { this.i.dispose(); EnumC13192fTf.e(this.a); this.c.dispose(); Throwable b = fYR.b(this.d); if (b != null && b != fYR.b) { C13418fZu.c(b); } this.f9122o = true; e(); return; } EnumC13192fTf.e(this.a); } } @Override // o.fSJ public final boolean isDisposed() { return this.k.get(); } @Override // java.lang.Runnable public final void run() { if (this.l.decrementAndGet() == 0) { this.i.dispose(); EnumC13192fTf.e(this.a); this.c.dispose(); Throwable b = fYR.b(this.d); if (b != null && b != fYR.b) { C13418fZu.c(b); } this.f9122o = true; e(); } } /* JADX WARN: Multi-variable type inference failed */ final void e() { if (getAndIncrement() != 0) { return; } InterfaceC13174fSv> interfaceC13174fSv = this.h; InterfaceC13408fZk interfaceC13408fZk = this.e; List> list = this.p; int i = 1; while (true) { if (this.f9122o) { interfaceC13408fZk.e(); list.clear(); } else { boolean z = this.g; Object b = interfaceC13408fZk.b(); boolean z2 = b == null; if (z && (z2 || this.d.get() != null)) { d(interfaceC13174fSv); this.f9122o = true; } else if (!z2) { if (b instanceof IeS) { if (!this.k.get()) { try { InterfaceC13169fSq interfaceC13169fSq = (InterfaceC13169fSq) Objects.requireNonNull(this.j.a(((IeS) b).e), "The closingIndicator returned a null ObservableSource"); this.l.getAndIncrement(); C13423fZz b2 = C13423fZz.b(this.f, this); Sts sts = new Sts(this, b2); interfaceC13174fSv.onNext(sts); if (!sts.b.get() && sts.b.compareAndSet(false, true)) { b2.onComplete(); } else { list.add(b2); this.c.e(sts); interfaceC13169fSq.subscribe(sts); } } catch (Throwable th) { C13147fSI.c(th); this.i.dispose(); EnumC13192fTf.e(this.a); this.c.dispose(); C13147fSI.c(th); if (!fYR.c(this.d, th)) { C13418fZu.c(th); } this.g = true; } } } else if (b instanceof Sts) { C13423fZz c13423fZz = ((Sts) b).d; list.remove(c13423fZz); this.c.b((fSJ) b); c13423fZz.onComplete(); } else { Iterator> it = list.iterator(); while (it.hasNext()) { it.next().onNext(b); } } } else if (this.b && list.size() == 0) { this.i.dispose(); EnumC13192fTf.e(this.a); this.c.dispose(); d(interfaceC13174fSv); this.f9122o = true; } } i = addAndGet(-i); if (i == 0) { return; } } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class IeS { final B e; IeS(B b) { this.e = b; } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class LWm extends AtomicReference implements InterfaceC13174fSv { private HBt e; LWm(HBt hBt) { this.e = hBt; } @Override // o.InterfaceC13174fSv public final void onSubscribe(fSJ fsj) { EnumC13192fTf.a(this, fsj); } @Override // o.InterfaceC13174fSv public final void onNext(B b) { HBt hBt = this.e; hBt.e.b(new IeS(b)); hBt.e(); } @Override // o.InterfaceC13174fSv public final void onError(Throwable th) { HBt hBt = this.e; hBt.i.dispose(); hBt.c.dispose(); if (fYR.c(hBt.d, th)) { hBt.g = true; hBt.e(); } else { C13418fZu.c(th); } } @Override // o.InterfaceC13174fSv public final void onComplete() { HBt hBt = this.e; hBt.b = true; hBt.e(); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class Sts extends AbstractC13163fSk implements InterfaceC13174fSv, fSJ { private AtomicReference a = new AtomicReference<>(); AtomicBoolean b = new AtomicBoolean(); private HBt c; final C13423fZz d; Sts(HBt hBt, C13423fZz c13423fZz) { this.c = hBt; this.d = c13423fZz; } @Override // o.InterfaceC13174fSv public final void onSubscribe(fSJ fsj) { EnumC13192fTf.a(this.a, fsj); } @Override // o.InterfaceC13174fSv public final void onNext(V v) { if (EnumC13192fTf.e(this.a)) { HBt hBt = this.c; hBt.e.b(this); hBt.e(); } } @Override // o.InterfaceC13174fSv public final void onError(Throwable th) { if (isDisposed()) { C13418fZu.c(th); return; } HBt hBt = this.c; hBt.i.dispose(); EnumC13192fTf.e(hBt.a); hBt.c.dispose(); if (fYR.c(hBt.d, th)) { hBt.g = true; hBt.e(); } else { C13418fZu.c(th); } } @Override // o.InterfaceC13174fSv public final void onComplete() { HBt hBt = this.c; hBt.e.b(this); hBt.e(); } @Override // o.fSJ public final void dispose() { EnumC13192fTf.e(this.a); } @Override // o.fSJ public final boolean isDisposed() { return this.a.get() == EnumC13192fTf.DISPOSED; } @Override // o.AbstractC13163fSk public final void subscribeActual(InterfaceC13174fSv interfaceC13174fSv) { this.d.subscribe(interfaceC13174fSv); this.b.set(true); } } @Override // o.InterfaceC13174fSv public final void onComplete() { EnumC13192fTf.e(this.a); this.c.dispose(); this.g = true; e(); } @Override // o.InterfaceC13174fSv public final void onError(Throwable th) { EnumC13192fTf.e(this.a); this.c.dispose(); if (fYR.c(this.d, th)) { this.g = true; e(); } else { C13418fZu.c(th); } } private void d(InterfaceC13174fSv interfaceC13174fSv) { Throwable b = fYR.b(this.d); if (b == null) { Iterator> it = this.p.iterator(); while (it.hasNext()) { it.next().onComplete(); } interfaceC13174fSv.onComplete(); return; } if (b != fYR.b) { Iterator> it2 = this.p.iterator(); while (it2.hasNext()) { it2.next().onError(b); } interfaceC13174fSv.onError(b); } } } }