import { useEffect } from 'react'
import { useLocation } from 'react-router-dom'

const BASE_TITLE = 'Любимый Креатив — Изделия ручной работы'

let didPageTitleSet = false

export function usePageTitle(title: string | null) {
  useEffect(() => {
    didPageTitleSet = true
    document.title = title ? `${title} — Любимый Креатив` : BASE_TITLE
  }, [title])
}

export function usePageTitleReset() {
  const location = useLocation()

  useEffect(() => {
    if (!didPageTitleSet) {
      document.title = BASE_TITLE
    }
    didPageTitleSet = false
  }, [location.pathname])
}
