if (!window.stellaWidget) {
  const stellaWidget = (function () {
    const src = `https://stella2.demand-iq.com/`
    const appUrl = `https://nature-coast-solar.estimate.demand-iq.com`
    const launcherType = `none`
    let launcherPosition = `right`
    const launcher_class = `diq-stella-2`

    if (!["left", "right"].includes(launcherPosition)) {
      launcherPosition = "right"
    }
    let height = null

    const urlParams = new URLSearchParams(document.location.search)
    const progressId = urlParams.get("stella_progress_id") ?? ""
    urlParams.delete("stella_progress_id")
    const referringUrl = encodeURIComponent(`${document.location.origin}${document.location.pathname}?${urlParams}`)
    const sourceUrl = encodeURIComponent(document.referrer)

    const iframe = document.createElement("iframe")
    iframe.src = `${src}?app_url=${appUrl}&source_url=${sourceUrl}&referring_url=${referringUrl}&progress_id=${progressId}`
    iframe.width = "100%"
    iframe.height = "100%"
    iframe.style = "border: none;"

    const container = document.createElement("div")
    container.style.position = "fixed"
    container.style.zIndex = "2147483647"
    container.appendChild(iframe)
    document.body.appendChild(container)

    const setHeight = function (h) {
      height = h
    }

    const resetContainer = function (expand, init = false) {
      if (launcherType === "icon") {
        container.style.bottom = 0
        container.style.right = launcherPosition === "right" ? 0 : "auto"
        container.style.left = launcherPosition === "right" ? "auto" : 0

        if (expand) {
          container.style.width = "100%"
          container.style.height = "100%"
        } else {
          container.style.width = "100px"
          container.style.height = "100px"
        }
      }

      if (launcherType === "drawer") {
        container.style.bottom = expand ? 0 : "50%"
        container.style.right = 0
        container.style.left = "auto"
        container.style.transform = expand ? "none" : "translateY(50%)"

        if (expand) {
          container.style.width = "100%"
          container.style.height = "100%"
        } else {
          container.style.width = "50px"
          container.style.height = height ?? "100%"
        }
      }

      if (launcherType === "none") {
        container.style.bottom = 0

        if (expand) {
          container.style.width = "100%"
          container.style.height = "100%"
        } else {
          container.style.width = 0
          container.style.height = 0
        }
      }

      if (init) {
        container.style.width = 0
        container.style.height = 0
      }

      iframe.style.maxWidth = "100%"
      iframe.style.maxHeight = "100%"
    }

    const toggle = () => {
      iframe.contentWindow.postMessage({ type: "stella-embed-wizard-toggle" }, "*")
      resetContainer(true)
    }

    const show = () => {
      iframe.contentWindow.postMessage({ type: "stella-embed-wizard-show" }, "*")
      resetContainer(true)
    }

    const hide = () => {
      iframe.contentWindow.postMessage({ type: "stella-embed-wizard-hide" }, "*")
      resetContainer(false)
    }

    const handleLauncherClick = e => {
      e.preventDefault()

      show()
    }

    const launcherElements = document.getElementsByClassName(launcher_class)
    for (let i = 0; i < launcherElements.length; i++) {
      launcherElements[i].addEventListener("click", handleLauncherClick.bind(this))
    }

    resetContainer(false, true)

    return {
      setHeight,
      resetContainer,
      toggle,
      show,
      hide,
    }
  })()

  window.stellaWidget = stellaWidget

  window.addEventListener("message", function (e) {
    const { type, height } = e.data

    if (type === "stella-embed-wizard-set-height") {
      window.stellaWidget.setHeight(height)
      window.stellaWidget.resetContainer(false)
    }

    if (type === "stella-embed-wizard-open") {
      window.stellaWidget.resetContainer(true)
    }

    if (type === "stella-embed-wizard-close") {
      window.stellaWidget.resetContainer(false)
    }
  })
}
