'use client';
import { useEffect, useState } from 'react';
import { ArrowUp } from 'lucide-react';
import { usePathname } from "next/navigation";

export default function BackToTopButton({ settingsMap, locale }: any) {
  const [isVisible, setIsVisible] = useState(false);
  const pathname = usePathname();

  const allPaths = ["/", "/dashboard","/account-settings","/my-address","/listing","/my-orders","/order-received","/rating-reviews","/save-search","/subscription","/my-banks","/notifications","/privacy-policy"]

  useEffect(() => {
    const scrollContainer = document.getElementById('page-wrapper');

    if (!scrollContainer) return;

    const handleScroll = () => {
      const scrollTop = scrollContainer.scrollTop;
      const scrollHeight = scrollContainer.scrollHeight;
      const clientHeight = scrollContainer.clientHeight;

      const isNearBottom = scrollTop + clientHeight >= scrollHeight - 100;
      setIsVisible(scrollTop > 100 || isNearBottom);
    };

    scrollContainer.addEventListener('scroll', handleScroll);
    handleScroll(); 
    return () => scrollContainer.removeEventListener('scroll', handleScroll);
  }, []);

  const scrollToTop = () => {
    const scrollContainer = document.getElementById('page-wrapper');
    if (scrollContainer) {
      scrollContainer.scrollTo({ top: 0, behavior: 'smooth' });
    }
  };

  return (
    <>
      {(settingsMap?.siteSettings?.siteSettings?.contact_information?.contact_support_phone?.[locale] && allPaths.includes(pathname)) && (
        <a
          href={`https://wa.me/${(settingsMap?.siteSettings?.siteSettings?.contact_information?.contact_support_phone?.[locale] || "").replace(/[^0-9]/g, "")}`}
          target="_blank"
          rel="noopener noreferrer"
          className="fixed bottom-6 right-20 z-10 max-md:bottom-20 flex items-center gap-2 rtl:left-20 rtl:right-auto"
        >
          <img
            src="/socialicons/whatsapp.svg"
            alt="whatsapp"
            width={43}
            height={43}
            className="transition-transform duration-300 ease-in-out hover:scale-105"
          />
        </a>
      )}

      <button
        onClick={scrollToTop}
        aria-label="Scroll to top"
        className={`fixed bottom-6 right-6 z-20 max-md:bottom-20 flex items-center justify-center p-3 rounded-full text-white bg-primary500 hover:bg-primary600 hover:shadow-lg hover:shadow-primary500/30 active:translate-y-0 active:shadow-md dark:bg-primary400 dark:hover:bg-primary300 dark:hover:shadow-primary400/30 focus:ring-2 focus:ring-primary500 focus:ring-offset-2 dark:focus:ring-primary400 dark:focus:ring-offset-gray-900 before:absolute before:inset-0 before:bg-gradient-to-r before:from-transparent before:via-white/20 before:to-transparent before:-translate-x-full hover:before:translate-x-full before:transition-transform before:duration-700 overflow-hidden ${
          isVisible ? 'opacity-100 visible' : 'opacity-0 invisible'
        }`}
      >
        <ArrowUp className="w-5 h-5" />
      </button>
    </>
  );
}
