import { Session } from 'next-auth';
import { ApiKeyEntry } from './db';
import crypto from 'crypto';

export function sha256sum(input: any) {
  const hash = crypto.createHash('sha256');
  hash.update(input);
  return hash.digest('hex');
}

export function isAdmin(session: Session | string | null) {
  let email = typeof session == 'string' ? session : session?.user?.email;
  return email && ["lea@amogus.cloud", "lexi@futacockinside.me"].includes(email); // todo
}

export function aliasesNeedApproval(session: Session | null) {
  return !isAdmin(session); // also todo
}

export function anonymizeApiKey(key: ApiKeyEntry): ApiKeyEntry {
  return {
    ...key,
    token: key.token.substring(0, 6) + "********",
  };
}
