import { openDB } from "idb";
import { DB_NAME, DB_VERSION } from "./constant";
// const dummyDB = {
//   objectStoreNames: Object.assign([], {
//     contains: () => false,
//     item: () => null,
//   }),
//   getAllFromIndex: async () => [],
//   getAll: async () => [],
//   getAllKeys: async () => [],
//   get: async () => undefined,
//   put: async () => { },
//   delete: async () => { },
//   clear: async () => { },
//   transaction: () => ({
//     objectStore: () => ({
//       put: async () => { },
//       delete: async () => { },
//     }),
//     store: {
//       delete: async () => { },
//     },
//     done: Promise.resolve(),
//   }),
// } as unknown as IDBPDatabase<ZignalChatDB>;
let dbInstance = null;
let dbPromise = null;
export function initCacheDB() {
    // if (configStore.get('isProduction')) {
    //   dbInstance = dummyDB;
    //   return Promise.resolve(dummyDB);
    // }
    if (dbInstance)
        return Promise.resolve(dbInstance);
    if (dbPromise)
        return dbPromise;
    dbPromise = openDB(DB_NAME, DB_VERSION, {
        upgrade(db, oldVersion) {
            if (oldVersion < 3) {
                // Remove all existing object stores to reset index db
                const storeNames = Array.from(db.objectStoreNames);
                for (const storeName of storeNames) {
                    db.deleteObjectStore(storeName);
                }
            }
            if (!db.objectStoreNames.contains('chats')) {
                const chatStore = db.createObjectStore('chats', { keyPath: 'id' });
                chatStore.createIndex('by-business-shop', ['business_id', 'shop_id']);
                chatStore.createIndex('by-updated', 'updated_at');
            }
            if (!db.objectStoreNames.contains('messages')) {
                const msgStore = db.createObjectStore('messages', { keyPath: 'id' });
                msgStore.createIndex('by-chat', 'chat_id');
                msgStore.createIndex('by-chat-time', ['chat_id', 'message_at']);
            }
            if (!db.objectStoreNames.contains('users')) {
                const userStore = db.createObjectStore('users', { keyPath: 'id' });
                userStore.createIndex('by-business-shop', ['business_id', 'shop_id']);
                userStore.createIndex('by-fetched', 'fetched_at');
            }
            if (!db.objectStoreNames.contains('metadata')) {
                db.createObjectStore('metadata', { keyPath: 'id' });
            }
            if (!db.objectStoreNames.contains('media')) {
                const mediaStore = db.createObjectStore('media', { keyPath: 'id' });
                mediaStore.createIndex('by-business-shop', ['business_id', 'shop_id']);
                mediaStore.createIndex('by-accessed', 'accessed_at');
                mediaStore.createIndex('by-size', 'size');
            }
            if (!db.objectStoreNames.contains('sync_state')) {
                db.createObjectStore('sync_state');
            }
        },
    }).then(db => {
        dbInstance = db;
        return db;
    });
    return dbPromise;
}
export function getCacheDB() {
    return dbInstance;
}
