import { computed } from 'vue'
import { COPY, type Lang } from '~/utils/landingCopy'
import { makeTheme } from '~/utils/landingTokens'

// Locale is owned by @nuxtjs/i18n (URL-based: English at /, Danish at /da, with
// cookie-remembered browser detection). useLang exposes it as the 'da' | 'en'
// the COPY object expects; useCopy maps to the matching translations. `dark` is
// unused machinery from the design's Tweaks panel (the site is light-only).
export const useLang = () => {
  const { locale } = useI18n()
  return computed<Lang>(() => (locale.value === 'da' ? 'da' : 'en'))
}
export const useDark = () => useState<boolean>('dz-dark', () => false)

export const useTheme = () => {
  const dark = useDark()
  return computed(() => makeTheme(dark.value))
}

export const useCopy = () => {
  const { locale } = useI18n()
  return computed(() => COPY[locale.value === 'da' ? 'da' : 'en'])
}

// Setup-only. Returns a click handler that switches to the other locale's
// localized route (flips the URL, e.g. / <-> /da).
export function useLangToggle() {
  const { locale } = useI18n()
  const switchLocalePath = useSwitchLocalePath()
  return () => navigateTo(switchLocalePath(locale.value === 'da' ? 'en' : 'da'))
}

// Setup-only. Localizes an internal href, preserving any #hash. Page links get
// the locale prefix (/about -> /da/about in Danish); section anchors resolve
// against the current locale's home (/#suite -> /da#suite). Bare #hash returns
// unchanged (same-page anchor).
export function useLocalizeHref() {
  const localePath = useLocalePath()
  return (href: string) => {
    if (href.startsWith('#')) return href
    const i = href.indexOf('#')
    const path = i === -1 ? href : href.slice(0, i)
    const hash = i === -1 ? '' : href.slice(i)
    return localePath(path || '/') + hash
  }
}

// Fire a custom Umami event. No-op on the server and when the tracker isn't
// active (e.g. localhost dev, where data-domains limits it to dezky.eu).
export function track(event: string, data?: Record<string, unknown>) {
  if (!import.meta.client) return
  const u = (window as unknown as { umami?: { track: (e: string, d?: unknown) => void } }).umami
  u?.track(event, data)
}

// Smooth-scroll to an in-page anchor, accounting for the sticky 72px nav.
// Non-anchor / placeholder links (#) are ignored.
export function scrollToAnchor(hash: string) {
  if (!hash || hash === '#' || !hash.startsWith('#')) return
  const el = document.getElementById(hash.slice(1))
  if (!el) return
  const top = el.getBoundingClientRect().top + window.scrollY - 72
  window.scrollTo({ top, behavior: 'smooth' })
  history.replaceState(null, '', hash)
}
