import {openDB, deleteDB, wrap, unwrap} from 'idb';
import {closeAllDatabases, databases, DBObject, getDatabase} from './getDB';
import DBSchemas from './schemas';

export const initDatabase = (): Promise<void> => {
  return new Promise(async (resolve, reject) => {
    closeAllDatabases();

    for (const key in DBSchemas) {
      const schema = DBSchemas[key as keyof typeof DBSchemas];

      let dbObj: DBObject = {
        schema: schema,
        name: schema.details.name,
        db: undefined,
      };

      databases[dbObj.name] = dbObj;

      await getDatabase(dbObj.name, true); // init Database
    }
    resolve();
  });
};
