package androidx.room;

import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import kotlin.Pair;
import kotlin.collections.builders.SetBuilder;
import kotlinx.coroutines.channels.BufferOverflow;

/* JADX INFO: loaded from: classes.dex */
public final class u {

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final kotlinx.coroutines.x f4160d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final AtomicBoolean f4161e = new AtomicBoolean(true);

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int f4162f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public o f4163g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final kotlinx.coroutines.flow.s f4164h;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final r f4166j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final t f4167k;

    public u(Context context, String str, q qVar) {
        this.f4157a = str;
        this.f4158b = qVar;
        this.f4159c = context.getApplicationContext();
        this.f4160d = qVar.f4122a.getCoroutineScope();
        BufferOverflow bufferOverflow = BufferOverflow.f29297a;
        this.f4164h = new kotlinx.coroutines.flow.s(0);
        this.f4165i = new s(this, qVar.f4125d);
        this.f4166j = new r(this);
        this.f4167k = new t(this);
    }

    public final void a(Intent intent) {
        if (this.f4161e.compareAndSet(true, false)) {
            this.f4159c.bindService(intent, this.f4167k, 1);
            q qVar = this.f4158b;
            qVar.getClass();
            s sVar = this.f4165i;
            nb.g.i(sVar, "observer");
            x0 x0Var = qVar.f4126e;
            x0Var.getClass();
            String[] strArr = sVar.f4142a;
            nb.g.i(strArr, "names");
            SetBuilder setBuilder = new SetBuilder();
            for (String str : strArr) {
                String lowerCase = str.toLowerCase(Locale.ROOT);
                nb.g.h(lowerCase, "toLowerCase(...)");
                Set set = (Set) x0Var.f4202c.get(lowerCase);
                if (set != null) {
                    setBuilder.addAll(set);
                } else {
                    setBuilder.add(str);
                }
            }
            setBuilder.g();
            String[] strArr2 = (String[]) setBuilder.toArray(new String[0]);
            int length = strArr2.length;
            int[] iArr = new int[length];
            for (int i10 = 0; i10 < length; i10++) {
                String str2 = strArr2[i10];
                LinkedHashMap linkedHashMap = x0Var.f4205f;
                String lowerCase2 = str2.toLowerCase(Locale.ROOT);
                nb.g.h(lowerCase2, "toLowerCase(...)");
                Integer num = (Integer) linkedHashMap.get(lowerCase2);
                if (num == null) {
                    throw new IllegalArgumentException("There is no table with name ".concat(str2));
                }
                iArr[i10] = num.intValue();
            }
            Pair pair = new Pair(strArr2, iArr);
            String[] strArr3 = (String[]) pair.getFirst();
            int[] iArr2 = (int[]) pair.getSecond();
            y yVar = new y(sVar, iArr2, strArr3);
            ReentrantLock reentrantLock = qVar.f4128g;
            reentrantLock.lock();
            LinkedHashMap linkedHashMap2 = qVar.f4127f;
            try {
                y yVar2 = linkedHashMap2.containsKey(sVar) ? (y) kotlin.collections.q.Y(sVar, linkedHashMap2) : (y) linkedHashMap2.put(sVar, yVar);
                reentrantLock.unlock();
                if (yVar2 == null) {
                    x xVar = x0Var.f4207h;
                    xVar.getClass();
                    ReentrantLock reentrantLock2 = (ReentrantLock) xVar.f4197c;
                    reentrantLock2.lock();
                    try {
                        for (int i11 : iArr2) {
                            Object obj = xVar.f4198d;
                            long j10 = ((long[]) obj)[i11];
                            ((long[]) obj)[i11] = 1 + j10;
                            if (j10 == 0) {
                                xVar.f4196b = true;
                            }
                        }
                    } finally {
                        reentrantLock2.unlock();
                    }
                }
            } catch (Throwable th) {
                reentrantLock.unlock();
                throw th;
            }
        }
    }

    public final void b() {
        if (this.f4161e.compareAndSet(false, true)) {
            q qVar = this.f4158b;
            qVar.getClass();
            s sVar = this.f4165i;
            nb.g.i(sVar, "observer");
            ReentrantLock reentrantLock = qVar.f4128g;
            reentrantLock.lock();
            try {
                y yVar = (y) qVar.f4127f.remove(sVar);
                if (yVar != null) {
                    x0 x0Var = qVar.f4126e;
                    x0Var.getClass();
                    int[] iArr = yVar.f4212b;
                    nb.g.i(iArr, "tableIds");
                    x xVar = x0Var.f4207h;
                    xVar.getClass();
                    ReentrantLock reentrantLock2 = (ReentrantLock) xVar.f4197c;
                    reentrantLock2.lock();
                    try {
                        boolean z10 = false;
                        for (int i10 : iArr) {
                            Object obj = xVar.f4198d;
                            long j10 = ((long[]) obj)[i10];
                            ((long[]) obj)[i10] = j10 - 1;
                            if (j10 == 1) {
                                xVar.f4196b = true;
                                z10 = true;
                            }
                        }
                        if (z10) {
                            androidx.room.coroutines.m.a(new InvalidationTracker$removeObserver$1(qVar, null));
                        }
                    } finally {
                        reentrantLock2.unlock();
                    }
                }
                try {
                    o oVar = this.f4163g;
                    if (oVar != null) {
                        oVar.J2(this.f4166j, this.f4162f);
                    }
                } catch (RemoteException unused) {
                }
                this.f4159c.unbindService(this.f4167k);
            } finally {
                reentrantLock.unlock();
            }
        }
    }
}
