package y0;

import java.util.ArrayList;
import java.util.List;
import rm.x;
import vm.g;
import y0.g1;

/* loaded from: classes.dex */
public final class h implements g1 {

    /* renamed from: p, reason: collision with root package name */
    private final dn.a f51546p;

    /* renamed from: r, reason: collision with root package name */
    private Throwable f51548r;

    /* renamed from: q, reason: collision with root package name */
    private final Object f51547q = new Object();

    /* renamed from: s, reason: collision with root package name */
    private List f51549s = new ArrayList();

    /* renamed from: t, reason: collision with root package name */
    private List f51550t = new ArrayList();

    /* renamed from: u, reason: collision with root package name */
    private final g f51551u = new g(0);

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static final class a {

        /* renamed from: a, reason: collision with root package name */
        private final dn.l f51552a;

        /* renamed from: b, reason: collision with root package name */
        private final vm.d f51553b;

        public a(dn.l lVar, vm.d dVar) {
            this.f51552a = lVar;
            this.f51553b = dVar;
        }

        public final vm.d a() {
            return this.f51553b;
        }

        public final void b(long j10) {
            Object a10;
            vm.d dVar = this.f51553b;
            try {
                x.a aVar = rm.x.f34910p;
                a10 = rm.x.a(this.f51552a.invoke(Long.valueOf(j10)));
            } catch (Throwable th2) {
                x.a aVar2 = rm.x.f34910p;
                a10 = rm.x.a(rm.y.a(th2));
            }
            dVar.resumeWith(a10);
        }
    }

    /* loaded from: classes.dex */
    static final class b extends kotlin.jvm.internal.v implements dn.l {

        /* renamed from: q, reason: collision with root package name */
        final /* synthetic */ a f51555q;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        b(a aVar) {
            super(1);
            this.f51555q = aVar;
        }

        public final void a(Throwable th2) {
            Object obj = h.this.f51547q;
            h hVar = h.this;
            a aVar = this.f51555q;
            synchronized (obj) {
                hVar.f51549s.remove(aVar);
                if (hVar.f51549s.isEmpty()) {
                    hVar.f51551u.set(0);
                }
                rm.n0 n0Var = rm.n0.f34904a;
            }
        }

        @Override // dn.l
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            a((Throwable) obj);
            return rm.n0.f34904a;
        }
    }

    public h(dn.a aVar) {
        this.f51546p = aVar;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void o(Throwable th2) {
        synchronized (this.f51547q) {
            if (this.f51548r != null) {
                return;
            }
            this.f51548r = th2;
            List list = this.f51549s;
            int size = list.size();
            for (int i10 = 0; i10 < size; i10++) {
                vm.d a10 = ((a) list.get(i10)).a();
                x.a aVar = rm.x.f34910p;
                a10.resumeWith(rm.x.a(rm.y.a(th2)));
            }
            this.f51549s.clear();
            this.f51551u.set(0);
            rm.n0 n0Var = rm.n0.f34904a;
        }
    }

    @Override // y0.g1
    public Object J(dn.l lVar, vm.d dVar) {
        vm.d d10;
        Object f10;
        d10 = wm.c.d(dVar);
        tp.p pVar = new tp.p(d10, 1);
        pVar.u();
        a aVar = new a(lVar, pVar);
        synchronized (this.f51547q) {
            Throwable th2 = this.f51548r;
            if (th2 != null) {
                x.a aVar2 = rm.x.f34910p;
                pVar.resumeWith(rm.x.a(rm.y.a(th2)));
            } else {
                boolean z10 = !this.f51549s.isEmpty();
                this.f51549s.add(aVar);
                if (!z10) {
                    this.f51551u.set(1);
                }
                boolean z11 = true ^ z10;
                pVar.x(new b(aVar));
                if (z11 && this.f51546p != null) {
                    try {
                        this.f51546p.invoke();
                    } catch (Throwable th3) {
                        o(th3);
                    }
                }
            }
        }
        Object r10 = pVar.r();
        f10 = wm.d.f();
        if (r10 == f10) {
            kotlin.coroutines.jvm.internal.h.c(dVar);
        }
        return r10;
    }

    @Override // vm.g.b, vm.g
    public g.b d(g.c cVar) {
        return g1.a.b(this, cVar);
    }

    @Override // vm.g
    public vm.g h(vm.g gVar) {
        return g1.a.d(this, gVar);
    }

    public final boolean q() {
        return this.f51551u.get() != 0;
    }

    @Override // vm.g
    public vm.g r(g.c cVar) {
        return g1.a.c(this, cVar);
    }

    public final void u(long j10) {
        synchronized (this.f51547q) {
            List list = this.f51549s;
            this.f51549s = this.f51550t;
            this.f51550t = list;
            this.f51551u.set(0);
            int size = list.size();
            for (int i10 = 0; i10 < size; i10++) {
                ((a) list.get(i10)).b(j10);
            }
            list.clear();
            rm.n0 n0Var = rm.n0.f34904a;
        }
    }

    @Override // vm.g
    public Object y(Object obj, dn.p pVar) {
        return g1.a.a(this, obj, pVar);
    }
}
