import {
  getScrollContainer
} from "element-plus/lib/utils";

/**
 * 自定义懒加载指令
 */
export default {
    name:'InfiniteScroll',
    inserted(el,binding,vnode){
        //绑定回调函数
        const cb = binding.value;
        //当前组件实例引用
        const vm = vnode.context;
        const container = getScrollContainer(el,true);
        const {delay,immediate} =getScrollOptions(el,vm);
        const onScroll = throttle(delay,handleScroll.bind(el,cb));
        el[scope] = {el,vm,container ,onScroll};
        if(container){
            container.addEventListener('scroll',onScroll);
            if(immediate){
                const observer = el[scope].observer = new MutationObserver(onScroll);
                observer.observe(container,{childList:true,subtree:true});
                onScroll()
            }
        }
    },
    unbind(el){
        const {container,onScroll} = el[scope];
        if(container){
            container.removeEventListener('scrrll',onScroll)
        }
    }
}
