import { watch, ref } from 'vue'
import { isString } from '@/utils/is'
import { useI18n } from './useI18n'
const mTitle = import.meta.env.VITE_APP_TITLE

export const useTitle = (newTitle) => {
  const { t } = useI18n()
  const title = ref(
    newTitle ? `${mTitle} - ${t(newTitle)}` : mTitle
  )

  watch(
    title,
    (n, o) => {
      if (isString(n) && n !== o && document) {
        document.title = n
      }
    },
    { immediate: true }
  )

  return title
}
