package defpackage;

import android.database.sqlite.SQLiteException;
import android.util.Log;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.upstream.CmcdData;
import androidx.room.RoomDatabase;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes.dex */
@Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0006\b\u0016\u0018\u00002\u00020\u0001:\u0005\u0002\u0003\u0004\u0005\u0006¨\u0006\u0007"}, d2 = {"Las4;", "", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "b", "c", "d", "e", "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public class as4 {
    public static final String[] p = {"UPDATE", "DELETE", "INSERT"};
    public final RoomDatabase a;
    public final HashMap b;
    public final HashMap c;
    public final String[] e;
    public volatile boolean g;
    public volatile pe9 h;
    public final b i;
    public final zr4 j;
    public yb6 l;
    public final bs4 o;
    public final AtomicBoolean f = new AtomicBoolean(false);
    public final z98 k = new z98();
    public final Object m = new Object();
    public final Object n = new Object();
    public final LinkedHashMap d = new LinkedHashMap();

    @Metadata(d1 = {"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\n\u0002\u0010\u0011\n\u0002\b\u0004\b\u0086\u0003\u0018\u00002\u00020\u0001R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0003\u0010\u0004R\u0014\u0010\u0005\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0005\u0010\u0004R\u0014\u0010\u0006\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0006\u0010\u0004R\u001a\u0010\b\u001a\b\u0012\u0004\u0012\u00020\u00020\u00078\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b\b\u0010\tR\u0014\u0010\n\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\n\u0010\u0004¨\u0006\u000b"}, d2 = {"Las4$a;", "", "", "CREATE_TRACKING_TABLE_SQL", "Ljava/lang/String;", "INVALIDATED_COLUMN_NAME", "TABLE_ID_COLUMN_NAME", "", "TRIGGERS", "[Ljava/lang/String;", "UPDATE_TABLE_NAME", "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a {
        public static String a(String str, String str2) {
            cr4.g(str, "tableName");
            cr4.g(str2, "triggerType");
            return "`room_table_modification_trigger_" + str + '_' + str2 + '`';
        }
    }

    @Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\b\u0000\u0018\u00002\u00020\u0001:\u0001\u0002¨\u0006\u0003"}, d2 = {"Las4$b;", "", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @vx8
    public static final class b {
        public final long[] a;
        public final boolean[] b;
        public final int[] c;
        public boolean d;

        @Metadata(d1 = {"\u0000\u0010\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\b\n\u0002\b\u0005\b\u0080\u0003\u0018\u00002\u00020\u0001R\u0014\u0010\u0003\u001a\u00020\u00028\u0006X\u0086T¢\u0006\u0006\n\u0004\b\u0003\u0010\u0004R\u0014\u0010\u0005\u001a\u00020\u00028\u0006X\u0086T¢\u0006\u0006\n\u0004\b\u0005\u0010\u0004R\u0014\u0010\u0006\u001a\u00020\u00028\u0006X\u0086T¢\u0006\u0006\n\u0004\b\u0006\u0010\u0004¨\u0006\u0007"}, d2 = {"Las4$b$a;", "", "", "ADD", "I", "NO_OP", "REMOVE", "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
        public static final class a {
        }

        public b(int i) {
            this.a = new long[i];
            this.b = new boolean[i];
            this.c = new int[i];
        }

        public final int[] a() {
            synchronized (this) {
                try {
                    if (!this.d) {
                        return null;
                    }
                    long[] jArr = this.a;
                    int length = jArr.length;
                    int i = 0;
                    int i2 = 0;
                    while (i < length) {
                        int i3 = i2 + 1;
                        int i4 = 1;
                        boolean z = jArr[i] > 0;
                        boolean[] zArr = this.b;
                        if (z != zArr[i2]) {
                            int[] iArr = this.c;
                            if (!z) {
                                i4 = 2;
                            }
                            iArr[i2] = i4;
                        } else {
                            this.c[i2] = 0;
                        }
                        zArr[i2] = z;
                        i++;
                        i2 = i3;
                    }
                    this.d = false;
                    return (int[]) this.c.clone();
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\b&\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Las4$c;", "", "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @vx8
    public static abstract class c {
        public final String[] a;

        public c(String[] strArr) {
            cr4.g(strArr, "tables");
            this.a = strArr;
        }

        public abstract void a(Set set);
    }

    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\b\u0000\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Las4$d;", "", "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    @vx8
    public static final class d {
        public final c a;
        public final int[] b;
        public final String[] c;
        public final Set d;

        public d(c cVar, int[] iArr, String[] strArr) {
            this.a = cVar;
            this.b = iArr;
            this.c = strArr;
            this.d = strArr.length == 0 ? pk2.a : km8.h(strArr[0]);
            if (iArr.length != strArr.length) {
                throw new IllegalStateException("Check failed.");
            }
        }

        public final void a(Set set) {
            cr4.g(set, "invalidatedTablesIds");
            int[] iArr = this.b;
            int length = iArr.length;
            Set setA = pk2.a;
            if (length != 0) {
                int i = 0;
                if (length != 1) {
                    pl8 pl8Var = new pl8();
                    int length2 = iArr.length;
                    int i2 = 0;
                    while (i < length2) {
                        int i3 = i2 + 1;
                        if (set.contains(Integer.valueOf(iArr[i]))) {
                            pl8Var.add(this.c[i2]);
                        }
                        i++;
                        i2 = i3;
                    }
                    setA = km8.a(pl8Var);
                } else if (set.contains(Integer.valueOf(iArr[0]))) {
                    setA = this.d;
                }
            }
            if (setA.isEmpty()) {
                return;
            }
            this.a.a(setA);
        }

        public final void b(String[] strArr) {
            String[] strArr2 = this.c;
            int length = strArr2.length;
            Set setA = pk2.a;
            if (length != 0) {
                if (length != 1) {
                    pl8 pl8Var = new pl8();
                    for (String str : strArr) {
                        for (String str2 : strArr2) {
                            if (n89.u(str2, str)) {
                                pl8Var.add(str2);
                            }
                        }
                    }
                    setA = km8.a(pl8Var);
                } else {
                    int length2 = strArr.length;
                    int i = 0;
                    while (true) {
                        if (i >= length2) {
                            break;
                        }
                        if (n89.u(strArr[i], strArr2[0])) {
                            setA = this.d;
                            break;
                        }
                        i++;
                    }
                }
            }
            if (setA.isEmpty()) {
                return;
            }
            this.a.a(setA);
        }
    }

    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\b\u0000\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Las4$e;", "Las4$c;", "room-runtime_release"}, k = 1, mv = {1, 8, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class e extends c {
        public final as4 b;
        public final WeakReference c;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public e(as4 as4Var, s58 s58Var) {
            super(s58Var.a);
            cr4.g(s58Var, "delegate");
            this.b = as4Var;
            this.c = new WeakReference(s58Var);
        }

        @Override // as4.c
        public final void a(Set set) {
            cr4.g(set, "tables");
            c cVar = (c) this.c.get();
            if (cVar == null) {
                this.b.c(this);
            } else {
                cVar.a(set);
            }
        }
    }

    public as4(RoomDatabase roomDatabase, HashMap map, HashMap map2, String... strArr) {
        String lowerCase;
        this.a = roomDatabase;
        this.b = map;
        this.c = map2;
        this.i = new b(strArr.length);
        this.j = new zr4(roomDatabase);
        int length = strArr.length;
        String[] strArr2 = new String[length];
        for (int i = 0; i < length; i++) {
            String str = strArr[i];
            Locale locale = Locale.US;
            cr4.f(locale, "US");
            String lowerCase2 = str.toLowerCase(locale);
            cr4.f(lowerCase2, "this as java.lang.String).toLowerCase(locale)");
            this.d.put(lowerCase2, Integer.valueOf(i));
            String str2 = (String) this.b.get(strArr[i]);
            if (str2 != null) {
                lowerCase = str2.toLowerCase(locale);
                cr4.f(lowerCase, "this as java.lang.String).toLowerCase(locale)");
            } else {
                lowerCase = null;
            }
            if (lowerCase != null) {
                lowerCase2 = lowerCase;
            }
            strArr2[i] = lowerCase2;
        }
        this.e = strArr2;
        for (Map.Entry entry : this.b.entrySet()) {
            String str3 = (String) entry.getValue();
            Locale locale2 = Locale.US;
            cr4.f(locale2, "US");
            String lowerCase3 = str3.toLowerCase(locale2);
            cr4.f(lowerCase3, "this as java.lang.String).toLowerCase(locale)");
            if (this.d.containsKey(lowerCase3)) {
                String lowerCase4 = ((String) entry.getKey()).toLowerCase(locale2);
                cr4.f(lowerCase4, "this as java.lang.String).toLowerCase(locale)");
                LinkedHashMap linkedHashMap = this.d;
                linkedHashMap.put(lowerCase4, wo5.e(linkedHashMap, lowerCase3));
            }
        }
        this.o = new bs4(this);
    }

    public final void a(c cVar) {
        d dVar;
        boolean z;
        String[] strArrD = d(cVar.a);
        ArrayList arrayList = new ArrayList(strArrD.length);
        for (String str : strArrD) {
            LinkedHashMap linkedHashMap = this.d;
            Locale locale = Locale.US;
            cr4.f(locale, "US");
            String lowerCase = str.toLowerCase(locale);
            cr4.f(lowerCase, "this as java.lang.String).toLowerCase(locale)");
            Integer num = (Integer) linkedHashMap.get(lowerCase);
            if (num == null) {
                throw new IllegalArgumentException("There is no table with name ".concat(str));
            }
            arrayList.add(num);
        }
        int[] iArrBn = y41.bn(arrayList);
        d dVar2 = new d(cVar, iArrBn, strArrD);
        synchronized (this.k) {
            dVar = (d) this.k.b(cVar, dVar2);
        }
        if (dVar == null) {
            b bVar = this.i;
            int[] iArrCopyOf = Arrays.copyOf(iArrBn, iArrBn.length);
            bVar.getClass();
            cr4.g(iArrCopyOf, "tableIds");
            synchronized (bVar) {
                z = false;
                for (int i : iArrCopyOf) {
                    long[] jArr = bVar.a;
                    long j = jArr[i];
                    jArr[i] = 1 + j;
                    if (j == 0) {
                        z = true;
                        bVar.d = true;
                    }
                }
            }
            if (z) {
                RoomDatabase roomDatabase = this.a;
                if (roomDatabase.isOpenInternal()) {
                    f(roomDatabase.getOpenHelper().getWritableDatabase());
                }
            }
        }
    }

    public final boolean b() {
        if (!this.a.isOpenInternal()) {
            return false;
        }
        if (!this.g) {
            this.a.getOpenHelper().getWritableDatabase();
        }
        if (this.g) {
            return true;
        }
        Log.e("ROOM", "database is not initialized even though it is open");
        return false;
    }

    public final void c(c cVar) {
        d dVar;
        boolean z;
        synchronized (this.k) {
            dVar = (d) this.k.c(cVar);
        }
        if (dVar != null) {
            b bVar = this.i;
            int[] iArr = dVar.b;
            int[] iArrCopyOf = Arrays.copyOf(iArr, iArr.length);
            bVar.getClass();
            cr4.g(iArrCopyOf, "tableIds");
            synchronized (bVar) {
                z = false;
                for (int i : iArrCopyOf) {
                    long[] jArr = bVar.a;
                    long j = jArr[i];
                    jArr[i] = j - 1;
                    if (j == 1) {
                        z = true;
                        bVar.d = true;
                    }
                }
            }
            if (z) {
                RoomDatabase roomDatabase = this.a;
                if (roomDatabase.isOpenInternal()) {
                    f(roomDatabase.getOpenHelper().getWritableDatabase());
                }
            }
        }
    }

    public final String[] d(String[] strArr) {
        pl8 pl8Var = new pl8();
        for (String str : strArr) {
            Locale locale = Locale.US;
            cr4.f(locale, "US");
            String lowerCase = str.toLowerCase(locale);
            cr4.f(lowerCase, "this as java.lang.String).toLowerCase(locale)");
            HashMap map = this.c;
            if (map.containsKey(lowerCase)) {
                String lowerCase2 = str.toLowerCase(locale);
                cr4.f(lowerCase2, "this as java.lang.String).toLowerCase(locale)");
                Object obj = map.get(lowerCase2);
                cr4.d(obj);
                pl8Var.addAll((Collection) obj);
            } else {
                pl8Var.add(str);
            }
        }
        return (String[]) km8.a(pl8Var).toArray(new String[0]);
    }

    public final void e(je9 je9Var, int i) {
        je9Var.execSQL("INSERT OR IGNORE INTO room_table_modification_log VALUES(" + i + ", 0)");
        String str = this.e[i];
        for (int i2 = 0; i2 < 3; i2++) {
            String str2 = p[i2];
            String str3 = "CREATE TEMP TRIGGER IF NOT EXISTS " + a.a(str, str2) + " AFTER " + str2 + " ON `" + str + "` BEGIN UPDATE room_table_modification_log SET invalidated = 1 WHERE table_id = " + i + " AND invalidated = 0; END";
            cr4.f(str3, "StringBuilder().apply(builderAction).toString()");
            je9Var.execSQL(str3);
        }
    }

    public final void f(je9 je9Var) {
        cr4.g(je9Var, "database");
        if (je9Var.inTransaction()) {
            return;
        }
        try {
            Lock closeLock$room_runtime_release = this.a.getCloseLock$room_runtime_release();
            closeLock$room_runtime_release.lock();
            try {
                synchronized (this.m) {
                    int[] iArrA = this.i.a();
                    if (iArrA != null) {
                        if (je9Var.isWriteAheadLoggingEnabled()) {
                            je9Var.beginTransactionNonExclusive();
                        } else {
                            je9Var.beginTransaction();
                        }
                        try {
                            int length = iArrA.length;
                            int i = 0;
                            int i2 = 0;
                            while (i < length) {
                                int i3 = iArrA[i];
                                int i4 = i2 + 1;
                                if (i3 == 1) {
                                    e(je9Var, i2);
                                } else if (i3 == 2) {
                                    String str = this.e[i2];
                                    String[] strArr = p;
                                    for (int i5 = 0; i5 < 3; i5++) {
                                        String str2 = "DROP TRIGGER IF EXISTS " + a.a(str, strArr[i5]);
                                        cr4.f(str2, "StringBuilder().apply(builderAction).toString()");
                                        je9Var.execSQL(str2);
                                    }
                                }
                                i++;
                                i2 = i4;
                            }
                            je9Var.setTransactionSuccessful();
                            je9Var.endTransaction();
                        } catch (Throwable th) {
                            je9Var.endTransaction();
                            throw th;
                        }
                    }
                }
            } finally {
                closeLock$room_runtime_release.unlock();
            }
        } catch (SQLiteException e2) {
            Log.e("ROOM", "Cannot run invalidation tracker. Is the db closed?", e2);
        } catch (IllegalStateException e3) {
            Log.e("ROOM", "Cannot run invalidation tracker. Is the db closed?", e3);
        }
    }
}
