import * as athenaHelper from '../../global/helpers/athena-global-helper.js';
import { athenaConfig, athenaConfigOptions } from '../../global/athena-config.js';
import { scrollPage } from '../helper/infinite-scroll/scroll-page.js';
import { initialUrlParamPageSet } from '../helper/infinite-scroll/initial-url-param-page-set.js';
import { fetchMoreDataPrev } from '../ajax/infinite-scroll/fetch-more-data-prev.js';

//Getting Individual functions from Athena Helper
const {
    setAthenaUid,
    getCookie,
} = athenaHelper;

function infiniteScroll(){
    window.addEventListener('searchDataFetched', () => {

        //Get or Set Athena UID
        athenaConfigOptions.athenaUid = localStorage.getItem('_athena-uid') ? localStorage.getItem('_athena-uid') : setAthenaUid();

        //Get Athena Customer Cookie
        athenaConfigOptions.customerCookie = getCookie('athena_customer_cookie');

        //Check if infinite scroll is active
        if(athenaConfig.InfiniteScrollStatus) {
            //Adding Observer for bottom
            const bottomInfiniteObserver = new IntersectionObserver(scrollPage, { threshold: 0 });
            bottomInfiniteObserver.observe(athenaConfig.bottomObservedElement);

            athenaConfigOptions.loadPrevBtn.addEventListener('click', () => fetchMoreDataPrev());

            initialUrlParamPageSet();
        }
    });
}


export {
    infiniteScroll
}