import { cookies } from "next/headers"
import { z } from "zod"

import { SAS_REQUEST_OTP_STATE_STORAGE_COOKIE_NAME } from "./constants"

const otpStateSchema = z.object({
  referenceId: z.string().uuid(),
  databaseUUID: z.string().uuid(),
})

export type OtpState = z.infer<typeof otpStateSchema>

export async function getOTPState() {
  const cookieStore = await cookies()
  const otpState = cookieStore.get(SAS_REQUEST_OTP_STATE_STORAGE_COOKIE_NAME)

  return otpStateSchema.parse(JSON.parse(otpState?.value ?? "{}"))
}
