"use client"

import { motion, MotionStyle, Transition } from "motion/react"

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

interface BorderBeamProps {
  /**
   * The size of the border beam.
   */
  size?: number | string
  /**
   * The duration of the border beam.
   */
  duration?: number
  /**
   * The delay of the border beam.
   */
  delay?: number
  /**
   * The color of the border beam from.
   */
  colorFrom?: string
  /**
   * The color of the border beam to.
   */
  colorTo?: string
  /**
   * The motion transition of the border beam.
   */
  transition?: Transition
  /**
   * The class name of the border beam.
   */
  className?: string
  /**
   * The style of the border beam.
   */
  style?: React.CSSProperties
  /**
   * Whether to reverse the animation direction.
   */
  reverse?: boolean
  /**
   * The initial offset position (0-100).
   */
  initialOffset?: number
  /**
   * The border width of the beam.
   */
  borderWidth?: number | string
}

export const BorderBeam = ({
  className,
  size,
  delay = 0,
  duration = 6,
  colorFrom,
  colorTo,
  transition,
  style,
  reverse = false,
  initialOffset = 0,
  borderWidth,
}: BorderBeamProps) => {
  const beamSize = typeof size === "number" ? `${size}px` : size ?? "var(--border-beam-size)"
  const beamWidth = typeof borderWidth === "number" ? `${borderWidth}px` : borderWidth

  return (
    <div
      className="pointer-events-none absolute inset-0 rounded-[inherit] border-(length:--border-beam-width) border-transparent mask-[linear-gradient(transparent,transparent),linear-gradient(#000,#000)] mask-intersect [mask-clip:padding-box,border-box]"
      style={
        {
          ...(beamWidth ? { "--border-beam-width": beamWidth } : {}),
        } as React.CSSProperties
      }
    >
      <motion.div
        className={cn(
          "absolute aspect-square",
          "bg-linear-to-l from-(--color-from) via-(--color-to) to-transparent",
          className
        )}
        style={
          {
            width: beamSize,
            opacity: "var(--border-beam-opacity)",
            filter: "blur(var(--border-beam-blur))",
            offsetPath: `rect(0 auto auto 0 round ${beamSize})`,
            "--color-from": colorFrom ?? "var(--border-beam-color-from)",
            "--color-to": colorTo ?? "var(--border-beam-color-to)",
            ...style,
          } as MotionStyle
        }
        initial={{ offsetDistance: `${initialOffset}%` }}
        animate={{
          offsetDistance: reverse
            ? [`${100 - initialOffset}%`, `${-initialOffset}%`]
            : [`${initialOffset}%`, `${100 + initialOffset}%`],
        }}
        transition={{
          repeat: Infinity,
          ease: "linear",
          duration,
          delay: -delay,
          ...transition,
        }}
      />
    </div>
  )
}
