package g3;

import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.SetOptions;
import com.google.firebase.firestore.Transaction;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import kotlin.jvm.internal.F;
import x4.AbstractC3229F;

/* JADX INFO: renamed from: g3.g, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final /* synthetic */ class C2065g implements Transaction.Function {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final /* synthetic */ int f16488a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final /* synthetic */ DocumentReference f16489b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final /* synthetic */ String f16490c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final /* synthetic */ String f16491d;

    public /* synthetic */ C2065g(DocumentReference documentReference, String str, String str2, int i6) {
        this.f16488a = i6;
        this.f16489b = documentReference;
        this.f16490c = str;
        this.f16491d = str2;
    }

    @Override // com.google.firebase.firestore.Transaction.Function
    public final Object apply(Transaction transaction) throws FirebaseFirestoreException {
        String string;
        String string2;
        switch (this.f16488a) {
            case 0:
                DocumentReference documentReference = this.f16489b;
                DocumentSnapshot documentSnapshot = transaction.get(documentReference);
                kotlin.jvm.internal.o.g(documentSnapshot, "get(...)");
                Map<String, Object> data = documentSnapshot.getData();
                LinkedHashMap linkedHashMapI = data != null ? AbstractC3229F.I(data) : null;
                if (!F.e(linkedHashMapI)) {
                    linkedHashMapI = null;
                }
                if (linkedHashMapI == null) {
                    linkedHashMapI = new LinkedHashMap();
                }
                String str = this.f16490c;
                Object obj = linkedHashMapI.get(str);
                Map map = obj instanceof Map ? (Map) obj : null;
                LinkedHashMap linkedHashMapI2 = map != null ? AbstractC3229F.I(map) : new LinkedHashMap();
                ArrayList arrayList = new ArrayList();
                Object obj2 = linkedHashMapI2.get("watchlist");
                if (obj2 instanceof List) {
                    Iterator it = ((Iterable) obj2).iterator();
                    while (it.hasNext()) {
                        Object next = it.next();
                        String string3 = (next == null || (string = next.toString()) == null) ? null : S4.p.o1(string).toString();
                        if (string3 != null && !S4.p.Q0(string3) && !arrayList.contains(string3)) {
                            arrayList.add(string3);
                        }
                    }
                }
                String str2 = this.f16491d;
                if (!arrayList.contains(str2)) {
                    arrayList.add(0, str2);
                }
                linkedHashMapI2.put("watchlist", arrayList);
                linkedHashMapI.put(str, linkedHashMapI2);
                return transaction.set(documentReference, linkedHashMapI, SetOptions.merge());
            default:
                DocumentReference documentReference2 = this.f16489b;
                DocumentSnapshot documentSnapshot2 = transaction.get(documentReference2);
                kotlin.jvm.internal.o.g(documentSnapshot2, "get(...)");
                Map<String, Object> data2 = documentSnapshot2.getData();
                LinkedHashMap linkedHashMapI3 = data2 != null ? AbstractC3229F.I(data2) : null;
                if (!F.e(linkedHashMapI3)) {
                    linkedHashMapI3 = null;
                }
                if (linkedHashMapI3 == null) {
                    linkedHashMapI3 = new LinkedHashMap();
                }
                String str3 = this.f16490c;
                Object obj3 = linkedHashMapI3.get(str3);
                Map map2 = obj3 instanceof Map ? (Map) obj3 : null;
                LinkedHashMap linkedHashMapI4 = map2 != null ? AbstractC3229F.I(map2) : new LinkedHashMap();
                ArrayList arrayList2 = new ArrayList();
                Object obj4 = linkedHashMapI4.get("watchlist");
                if (obj4 instanceof List) {
                    Iterator it2 = ((Iterable) obj4).iterator();
                    while (it2.hasNext()) {
                        Object next2 = it2.next();
                        String string4 = (next2 == null || (string2 = next2.toString()) == null) ? null : S4.p.o1(string2).toString();
                        if (string4 != null && !S4.p.Q0(string4) && !string4.equals(this.f16491d) && !arrayList2.contains(string4)) {
                            arrayList2.add(string4);
                        }
                    }
                }
                linkedHashMapI4.put("watchlist", arrayList2);
                linkedHashMapI3.put(str3, linkedHashMapI4);
                return transaction.set(documentReference2, linkedHashMapI3, SetOptions.merge());
        }
    }
}
