const MD_BREAKPOINT = 768
const MOBILE_OFFSET = 96
const DESKTOP_OFFSET = 32

export function scrollToElement(href: string, offset?: number) {
    if (href === '#') {
        window.scrollTo({ top: 0, behavior: 'smooth' })
        return
    }

    const id = href.startsWith('#') ? href.slice(1) : href
    const element = document.getElementById(id)

    if (element) {
        const isMobile = window.innerWidth < MD_BREAKPOINT
        const scrollOffset = offset ?? (isMobile ? MOBILE_OFFSET : DESKTOP_OFFSET)
        const top = element.getBoundingClientRect().top + window.scrollY - scrollOffset
        window.scrollTo({ top, behavior: 'smooth' })
    }
}
