import {appNonSaveVarActions} from '@configs/appNonSaveVarReducer';
import {initDatabase} from '@helper/storage/bdm/init';
import DBSchemas from '@helper/storage/bdm/schemas';
import BigDataManager from '@helper/storage/BigDataManager';
import {store} from '@redux/store';
import {chatEntity} from '@configs/chat/types';

async function initChatDatabase(chat: chatEntity) {
  const keys = BigDataManager.databases.chatRoomInfos.keys;

  await initDatabase(DBSchemas.chat, chat.roomId);

  await BigDataManager.databases.chatRoomInfos.setEntry({
    [keys.RoomId]: chat.roomId,
    [keys.initSyncId]: chat.initSyncId,
    [keys.syncId]: chat.syncId,
    [keys.unreadMessages]: chat.unreadMessages,
    [keys.users]: chat.users,
    [keys.timestamp]: chat.timestamp,
  });
  addChatEntity(chat);
}

export function addChatEntity(chat: chatEntity) {
  store.dispatch(appNonSaveVarActions.setChatEntity(chat));
}

export default initChatDatabase;
