'use client'

import {useEffect} from 'react'

export function useScrollTarget() {
    const scrollToSection = (id: string) => {
        if (id === 'top') {
            window.scrollTo({top: 0, behavior: 'smooth'})
            return
        }

        const el = document.getElementById(id)
        if (el) {
            el.scrollIntoView({behavior: 'smooth', block: 'start'})
        }
    }

    const handleSectionClick = (id: string) => {
        if (window.location.pathname !== '/') {
            localStorage.setItem('scrollTarget', id)
            window.location.href = '/'
        } else {
            scrollToSection(id)
        }
    }

    useEffect(() => {
        const target = localStorage.getItem('scrollTarget')
        if (target) {
            localStorage.removeItem('scrollTarget')
            scrollToSection(target)
        }
    }, [])

    return {scrollToSection, handleSectionClick}
}
