package com.google.android.gms.measurement.internal;

import android.content.SharedPreferences;
import android.os.Bundle;
import com.google.android.gms.common.internal.Preconditions;
import com.google.android.gms.internal.measurement.zzpn;
import java.util.Arrays;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* loaded from: classes3.dex */
public final class zzhc {
    private final String zza;
    private final Bundle zzb;
    private Bundle zzc;
    private final /* synthetic */ zzha zzd;

    public zzhc(zzha zzhaVar, String str, Bundle bundle) {
        this.zzd = zzhaVar;
        Preconditions.checkNotEmpty(str);
        this.zza = str;
        if (zzhaVar.zze().zza(zzbh.zzdk)) {
            this.zzb = new Bundle();
        } else {
            this.zzb = new Bundle();
        }
    }

    private final String zzb(Bundle bundle) {
        JSONArray jSONArray = new JSONArray();
        for (String str : bundle.keySet()) {
            Object obj = bundle.get(str);
            if (obj != null) {
                try {
                    JSONObject jSONObject = new JSONObject();
                    jSONObject.put("n", str);
                    if (!zzpn.zza() || !this.zzd.zze().zza(zzbh.zzci)) {
                        jSONObject.put("v", String.valueOf(obj));
                        if (obj instanceof String) {
                            jSONObject.put("t", "s");
                        } else if (obj instanceof Long) {
                            jSONObject.put("t", "l");
                        } else if (obj instanceof Double) {
                            jSONObject.put("t", "d");
                        } else {
                            this.zzd.zzj().zzg().zza("Cannot serialize bundle value to SharedPreferences. Type", obj.getClass());
                        }
                    } else if (obj instanceof String) {
                        jSONObject.put("v", String.valueOf(obj));
                        jSONObject.put("t", "s");
                    } else if (obj instanceof Long) {
                        jSONObject.put("v", String.valueOf(obj));
                        jSONObject.put("t", "l");
                    } else if (obj instanceof int[]) {
                        jSONObject.put("v", Arrays.toString((int[]) obj));
                        jSONObject.put("t", "ia");
                    } else if (obj instanceof long[]) {
                        jSONObject.put("v", Arrays.toString((long[]) obj));
                        jSONObject.put("t", "la");
                    } else if (obj instanceof Double) {
                        jSONObject.put("v", String.valueOf(obj));
                        jSONObject.put("t", "d");
                    } else {
                        this.zzd.zzj().zzg().zza("Cannot serialize bundle value to SharedPreferences. Type", obj.getClass());
                    }
                    jSONArray.put(jSONObject);
                } catch (JSONException e10) {
                    this.zzd.zzj().zzg().zza("Cannot serialize bundle value to SharedPreferences", e10);
                }
            }
        }
        return jSONArray.toString();
    }

