import { EncryptionService } from "../service/EncryptionService";
import { ChatMessageDTO } from "../dto/ChatMessageDTO";
import * as log from "loglevel";
import { getOneMessage } from "../ajax/Messages";

export async function isPassphraseValid(
  passphrase: string,
  userName: string,
  es: EncryptionService,
  authToken: string
): Promise<boolean> {
  const messages: ChatMessageDTO[] = await getOneMessage(userName, 0, authToken);
  if (messages.length === 0) return true;
  try {
    es.decrypt(passphrase, messages[0].messageCipher);
  } catch (error) {
    log.debug("here");
    return false;
  }
  return true;
}
