'use client';
import { useMemo, useState } from 'react';
import Link from 'next/link';
import { HomeModernIcon, Bars3Icon, XMarkIcon } from '@heroicons/react/24/outline';
import { AnimatePresence, m, LazyMotion, domAnimation } from "framer-motion";
import { usePathname } from 'next/navigation';
import ThemeSwitcher from './theme-switcher';

type NavBarClientProps =  {
  LogIn: React.JSX.Element,
  navigation: {
    name: string;
    href: string;
    current: boolean;
  }[]
}

export default function NavBarClient({LogIn, navigation}: NavBarClientProps): React.JSX.Element {
  const [open, setOpen] = useState(false);
  const pathname = usePathname();
  
  const activeNavigation = useMemo((): typeof navigation => {
    const nav = structuredClone(navigation);
 
    const current = nav.find((nav) => nav.href === pathname);
    if (current) {
      current.current = true;
    }
    return nav;
  }, [pathname]);

  return (
    <nav className="dark:bg-dracula-bg-darker border-b-2 dark:border-dracula-purple">
      <LazyMotion features={domAnimation}>
        <div className="mx-auto max-w-7xl px-4">
          <div className="relative flex h-16 items-center justify-between">
            <div className="flex">
              <button className='sm:hidden dark:hover:bg-dracula-bg-light transition-colors duration-100 rounded-sm p-1' onClick={() => setOpen(!open)}>
                {open ? (
                  <XMarkIcon className='rounded-sm dark:stroke-dracula-cyan h-8 w-auto'/>
                ) : (
                  <Bars3Icon className='rounded-sm dark:stroke-dracula-cyan h-8 w-auto'/>
                )}
              </button>
              <Link className='hidden sm:flex items-center p-1 dark:hover:bg-dracula-bg-light transition-colors' href='/'>
                <HomeModernIcon className='dark:stroke-dracula-cyan rounded-sm h-8 w-auto'/>
              </Link>
              <div className='space-x-5 hidden sm:flex ml-10'>
                {activeNavigation.map((item) => (
                  <Link
                    key={item.name}
                    href={item.href}
                    className={`dark:hover:bg-dracula-bg-light transition-colors duration-100 dark:text-white rounded-sm px-3 pt-2 pb-1.5 font-normal border-b-2 border-transparent ${
                      item.current ? 'dark:border-b-dracula-pink' : ''
                    }`}
                    aria-current={item.current ? 'page' : undefined}
                  >
                    {item.name}
                  </Link>
                ))}
              </div>
            </div>
            <div className='space-x-4 flex'>
              <ThemeSwitcher/>
              {LogIn}
            </div>
          </div>
        </div>
        <AnimatePresence>
          { open ? (
            <m.div
              initial={{ height: 0 }}
              animate={{ height: "auto" }}
              transition={{ duration: 0.15, ease: 'linear' }}
              exit={{ height: 0 }}
              className='sm:hidden overflow-hidden'
            >
              <div className='flex flex-col space-y-1 py-1'>
                {activeNavigation.map((item) => (
                  <Link
                    key={item.name}
                    href={item.href}
                    className={`dark:hover:bg-dracula-bg-light transition-colors duration-100 dark:text-white px-2 py-2 font-normal border-l-4 border-transparent ${
                      item.current ? 'dark:border-l-dracula-pink' : ''
                    }`}
                    aria-current={item.current ? 'page' : undefined}
                  >
                    {item.name}
                  </Link>
                ))}
              </div>
            </m.div>
          ) : null}
        </AnimatePresence>
      </LazyMotion>
    </nav>
  );
}