import { useRouter } from 'solito/router'

const usePageOpener = () => {
  const router = useRouter()

  const formatTitleForURL = (title: string) => {
    return title
      .toLowerCase()
      .replace(/\s+/g, '-') // Replace whitespace with dash
      .replace(/[^\w-]+/g, '') // Remove disallowed special characters
      .replace(/^-+|-+$/g, '') // Remove leading dashes and trailing dashes
  }

  const openTopicPage = (target_title: string) => {
    const formattedTitle = formatTitleForURL(target_title)
    router.push(`/topic/${formattedTitle}`)
  }

  const openPostPage = (target_title: string) => {
    const formattedTitle = formatTitleForURL(target_title)
    router.push(`/post/${formattedTitle}`)
  }

  const openPageLink = (link_target_kind: string, link_code: string) => {
    router.push(`/${link_target_kind}/${link_code}`)
  }

  return { openTopicPage, openPostPage, openPageLink }
}

export default usePageOpener
