package com.google.firebase.messaging;

import android.content.Context;
import android.os.Bundle;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/* JADX INFO: loaded from: classes2.dex */
public final class j0 {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final long f24195i = TimeUnit.HOURS.toSeconds(8);

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final /* synthetic */ int f24196j = 0;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f24197a;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final p f24199c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final FirebaseMessaging f24200d;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final androidx.collection.f f24201e = new androidx.collection.f(0);

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public boolean f24203g = false;

    public j0(FirebaseMessaging firebaseMessaging, s sVar, h0 h0Var, p pVar, Context context, ScheduledExecutorService scheduledExecutorService) {
        this.f24200d = firebaseMessaging;
        this.f24198b = sVar;
        this.f24204h = h0Var;
        this.f24199c = pVar;
        this.f24197a = context;
        this.f24202f = scheduledExecutorService;
    }

    public static void a(w8.t tVar) throws IOException {
        try {
            w8.k.b(tVar, 30L, TimeUnit.SECONDS);
        } catch (InterruptedException e10) {
            e = e10;
            throw new IOException("SERVICE_NOT_AVAILABLE", e);
        } catch (ExecutionException e11) {
            Throwable cause = e11.getCause();
            if (cause instanceof IOException) {
                throw ((IOException) cause);
            }
            if (!(cause instanceof RuntimeException)) {
                throw new IOException(e11);
            }
            throw ((RuntimeException) cause);
        } catch (TimeoutException e12) {
            e = e12;
            throw new IOException("SERVICE_NOT_AVAILABLE", e);
        }
    }

    public final void b(String str) throws IOException {
        String strA = this.f24200d.a();
        p pVar = this.f24199c;
        pVar.getClass();
        Bundle bundle = new Bundle();
        bundle.putString("gcm.topic", "/topics/" + str);
        a(pVar.a(pVar.c(strA, "/topics/" + str, bundle)));
    }

    public final void c(String str) throws IOException {
        String strA = this.f24200d.a();
        p pVar = this.f24199c;
        pVar.getClass();
        Bundle bundle = new Bundle();
        bundle.putString("gcm.topic", "/topics/" + str);
        bundle.putString("delete", "1");
        a(pVar.a(pVar.c(strA, "/topics/" + str, bundle)));
    }

    /* JADX WARN: Removed duplicated region for block: B:24:0x0036  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean d() throws IOException {
        g0 nextTopicOperation;
        byte b10;
        while (true) {
            synchronized (this) {
                try {
                    nextTopicOperation = this.f24204h.getNextTopicOperation();
                    if (nextTopicOperation == null) {
                        return true;
                    }
                } finally {
                }
            }
            try {
                String str = nextTopicOperation.f24175b;
                int iHashCode = str.hashCode();
                if (iHashCode != 83) {
                    b10 = (iHashCode == 85 && str.equals("U")) ? (byte) 1 : (byte) -1;
                } else if (str.equals("S")) {
                    b10 = 0;
                }
                String str2 = nextTopicOperation.f24174a;
                if (b10 == 0) {
                    b(str2);
                } else if (b10 == 1) {
                    c(str2);
                }
                h0 h0Var = this.f24204h;
                synchronized (h0Var) {
                    h0Var.f24181a.remove(nextTopicOperation.f24176c);
                }
                synchronized (this.f24201e) {
                    try {
                        String str3 = nextTopicOperation.f24176c;
                        if (this.f24201e.containsKey(str3)) {
                            ArrayDeque arrayDeque = (ArrayDeque) this.f24201e.get(str3);
                            w8.i iVar = (w8.i) arrayDeque.poll();
                            if (iVar != null) {
                                iVar.setResult(null);
                            }
                            if (arrayDeque.isEmpty()) {
                                this.f24201e.remove(str3);
                            }
                        }
                    } finally {
                    }
                }
            } catch (IOException e10) {
                if ("SERVICE_NOT_AVAILABLE".equals(e10.getMessage()) || "INTERNAL_SERVER_ERROR".equals(e10.getMessage()) || "TOO_MANY_SUBSCRIBERS".equals(e10.getMessage())) {
                    e10.getMessage();
                } else if (e10.getMessage() != null) {
                    throw e10;
                }
                return false;
            }
        }
    }

    public final void e(long j10) {
        this.f24202f.schedule(new l0(this, this.f24197a, this.f24198b, Math.min(Math.max(30L, 2 * j10), f24195i)), j10, TimeUnit.SECONDS);
        synchronized (this) {
            this.f24203g = true;
        }
    }
}
