import { sha256 } from 'crypto-hash'
import { toast } from 'react-toastify'

/**
 * Computes the SHA-256 hash of an ArrayBuffer.
 * @param arrayBuffer The ArrayBuffer to hash.
 * @returns A Promise resolving to the SHA-256 hash as a hexadecimal string, or null if hashing fails.
 */
export const getHash = async (input: ArrayBuffer | string) => {
  // Compute the SHA-256 hash of the array buffer
  const hash = await sha256(input).catch((err) => {
    // Handle error if hashing fails
    console.log(`error occurred in hashing arrayBuffer :>> `, err)
    toast.error(err.message || `error occurred in hashing`)
    return null // Return null if hashing fails
  })

  return hash // Return the SHA-256 hash
}
