import type {
  VerifySignatureHashAlgorithmEnum,
  VerifySignatureResponseDto,
  SignPdfHashHashAlgorithmEnum
} from '~/.api-client-middleware'
import { useMiddlewareApi } from '~/composables/middleware/useMiddlewareApi'

export function useMiddleware() {
  const middlewareApi = useMiddlewareApi()

  async function signPdfHash(document: Blob, certificateId: string, hashAlgorithm?: SignPdfHashHashAlgorithmEnum) {
    try {
      return middlewareApi.signPdfHash(document, certificateId, hashAlgorithm)
    } catch (e: unknown) {
      console.error('Failed signing PDF hash:', e)
      return Promise.reject(e)
    }
  }

  async function verifySignature(
    document: Blob,
    signature: string,
    certificateId?: string,
    hashAlgorithm?: VerifySignatureHashAlgorithmEnum
  ): Promise<VerifySignatureResponseDto> {
    try {
      return await middlewareApi.verifySignature(document, signature, certificateId, hashAlgorithm)
    } catch (e: unknown) {
      console.error('Failed verifying signature:', e)
      return Promise.reject(e)
    }
  }

  return {
    signPdfHash,
    verifySignature
  }
}