    /* JADX WARN: Removed duplicated region for block: B:27:0x0089  */
    /* JADX WARN: Removed duplicated region for block: B:62:0x0120 A[Catch: NumberFormatException | JSONException -> 0x0128, NumberFormatException | JSONException -> 0x0128, TRY_LEAVE, TryCatch #1 {NumberFormatException | JSONException -> 0x0128, blocks: (B:13:0x0025, B:31:0x0091, B:31:0x0091, B:34:0x00a2, B:34:0x00a2, B:36:0x00a8, B:36:0x00a8, B:38:0x00b6, B:38:0x00b6, B:40:0x00c8, B:40:0x00c8, B:42:0x00d1, B:42:0x00d1, B:46:0x00d5, B:46:0x00d5, B:48:0x00db, B:48:0x00db, B:50:0x00e9, B:50:0x00e9, B:52:0x00fb, B:52:0x00fb, B:54:0x0104, B:54:0x0104, B:58:0x0108, B:58:0x0108, B:60:0x0114, B:60:0x0114, B:62:0x0120, B:62:0x0120, B:64:0x0052, B:67:0x005c, B:70:0x0066, B:73:0x0070, B:76:0x007a), top: B:12:0x0025, outer: #0 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Bundle zza() {
        JSONObject jSONObject;
        String string;
        String string2;
        int hashCode;
        char c10;
        if (this.zzc == null) {
            String string3 = this.zzd.zzg().getString(this.zza, null);
            if (string3 != null) {
                try {
                    Bundle bundle = new Bundle();
                    JSONArray jSONArray = new JSONArray(string3);
                    for (int i10 = 0; i10 < jSONArray.length(); i10++) {
                        try {
                            jSONObject = jSONArray.getJSONObject(i10);
                            string = jSONObject.getString("n");
                            string2 = jSONObject.getString("t");
                            hashCode = string2.hashCode();
                        } catch (NumberFormatException | JSONException unused) {
                            this.zzd.zzj().zzg().zza("Error reading value from SharedPreferences. Value dropped");
                        }
                        if (hashCode == 100) {
                            if (string2.equals("d")) {
                                c10 = 1;
                                if (c10 != 0) {
                                }
                            }
                            c10 = 65535;
                            if (c10 != 0) {
                            }
                        } else if (hashCode == 108) {
                            if (string2.equals("l")) {
                                c10 = 2;
                                if (c10 != 0) {
                                }
                            }
                            c10 = 65535;
                            if (c10 != 0) {
                            }
                        } else if (hashCode == 115) {
                            if (string2.equals("s")) {
                                c10 = 0;
                                if (c10 != 0) {
                                }
                            }
                            c10 = 65535;
                            if (c10 != 0) {
                            }
                        } else if (hashCode != 3352) {
                            if (hashCode == 3445 && string2.equals("la")) {
                                c10 = 4;
                                if (c10 != 0) {
                                    bundle.putString(string, jSONObject.getString("v"));
                                } else if (c10 == 1) {
                                    bundle.putDouble(string, Double.parseDouble(jSONObject.getString("v")));
                                } else if (c10 == 2) {
                                    bundle.putLong(string, Long.parseLong(jSONObject.getString("v")));
                                } else if (c10 != 3) {
                                    if (c10 != 4) {
                                        this.zzd.zzj().zzg().zza("Unrecognized persisted bundle type. Type", string2);
                                    } else if (zzpn.zza() && this.zzd.zze().zza(zzbh.zzci)) {
                                        JSONArray jSONArray2 = new JSONArray(jSONObject.getString("v"));
                                        int length = jSONArray2.length();
                                        long[] jArr = new long[length];
                                        for (int i11 = 0; i11 < length; i11++) {
                                            jArr[i11] = jSONArray2.optLong(i11);
                                        }
                                        bundle.putLongArray(string, jArr);
                                    }
                                } else if (zzpn.zza() && this.zzd.zze().zza(zzbh.zzci)) {
                                    JSONArray jSONArray3 = new JSONArray(jSONObject.getString("v"));
                                    int length2 = jSONArray3.length();
                                    int[] iArr = new int[length2];
                                    for (int i12 = 0; i12 < length2; i12++) {
                                        iArr[i12] = jSONArray3.optInt(i12);
                                    }
                                    bundle.putIntArray(string, iArr);
                                }
                            }
                            c10 = 65535;
                            if (c10 != 0) {
                            }
                        } else {
                            if (string2.equals("ia")) {
                                c10 = 3;
                                if (c10 != 0) {
                                }
                            }
                            c10 = 65535;
                            if (c10 != 0) {
                            }
                        }
                    }
                    this.zzc = bundle;
                } catch (JSONException unused2) {
                    this.zzd.zzj().zzg().zza("Error loading bundle from SharedPreferences. Values will be lost");
                }
            }
            if (this.zzc == null) {
                this.zzc = this.zzb;
            }
        }
        if (this.zzd.zze().zza(zzbh.zzdk)) {
            return new Bundle((Bundle) Preconditions.checkNotNull(this.zzc));
        }
        return (Bundle) Preconditions.checkNotNull(this.zzc);
    }

    public final void zza(Bundle bundle) {
        if (bundle == null) {
            bundle = new Bundle();
        } else if (this.zzd.zze().zza(zzbh.zzdk)) {
            bundle = new Bundle(bundle);
        }
        SharedPreferences.Editor edit = this.zzd.zzg().edit();
        if (bundle.size() == 0) {
            edit.remove(this.zza);
        } else {
            edit.putString(this.zza, zzb(bundle));
        }
        edit.apply();
        this.zzc = bundle;
    }
}
