"use client"

import { usePathname } from "next/navigation"
import { useEffect } from "react"

import useLang from "@/hooks/useLang"

import { CHATBOT_HIDE_ROUTES } from "./constants"

interface ChatbotRouteChangeProps {
  liveLangs: string[]
}

export function ChatbotRouteChange({ liveLangs }: ChatbotRouteChangeProps) {
  const pathName = usePathname()
  const currentLang = useLang()

  useEffect(() => {
    const isLive = liveLangs.includes(currentLang)
    const shouldHideChatbot = CHATBOT_HIDE_ROUTES.some((route) =>
      pathName.includes(route)
    )
    if (window.kindlyChat) {
      if (shouldHideChatbot || !isLive) {
        window.kindlyChat.closeChat()
        window.kindlyChat.hideBubble()
      } else {
        window.kindlyChat.showBubble()
      }
    }
  }, [pathName, liveLangs, currentLang])

  return null
}
