package o;

import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import androidx.work.impl.WorkDatabase_Impl;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class II {
    public static final String[] m = {"UPDATE", "DELETE", "INSERT"};
    public final WorkDatabase_Impl a;
    public final HashMap b;
    public final LinkedHashMap c;
    public final String[] d;
    public final AtomicBoolean e = new AtomicBoolean(false);
    public volatile boolean f;
    public volatile LC g;
    public final C0949cs h;
    public final U30 i;
    public final Object j;
    public final Object k;
    public final RunnableC2017o3 l;

    public II(WorkDatabase_Impl workDatabase_Impl, HashMap map, HashMap map2, String... strArr) {
        this.a = workDatabase_Impl;
        this.b = map;
        this.h = new C0949cs(strArr.length);
        Collections.newSetFromMap(new IdentityHashMap());
        this.i = new U30();
        this.j = new Object();
        this.k = new Object();
        this.c = new LinkedHashMap();
        int length = strArr.length;
        String[] strArr2 = new String[length];
        for (int i = 0; i < length; i++) {
            String str = strArr[i];
            Locale locale = Locale.US;
            String lowerCase = str.toLowerCase(locale);
            this.c.put(lowerCase, Integer.valueOf(i));
            String str2 = (String) this.b.get(strArr[i]);
            String lowerCase2 = str2 != null ? str2.toLowerCase(locale) : null;
            if (lowerCase2 != null) {
                lowerCase = lowerCase2;
            }
            strArr2[i] = lowerCase;
        }
        this.d = strArr2;
        for (Map.Entry entry : this.b.entrySet()) {
            String str3 = (String) entry.getValue();
            Locale locale2 = Locale.US;
            String lowerCase3 = str3.toLowerCase(locale2);
            if (this.c.containsKey(lowerCase3)) {
                String lowerCase4 = ((String) entry.getKey()).toLowerCase(locale2);
                LinkedHashMap linkedHashMap = this.c;
                linkedHashMap.put(lowerCase4, GN.Y(lowerCase3, linkedHashMap));
            }
        }
        this.l = new RunnableC2017o3(11, this);
    }

    public final boolean a() {
        FC fc = this.a.a;
        if (!F3.c(fc != null ? Boolean.valueOf(fc.l.isOpen()) : null, Boolean.TRUE)) {
            return false;
        }
        if (!this.f) {
            Hb0 hb0 = this.a.c;
            if (hb0 == null) {
                hb0 = null;
            }
            hb0.L();
        }
        return this.f;
    }

    public final void b(FC fc, int i) throws SQLException {
        fc.g("INSERT OR IGNORE INTO room_table_modification_log VALUES(" + i + ", 0)");
        String str = this.d[i];
        for (int i2 = 0; i2 < 3; i2++) {
            String str2 = m[i2];
            StringBuilder sb = new StringBuilder("CREATE TEMP TRIGGER IF NOT EXISTS ");
            sb.append("`room_table_modification_trigger_" + str + '_' + str2 + '`');
            sb.append(" AFTER ");
            sb.append(str2);
            sb.append(" ON `");
            sb.append(str);
            sb.append("` BEGIN UPDATE room_table_modification_log SET invalidated = 1 WHERE table_id = ");
            sb.append(i);
            sb.append(" AND invalidated = 0; END");
            fc.g(sb.toString());
        }
    }

    public final void c(FC fc) {
        if (fc.l()) {
            return;
        }
        try {
            ReentrantReadWriteLock.ReadLock lock = this.a.h.readLock();
            lock.lock();
            try {
                synchronized (this.j) {
                    int[] iArrC = this.h.c();
                    if (iArrC != null) {
                        if (fc.m()) {
                            fc.c();
                        } else {
                            fc.b();
                        }
                        try {
                            int length = iArrC.length;
                            int i = 0;
                            int i2 = 0;
                            while (i < length) {
                                int i3 = iArrC[i];
                                int i4 = i2 + 1;
                                if (i3 == 1) {
                                    b(fc, i2);
                                } else if (i3 == 2) {
                                    String str = this.d[i2];
                                    String[] strArr = m;
                                    for (int i5 = 0; i5 < 3; i5++) {
                                        String str2 = strArr[i5];
                                        StringBuilder sb = new StringBuilder("DROP TRIGGER IF EXISTS ");
                                        sb.append("`room_table_modification_trigger_" + str + '_' + str2 + '`');
                                        fc.g(sb.toString());
                                    }
                                }
                                i++;
                                i2 = i4;
                            }
                            fc.w();
                            fc.f();
                        } catch (Throwable th) {
                            fc.f();
                            throw th;
                        }
                    }
                }
            } finally {
                lock.unlock();
            }
        } catch (SQLiteException | IllegalStateException unused) {
        }
    }
}
