"use client";

import * as React from "react";
import { motion, useReducedMotion, type Variants } from "framer-motion";

type IconProps = Omit<React.ComponentProps<typeof motion.svg>, "children">;

const routeD =
  "M39 82.335h36.833a15.167 15.167 0 1 0 0-30.334H28.167a15.167 15.167 0 1 1 0-30.333H65";

const route: Variants = {
  hidden: { opacity: 0, pathLength: 0 },
  visible: {
    opacity: 1,
    pathLength: 1,
    transition: {
      opacity: { duration: 0.2 },
      pathLength: { duration: 1.05, ease: "easeInOut" },
    },
  },
  hover: { opacity: 1, pathLength: 1 },
};

const node = (delay: number): Variants => ({
  hidden: { opacity: 0, pathLength: 0, scale: 0.7 },
  visible: {
    opacity: 1,
    pathLength: 1,
    scale: 1,
    transition: {
      opacity: { duration: 0.2, delay },
      pathLength: { duration: 0.35, delay },
      scale: { type: "spring", stiffness: 260, damping: 16 },
    },
  },
  hover: {
    opacity: 1,
    pathLength: 1,
    scale: [1, 1.12, 1],
    transition: {
      duration: 1.8,
      delay: delay === 0.25 ? 0.45 : 0,
      repeat: Infinity,
      ease: "easeInOut",
    },
  },
});

/** Draws the route once; pulses both destinations while hovered. */
export default function AdvancedRouting({
  width = 104,
  height = 104,
  style,
  animate,
  whileHover,
  ...props
}: IconProps) {
  const reducedMotion = useReducedMotion();

  return (
    <motion.svg
      {...props}
      width={width}
      height={height}
      viewBox="0 0 104 104"
      fill="none"
      xmlns="http://www.w3.org/2000/svg"
      role="img"
      aria-label="Advanced routing"
      initial={reducedMotion ? false : "hidden"}
      animate={animate}
      whileInView={
        animate !== undefined || reducedMotion ? undefined : "visible"
      }
      viewport={{ once: true, amount: 0.55 }}
      whileHover={
        whileHover !== undefined
          ? whileHover
          : reducedMotion
            ? undefined
            : "hover"
      }
      style={{ display: "inline-block", overflow: "visible", ...style }}
    >
      <motion.path
        d={routeD}
        stroke="#5db3fa"
        strokeWidth={8.75}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={route}
      />
      <motion.path
        d="M91 21.668c0 7.18-5.82 13-13 13s-13-5.82-13-13 5.82-13 13-13 13 5.82 13 13"
        stroke="#5db3fa"
        strokeWidth={8.75}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={node(0.25)}
        style={{ transformBox: "fill-box", transformOrigin: "center" }}
      />
      <motion.path
        d="M39 82.332c0 7.18-5.82 13-13 13s-13-5.82-13-13 5.82-13 13-13 13 5.82 13 13"
        stroke="#5db3fa"
        strokeWidth={8.75}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={node(1.03)}
        style={{ transformBox: "fill-box", transformOrigin: "center" }}
      />
      {/* A moving light follows the existing route without hiding the original path. */}
      {!reducedMotion && (
        <motion.path
          d={routeD}
          stroke="#b8e1ff"
          strokeWidth={3}
          strokeLinecap="round"
          fill="none"
          strokeDasharray="8 200"
          initial={{ opacity: 0, strokeDashoffset: 0 }}
          variants={{
            visible: { opacity: 0 },
            hover: {
              opacity: 0.95,
              strokeDashoffset: [0, -208],
              transition: {
                opacity: { duration: 0.16 },
                strokeDashoffset: {
                  duration: 1.8,
                  repeat: Infinity,
                  ease: "linear",
                },
              },
            },
          }}
        />
      )}
    </motion.svg>
  );
}
