"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 bubble: Variants = {
  hidden: { opacity: 0, pathLength: 0 },
  visible: {
    opacity: 1,
    pathLength: 1,
    transition: {
      opacity: { duration: 0.2 },
      pathLength: { duration: 0.85, ease: "easeInOut" },
    },
  },
  hover: {
    opacity: 1,
    pathLength: 1,
    scale: [1, 1.025, 1],
    transition: { duration: 2, repeat: Infinity, ease: "easeInOut" },
  },
};

const line = (delay: number): Variants => ({
  hidden: { opacity: 0, pathLength: 0, x: -5 },
  visible: {
    opacity: 1,
    pathLength: 1,
    x: 0,
    transition: {
      opacity: { duration: 0.25, delay },
      pathLength: { duration: 0.45, delay, ease: "easeOut" },
      x: { duration: 0.4, ease: "easeOut" },
    },
  },
  hover: {
    opacity: [1, 0.45, 1],
    pathLength: 1,
    x: [0, 3, 0],
    transition: {
      duration: 2.1,
      delay: delay - 0.95,
      repeat: Infinity,
      ease: "easeInOut",
    },
  },
});

/** Draws once on mount; the message lines animate in a loop while hovered. */
export default function SessionWiseBrowsingLog({
  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="Session-wise browsing log"
      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="M95.333 73.667a8.667 8.667 0 0 1-8.666 8.666h-57.08a8.67 8.67 0 0 0-6.126 2.54l-9.542 9.542a3.077 3.077 0 0 1-5.253-2.176V21.667A8.667 8.667 0 0 1 17.334 13h69.334a8.667 8.667 0 0 1 8.666 8.667z"
        stroke="#ff9758"
        strokeWidth={7.75}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={bubble}
        style={{ transformBox: "fill-box", transformOrigin: "center" }}
      />
      <motion.path
        d="M30.334 30.332H65"
        stroke="#ff9758"
        strokeWidth={7.75}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={line(0.95)}
      />
      <motion.path
        d="M30.334 47.668h43.333"
        stroke="#ff9758"
        strokeWidth={7.75}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={line(1.12)}
      />
      <motion.path
        d="M30.334 65h26"
        stroke="#ff9758"
        strokeWidth={7.75}
        strokeLinecap="round"
        strokeLinejoin="round"
        variants={line(1.29)}
      />
    </motion.svg>
  );
}
