package com.google.firebase.iid;

import android.text.TextUtils;
import android.util.Log;
import androidx.collection.ArrayMap;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import java.io.IOException;
import java.util.Map;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public final class zzaz {
    private final zzav zzar;
    private int zzdp = 0;
    private final Map<Integer, TaskCompletionSource<Void>> zzdq = new ArrayMap();

    /* JADX INFO: Access modifiers changed from: package-private */
    public zzaz(zzav zzavVar) {
        this.zzar = zzavVar;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final synchronized Task<Void> zza(String str) {
        String zzai;
        TaskCompletionSource<Void> taskCompletionSource;
        synchronized (this.zzar) {
            zzai = this.zzar.zzai();
            zzav zzavVar = this.zzar;
            StringBuilder sb = new StringBuilder(String.valueOf(zzai).length() + 1 + String.valueOf(str).length());
            sb.append(zzai);
            sb.append(",");
            sb.append(str);
            zzavVar.zzf(sb.toString());
        }
        taskCompletionSource = new TaskCompletionSource<>();
        this.zzdq.put(Integer.valueOf(this.zzdp + (TextUtils.isEmpty(zzai) ? 0 : zzai.split(",").length - 1)), taskCompletionSource);
        return taskCompletionSource.getTask();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final synchronized boolean zzao() {
        return zzap() != null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* JADX WARN: Code restructure failed: missing block: B:27:0x000c, code lost:
    
        if (com.google.firebase.iid.FirebaseInstanceId.zzm() == false) goto L8;
     */
    /* JADX WARN: Code restructure failed: missing block: B:28:0x000e, code lost:
    
        android.util.Log.d("FirebaseInstanceId", "topic sync succeeded");
     */
    /* JADX WARN: Code restructure failed: missing block: B:30:0x0016, code lost:
    
        return true;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean zzc(FirebaseInstanceId firebaseInstanceId) {
        TaskCompletionSource<Void> remove;
        while (true) {
            synchronized (this) {
                String zzap = zzap();
                if (zzap == null) {
                    break;
                }
                if (!zza(firebaseInstanceId, zzap)) {
                    return false;
                }
                synchronized (this) {
                    remove = this.zzdq.remove(Integer.valueOf(this.zzdp));
                    zzk(zzap);
                    this.zzdp++;
                }
                if (remove != null) {
                    remove.setResult(null);
                }
            }
        }
    }

    private final String zzap() {
        String zzai;
        synchronized (this.zzar) {
            zzai = this.zzar.zzai();
        }
        if (TextUtils.isEmpty(zzai)) {
            return null;
        }
        String[] split = zzai.split(",");
        if (split.length <= 1 || TextUtils.isEmpty(split[1])) {
            return null;
        }
        return split[1];
    }

    private final synchronized boolean zzk(String str) {
        synchronized (this.zzar) {
            String zzai = this.zzar.zzai();
            String valueOf = String.valueOf(str);
            if (!zzai.startsWith(valueOf.length() != 0 ? ",".concat(valueOf) : new String(","))) {
                return false;
            }
            String valueOf2 = String.valueOf(str);
            this.zzar.zzf(zzai.substring((valueOf2.length() != 0 ? ",".concat(valueOf2) : new String(",")).length()));
            return true;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:11:0x0035, code lost:
    
        if (r4 == 1) goto L19;
     */
    /* JADX WARN: Code restructure failed: missing block: B:13:0x0038, code lost:
    
        r7.zzc(r8);
     */
    /* JADX WARN: Code restructure failed: missing block: B:14:0x003f, code lost:
    
        if (com.google.firebase.iid.FirebaseInstanceId.zzm() == false) goto L33;
     */
    /* JADX WARN: Code restructure failed: missing block: B:15:0x0041, code lost:
    
        android.util.Log.d("FirebaseInstanceId", "unsubscribe operation succeeded");
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private static boolean zza(FirebaseInstanceId firebaseInstanceId, String str) {
        String[] split = str.split("!");
        if (split.length == 2) {
            String str2 = split[0];
            String str3 = split[1];
            char c = 65535;
            try {
                int hashCode = str2.hashCode();
                if (hashCode != 83) {
                    if (hashCode == 85 && str2.equals("U")) {
                        c = 1;
                    }
                } else if (str2.equals("S")) {
                    c = 0;
                }
                firebaseInstanceId.zzb(str3);
                if (FirebaseInstanceId.zzm()) {
                    Log.d("FirebaseInstanceId", "subscribe operation succeeded");
                }
            } catch (IOException e) {
                String valueOf = String.valueOf(e.getMessage());
                Log.e("FirebaseInstanceId", valueOf.length() != 0 ? "Topic sync failed: ".concat(valueOf) : new String("Topic sync failed: "));
                return false;
            }
        }
        return true;
    }
}
