var Wick = window.Wick || {};
window.Wick = Wick;

Wick.BillieModalScrollReset = (function () {
    const MODAL_ID = '#billieLearnMoreModal';
    const OPEN_LINK = '.billie__link[data-target="#billieLearnMoreModal"]';
    const SCROLL_BODY = '.modal-body.modal-box__body';
    const CONTENT = '.modal-content.modal-box__content';

    function resetScroll($modal) {
        if (!$modal || !$modal.length) {
            return;
        }

        let bodyEl = $modal.find(SCROLL_BODY)[0];
        if (bodyEl) {
            bodyEl.scrollTop = 0;
        }

        $modal.find(CONTENT).attr('data-scroll-state', 'top');
    }

    function bindEvents($modal) {
        $(document).on('click', OPEN_LINK, function () {
            resetScroll($modal);
        });

        $(document).on('shown.bs.modal', MODAL_ID, function () {
            resetScroll($modal);
            requestAnimationFrame(function () {
                resetScroll($modal);
            });
            setTimeout(function () {
                resetScroll($modal);
            }, 30);
        });

        $(document).on('hidden.bs.modal', MODAL_ID, function () {
            resetScroll($modal);
        });
    }

    function init() {
        $(function () {
            let $modal = $(MODAL_ID);
            if (!$modal.length) return;

            bindEvents($modal);
        });
    }

    return {
        init: init,
    };
})();

Wick.BillieModalScrollReset.init();
