"use client";

import { motion, AnimatePresence } from "framer-motion";

type Props = {
  tabKey: string;
  children: React.ReactNode;
};

/**
 * Fades the content of a tabbed panel when the active tab changes.
 * The tab key drives the animation, so switching tabs triggers a quick
 * opacity-only fade-in of the new content.
 *
 * Motion note: the previous version used `y: 4` / `y: -4` on enter/exit
 * for a small slide. That 4px slide, multiplied across every tab change
 * in the admin, was a steady source of positional movement. Now it's
 * pure opacity, 120ms, ease-out. `reducedMotion="user"` on the global
 * <MotionConfig> will further strip this to instant if the user has
 * prefers-reduced-motion enabled.
 *
 * No motion when the tab key is stable (i.e. on first render of a given tab).
 */
export function TabSwitcher({ tabKey, children }: Props) {
  return (
    <AnimatePresence mode="wait" initial={false}>
      <motion.div
        key={tabKey}
        initial={{ opacity: 0 }}
        animate={{ opacity: 1 }}
        exit={{ opacity: 0 }}
        transition={{ duration: 0.12, ease: "easeOut" }}
      >
        {children}
      </motion.div>
    </AnimatePresence>
  );
}
