'use client'

import {
  LazyMotion,
  MotionConfig,
  domAnimation,
  m,
  useReducedMotion,
  useScroll,
  useTransform,
  type HTMLMotionProps
} from 'framer-motion'
import { useRef, type ReactNode } from 'react'

const EASE_OUT = [0.21, 0.47, 0.32, 0.98] as const
const VIEWPORT = { once: true, margin: '-80px' } as const

// Loads only the DOM animation features (no layout animations) and respects
// the OS "reduce motion" setting for every animation inside.
export const MotionProvider = ({ children }: { children: ReactNode }) => (
  <LazyMotion features={domAnimation} strict>
    <MotionConfig reducedMotion='user'>{children}</MotionConfig>
  </LazyMotion>
)

type RevealProps = HTMLMotionProps<'div'> & { delay?: number; y?: number }

// Fades content in when it scrolls into view. `data-reveal` lets the <noscript>
// fallback on the page show it without JavaScript.
export const Reveal = ({ delay = 0, y = 24, ...props }: RevealProps) => (
  <m.div
    data-reveal
    initial={{ opacity: 0, y }}
    whileInView={{ opacity: 1, y: 0 }}
    viewport={VIEWPORT}
    transition={{ duration: 0.6, ease: EASE_OUT, delay }}
    {...props}
  />
)

// Reveals its <StaggerItem> children one after another
export const Stagger = ({
  stagger = 0.08,
  ...props
}: HTMLMotionProps<'div'> & { stagger?: number }) => (
  <m.div
    initial='hidden'
    whileInView='visible'
    viewport={VIEWPORT}
    variants={{ visible: { transition: { staggerChildren: stagger } } }}
    {...props}
  />
)

export const StaggerItem = (props: HTMLMotionProps<'div'>) => (
  <m.div
    data-reveal
    variants={{
      hidden: { opacity: 0, y: 24 },
      visible: {
        opacity: 1,
        y: 0,
        transition: { duration: 0.55, ease: EASE_OUT }
      }
    }}
    {...props}
  />
)

// Connector line that draws itself when it scrolls into view
export const DrawLine = ({
  className,
  vertical = false
}: {
  className?: string
  vertical?: boolean
}) => (
  <m.div
    aria-hidden
    className={className}
    style={{ originX: 0, originY: 0 }}
    initial={vertical ? { scaleY: 0 } : { scaleX: 0 }}
    whileInView={vertical ? { scaleY: 1 } : { scaleX: 1 }}
    viewport={{ once: true, margin: '-120px' }}
    transition={{ duration: 1.4, ease: EASE_OUT }}
  />
)

// Moves its children vertically while the element scrolls through the viewport
export const Parallax = ({
  children,
  offset = 60,
  className
}: {
  children: ReactNode
  offset?: number
  className?: string
}) => {
  const ref = useRef<HTMLDivElement>(null)
  const reduceMotion = useReducedMotion()
  const { scrollYProgress } = useScroll({
    target: ref,
    offset: ['start end', 'end start']
  })
  // Starts at 0 so the server-rendered transform matches the first client render
  const y = useTransform(
    scrollYProgress,
    [0, 1],
    reduceMotion ? [0, 0] : [0, -offset * 2]
  )

  return (
    <m.div ref={ref} style={{ y }} className={className}>
      {children}
    </m.div>
  )
}
