import { FC, PropsWithChildren, useState } from "react";
import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover";
import { Chevron, HoverMenu } from "./";
import { Link, useLocation } from "react-router-dom";
import { Modal } from "./modal";
import { DropDownContent } from "@/locales/types/nav.type";
import { cn } from "@/lib/utils";
import { useUiStore } from "@/store/ui";
import { useLenis } from "lenis/react";

interface Props {
  className?: string;
  title?: string;
  color?: string;
  triggerClassName?: string;

  dropDownContent?: DropDownContent[];
}

export const Menu: FC<PropsWithChildren<Props>> = ({
  title,
  dropDownContent,
  color,
  triggerClassName,
  children,
}) => {
  const [isOpen, setIsOpen] = useState(false);
  const setSheet = useUiStore((state) => state.setSheet);
  const lenis = useLenis();
  const location = useLocation();

  return (
    <Popover open={isOpen} onOpenChange={() => setIsOpen(!isOpen)}>
      <PopoverTrigger
        className={cn("flex items-center gap-2", triggerClassName)}
      >
        {title}
        {children}
        {!children && (
          <Chevron
            color={color}
            className={cn("transition-all", isOpen && "rotate-180")}
          />
        )}
      </PopoverTrigger>

      <PopoverContent className="w-fit relative z-[200] px-0 bg-surface_container">
        {dropDownContent &&
          dropDownContent.map((item, i) =>
            item.hover ? (
              <HoverMenu setIsOpen={setIsOpen} key={i} {...item} />
            ) : item.link ? (
              <Link
                key={i}
                to={item.link ?? ""}
                onClick={() => {
                  setIsOpen(false);
                  setSheet(false);

                  const hash = item.link?.split("#")[1];
                  const path = item.link?.split("#")[0] || "/";
                  if (hash && location.pathname === path) {
                    const el = document.getElementById(hash);
                    if (el && lenis) {
                      lenis.scrollTo(el, { offset: -140, duration: 0.8 });
                    }
                  }
                }}
                className="h-14 px-3 flex gap-3 relative justify-between cursor-pointer items-center hover:bg-slate-300/50 transition-all"
                target={item.blank ? "_blank" : undefined}
                rel={item.blank ? "noopener noreferrer" : undefined}
              >
                {item.text}
                {item.blank && <img src="/pdf.svg" />}
              </Link>
            ) : item.modal ? (
              <Modal key={item.text} title={item.text} />
            ) : (
              <div className="relative" key={i}>
                <div
                  className="h-14 px-3 py-2 flex items-center cursor-pointer hover:bg-slate-300/50 transition-all"
                  onClick={() => {
                    setIsOpen(false);
                    setSheet(false);
                  }}
                >
                  {item.text}
                </div>
              </div>
            )
          )}
      </PopoverContent>
    </Popover>
  );
};
