package com.google.firebase.firestore.remote;

import C0.D;
import com.google.firebase.firestore.local.TargetData;
import com.google.firebase.firestore.model.SnapshotVersion;
import com.google.firebase.firestore.remote.Stream;
import com.google.firebase.firestore.util.Assert;
import com.google.firebase.firestore.util.AsyncQueue;
import com.google.protobuf.AbstractC1320n;
import com.google.protobuf.C1343z;
import e4.C;
import e4.F;
import e4.G;
import java.util.Map;
import k4.h0;
import k4.i0;
import r4.AbstractC2890c;
import r4.C2889b;

/* JADX INFO: loaded from: classes3.dex */
public class WatchStream extends AbstractStream<F, G, Callback> {
    public static final AbstractC1320n EMPTY_RESUME_TOKEN = AbstractC1320n.f14310b;
    private final RemoteSerializer serializer;

    public interface Callback extends Stream.StreamCallback {
        void onWatchChange(SnapshotVersion snapshotVersion, WatchChange watchChange);
    }

    /* JADX WARN: Illegal instructions before constructor call */
    public WatchStream(FirestoreChannel firestoreChannel, AsyncQueue asyncQueue, RemoteSerializer remoteSerializer, Callback callback) {
        i0 i0VarB = C.f16284e;
        if (i0VarB == null) {
            synchronized (C.class) {
                try {
                    i0VarB = C.f16284e;
                    if (i0VarB == null) {
                        D dB = i0.b();
                        dB.f521d = h0.f19876c;
                        dB.f522e = i0.a("google.firestore.v1.Firestore", "Listen");
                        dB.f518a = true;
                        F fJ = F.j();
                        C1343z c1343z = AbstractC2890c.f24005a;
                        dB.f519b = new C2889b(fJ);
                        dB.f520c = new C2889b(G.f());
                        i0VarB = dB.b();
                        C.f16284e = i0VarB;
                    }
                } finally {
                }
            }
        }
        super(firestoreChannel, i0VarB, asyncQueue, AsyncQueue.TimerId.LISTEN_STREAM_CONNECTION_BACKOFF, AsyncQueue.TimerId.LISTEN_STREAM_IDLE, AsyncQueue.TimerId.HEALTH_CHECK_TIMEOUT, callback);
        this.serializer = remoteSerializer;
    }

    @Override // com.google.firebase.firestore.remote.AbstractStream, com.google.firebase.firestore.remote.Stream
    public /* bridge */ /* synthetic */ void inhibitBackoff() {
        super.inhibitBackoff();
    }

    @Override // com.google.firebase.firestore.remote.AbstractStream, com.google.firebase.firestore.remote.Stream
    public /* bridge */ /* synthetic */ boolean isOpen() {
        return super.isOpen();
    }

    @Override // com.google.firebase.firestore.remote.AbstractStream, com.google.firebase.firestore.remote.Stream
    public /* bridge */ /* synthetic */ boolean isStarted() {
        return super.isStarted();
    }

    @Override // com.google.firebase.firestore.remote.AbstractStream, com.google.firebase.firestore.remote.Stream
    public /* bridge */ /* synthetic */ void start() {
        super.start();
    }

    @Override // com.google.firebase.firestore.remote.AbstractStream, com.google.firebase.firestore.remote.Stream
    public /* bridge */ /* synthetic */ void stop() {
        super.stop();
    }

    public void unwatchTarget(int i6) {
        Assert.hardAssert(isOpen(), "Unwatching targets requires an open stream", new Object[0]);
        e4.D dK = F.k();
        dK.e(this.serializer.databaseName());
        dK.f(i6);
        writeRequest((F) dK.m359build());
    }

    public void watchQuery(TargetData targetData) {
        Assert.hardAssert(isOpen(), "Watching queries requires an open stream", new Object[0]);
        e4.D dK = F.k();
        dK.e(this.serializer.databaseName());
        dK.d(this.serializer.encodeTarget(targetData));
        Map<String, String> mapEncodeListenRequestLabels = this.serializer.encodeListenRequestLabels(targetData);
        if (mapEncodeListenRequestLabels != null) {
            dK.c(mapEncodeListenRequestLabels);
        }
        writeRequest((F) dK.m359build());
    }

    @Override // com.google.firebase.firestore.remote.AbstractStream
    public void onFirst(G g6) {
        onNext(g6);
    }

    @Override // com.google.firebase.firestore.remote.AbstractStream
    public void onNext(G g6) {
        this.backoff.reset();
        WatchChange watchChangeDecodeWatchChange = this.serializer.decodeWatchChange(g6);
        ((Callback) this.listener).onWatchChange(this.serializer.decodeVersionFromListenResponse(g6), watchChangeDecodeWatchChange);
    }
}
