"use client";

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

type ChannelSpecificThemesProps = React.ComponentProps<typeof motion.svg>;

const outlineVariants: Variants = {
  hidden: {
    opacity: 0,
    pathLength: 0,
    scale: 0.94,
    rotate: -7,
  },
  visible: {
    opacity: 1,
    pathLength: 1,
    scale: 1,
    rotate: 0,
    transition: {
      opacity: { duration: 0.2 },
      pathLength: { duration: 0.95, ease: "easeInOut" },
      scale: { type: "spring", stiffness: 220, damping: 18, delay: 0.4 },
      rotate: { type: "spring", stiffness: 220, damping: 18, delay: 0.4 },
    },
  },
  hover: {
    opacity: 1,
    pathLength: 1,
    scale: [1, 1.035, 1],
    rotate: [0, -3, 3, 0],
    transition: {
      duration: 2.2,
      ease: "easeInOut",
      repeat: Infinity,
      repeatType: "loop",
    },
  },
};

const dotVariants = (index: number): Variants => ({
  hidden: {
    opacity: 0,
    scale: 0,
  },
  visible: {
    opacity: 1,
    scale: 1,
    transition: {
      type: "spring",
      stiffness: 360,
      damping: 16,
      delay: 0.4 + index * 0.12,
    },
  },
  hover: {
    opacity: [1, 0.6, 1],
    scale: [1, 0.78, 1.23, 1],
    transition: {
      duration: 1.55,
      delay: index * 0.15,
      ease: "easeInOut",
      repeat: Infinity,
      repeatType: "loop",
    },
  },
});

// The original four dot paths are separated so each can twinkle independently.
// Their positions, strokes, sizes, and original pink color remain unchanged.
const dots = [
  "M58.5 30.333a2.167 2.167 0 1 0 0-4.333 2.167 2.167 0 0 0 0 4.333",
  "M75.833 47.665a2.167 2.167 0 1 0 0-4.333 2.167 2.167 0 0 0 0 4.333",
  "M28.167 56.333a2.167 2.167 0 1 0 0-4.333 2.167 2.167 0 0 0 0 4.333",
  "M36.833 34.665a2.167 2.167 0 1 0 0-4.333 2.167 2.167 0 0 0 0 4.333",
] as const;

/**
 * Channel-specific themes icon
 * - Draws and reveals itself once on mount.
 * - Continuously loops a gentle palette sway and staggered dot twinkles on hover.
 * - Returns smoothly to its resting state on mouse leave.
 * - Respects the user's reduced-motion preference.
 */
export default function ChannelSpecificThemes({
  width = 104,
  height = 104,
  style,
  animate,
  whileHover,
  ...props
}: ChannelSpecificThemesProps) {
  const reduceMotion = 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={props["aria-label"] ?? "Channel-specific themes"}
      initial={reduceMotion ? false : "hidden"}
      animate={animate}
      whileInView={
        animate !== undefined || reduceMotion ? undefined : "visible"
      }
      viewport={{ once: true, amount: 0.55 }}
      whileHover={
        whileHover !== undefined
          ? whileHover
          : reduceMotion
            ? undefined
            : "hover"
      }
      style={{
        display: "inline-block",
        overflow: "visible",
        cursor: "pointer",
        ...style,
      }}
    >
      <motion.path
        d="M52 95.335a43.333 43.333 0 1 1 0-86.667c11.493 0 22.515 4.109 30.641 11.423 8.127 7.314 12.692 17.234 12.692 27.577a21.667 21.667 0 0 1-21.666 21.667h-9.75a7.583 7.583 0 0 0-6.067 12.133l1.3 1.733a7.584 7.584 0 0 1-6.067 12.134z"
        stroke="#ff7d80"
        strokeWidth={8}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={outlineVariants}
        style={{ transformBox: "fill-box", transformOrigin: "center" }}
      />

      {dots.map((d, index) => (
        <motion.path
          key={d}
          d={d}
          fill="#ff7d80"
          stroke="#ff7d80"
          strokeWidth={8}
          strokeLinecap="round"
          strokeLinejoin="round"
          variants={dotVariants(index)}
          style={{ transformBox: "fill-box", transformOrigin: "center" }}
        />
      ))}
    </motion.svg>
  );
}
