import { useEffect, useState } from "react"

import useLang from "@/hooks/useLang"

const SOURCE =
  "https://scandic-bookingengine.s3.eu-central-1.amazonaws.com/script_stage.js"

export function useMeetingPackageWidget(destination?: string) {
  const lang = useLang()
  const [isLoading, setIsLoading] = useState(true)

  useEffect(() => {
    const script = document.createElement("script")
    script.src = SOURCE
    script.setAttribute("langcode", lang || "en")
    script.setAttribute("whitelabel_id", "224905")
    script.setAttribute("widget_id", "scandic_default_new")
    script.setAttribute("version", "frontpage-scandic")
    if (destination) {
      script.setAttribute("destination", destination)
    }
    document.body.appendChild(script)

    function onLoad() {
      setIsLoading(false)
    }

    script.addEventListener("load", onLoad)

    return () => {
      script.removeEventListener("load", onLoad)
      document.body.removeChild(script)
    }
  }, [destination, lang])

  return { isLoading }
}
