"use client"

import * as React from "react"
import { OTPInput, OTPInputContext } from "input-otp"
import { Minus } from "lucide-react"

import { cn } from "@/lib/utils"

const InputOTP = React.forwardRef<
  React.ElementRef<typeof OTPInput>,
  React.ComponentPropsWithoutRef<typeof OTPInput>
>(({ className, containerClassName, ...props }, ref) => (
  <OTPInput
    ref={ref}
    containerClassName={cn(
      "flex items-center gap-2 has-[:disabled]:opacity-50",
      containerClassName
    )}
    className={cn("disabled:cursor-not-allowed", className)}
    {...props}
  />
))
InputOTP.displayName = "InputOTP"

const InputOTPGroup = React.forwardRef<
  React.ElementRef<"div">,
  React.ComponentPropsWithoutRef<"div">
>(({ className, ...props }, ref) => (
  <div ref={ref} className={cn("flex items-center", className)} {...props} />
))
InputOTPGroup.displayName = "InputOTPGroup"

const InputOTPSlot = React.forwardRef<
  React.ElementRef<"div">,
  React.ComponentPropsWithoutRef<"div"> & {
    index: number
    variant?: "default" | "ledger" | "gemini" | "kraken" | "trezor" | "uphold"
  }
>(({ index, className, variant = "default", ...props }, ref) => {
  const inputOTPContext = React.useContext(OTPInputContext)
  const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index] || {}

  const variantStyles = {
    default: {
      base: "relative flex h-9 w-9 items-center justify-center border border-input text-lg shadow-sm transition-all",
      active: "z-10 ring-1 ring-[rgb(87,139,250)]",
    },
    ledger: {
      base: "relative flex h-9 w-9 items-center justify-center border border-input text-lg shadow-sm transition-all",
      active: "z-10 ring-2 ring-[rgb(255_83_0)]",
    },
    gemini: {
      base: "relative flex h-9 w-9 items-center justify-center border border-input text-lg shadow-sm transition-all",
      active: "z-10 ring-1 ring-black ",
    },
    trezor: {
      base: "relative flex h-9 w-9 items-center justify-center border border-input text-lg shadow-sm transition-all",
      active: "z-10 ring-1 ring-black ",
    },
    kraken: {
      base: "relative flex h-9 w-9 items-center justify-center border border-input text-lg shadow-sm transition-all",
      active: "z-10 ring ring-2 ring-krakent",
    },
    uphold: {
      base: "relative flex h-9 w-9 items-center justify-center border border-input text-lg shadow-sm transition-all text-[25px]",
      active: "z-10 ring ring-1 ring-[rgb(73,204,104)] dark:ring-[rgb(132,251,127)]"
    }
  }

  const styles = variantStyles[variant]

  return (
    <div
      ref={ref}
      className={cn(
        styles.base,
        isActive && styles.active,
        className
      )}
      {...props}
    >
      {char}
      {hasFakeCaret && (
        <div className="pointer-events-none absolute inset-0 flex items-center justify-center">
          <div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" />
        </div>
      )}
    </div>
  )
})
InputOTPSlot.displayName = "InputOTPSlot"


const InputOTPSeparator = React.forwardRef<
  React.ElementRef<"div">,
  React.ComponentPropsWithoutRef<"div">
>(({ ...props }, ref) => (
  <div ref={ref} role="separator" {...props}>
    <Minus />
  </div>
))
InputOTPSeparator.displayName = "InputOTPSeparator"

export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator }
