import type { Database } from '../../index.js';
import {
  clearLocalAccessGrantToken,
  clearLocalRegistry,
} from '../../utils/localStorageService.js';

export const logout =
  (db: Database) =>
  /**
   * Clears the login token from storage and disconnects all sync providers. Still leaves the local indexedDB yDocs.
   */
  () => {
    clearLocalAccessGrantToken(db)();
    db.accessGrantToken = '';
    db.useSync = false;
    db.online = false;
    for (const room of db.registry) {
      const dbRoom = db.getRoom(room.collectionKey, room.id);
      dbRoom.disconnect();
    }
    db.emit('onLoggedInChange', false);
  };

export const logoutAndClear =
  (db: Database) =>
  /**
   * Logs out and also clears all local data from indexedDB and localStorage.
   */
  () => {
    db.logout();
    for (const collectionKey of db.collectionKeys) {
      for (const room of db.getRooms(collectionKey)) {
        room.indexedDbProvider?.clearData();
        room.indexedDbProvider?.destroy();
      }
    }
    db.registry = [];
    clearLocalRegistry(db)();
  };
