import { inject, onBeforeUnmount, watch } from 'vue'

export default (callback = () => {}, instanceId) => {
    const instanceKey = `lenis${instanceId ? `-${instanceId}` : ''}`
    const lenis = inject(instanceKey)

    watch(
        lenis,
        () => {
            if (lenis.value) {
                lenis.value.on('scroll', callback)
            }
        },
        { immediate: true },
    )

    onBeforeUnmount(() => lenis.value?.off('scroll', callback))

    return lenis
}
