// redirect-confirm-library.js
(function (window) {
    function RedirectConfirm(options) {
        var defaults = {
            selector: 'a',
            excluding: 'data-rc-exclude',
            title: 'You are now leaving Genmab.com to go to our collaborator’s website. If you continue, we encourage you to read the website’s privacy and cookie policy.',
            message: '',
            continuelbl: 'Continue',
            returnlbl: 'Cancel',
            targetUrl: '_blank'
        };
 
        options = Object.assign({}, defaults, options);
        var confirmed = false;
        var link = [];
 
        function getDomain(hostname) {
            var s = hostname.split(',');
            return s.slice(-2).join('.');
        }
 
        function showModal(a) {
            var modal = document.createElement('div');
            //console.log('====>>>>>> options ==>>> ', options);
            modal.innerHTML = `
                <div id="redirectconfirm-modal" class="modal fade popup-modal">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="req-popup-close popup-close-icon-btn" data-dismiss="modal" aria-hidden="true"></button>                               
                                <h2 class="slds-text-heading_medium modal-main-heading">${options.title}</h2>
                            </div>
                            <div class="modal-body">
                                <p class="modal-content-para">${options.message}</p>
                            </div>
                            <div class="modal-footer">
                                <footer class="modal-footer">
                                    <a href="${a.getAttribute('href')}" target="${options.targetUrl}" class="btn btn-primary btn-continue slds-button primary-cta expand-btn">${options.continuelbl}</a> 
                                    <a href="#" class="btn btn-default btn-cancel slds-button slds-button_brand primary-cta expand-btn" data-dismiss="modal">${options.returnlbl}</a>                                                                                                              
                                </footer>                               
                            </div>
                        </div>
                    </div>
                </div>
            `;
            document.body.appendChild(modal);
 
            var btnContinue = modal.querySelector('.btn-continue');
            var btnReturn = modal.querySelector('.btn-cancel');
            var btnClose = modal.querySelector('.req-popup-close');
            var modalBody = modal.querySelector('.modal-body > p');
 
            modal.addEventListener('show.bs.modal', function () {
                btnContinue.addEventListener('click', function () {
                    confirmed = true;
                    a.click();
                    //modal.modal('hide');
                    modal.style.display = 'none';
                    location.reload();
                });
            });
 
            modal.addEventListener('hide.bs.modal', function () {
                confirmed = false;
            });

            btnClose.addEventListener('click', function(){
                modal.style.display = 'none';
            });
 
            btnReturn.addEventListener('click', function(event){
                event.preventDefault();
                event.stopPropagation();
                modal.style.display = 'none';
            });

            btnContinue.addEventListener('click', function () {
                //modal.modal('hide');
                modal.style.display = 'none';
            });
 
            modalBody.innerHTML = options.message.replace('{url}', a.getAttribute('href'));
            //$(modal).modal('show');
            modal.style.display = 'block';
        }
 
        function handleLinkClick(event) {
            var a = event.target.closest(options.selector);
            var exclude = a.getAttribute(options.excluding);
 
            if (exclude === 'true' && exclude !== 'undefined') {
                a.setAttribute(options.excluding, true);
            }
 
            var currentDomain = getDomain(location.hostname);
            //console.log('>>> exclude==>>>' , !exclude);
            //console.log('>>> host==>>>' , a.hostname);
            //console.log('>>> domain==>>>' , getDomain(a.hostname));
            //console.log('>>> currentDomain==>>>' , currentDomain);
            if (a.hostname && !exclude) {
            //if (a.hostname && getDomain(a.hostname) !== currentDomain && !exclude) {
                if (!confirmed) {
                    link.push(a.getAttribute("href"));
                    event.preventDefault();
                    event.stopPropagation();
                    showModal(a);
                }
            }
        }
 
        // Public API
        return {
            init: function (links) {
                //document.addEventListener('click', handleLinkClick);
                // document.querySelectorAll('a.req-confirmation').forEach((link)=>{
                //     link.addEventListener('click', handleLinkClick);
                // });
                links.forEach((link)=>{
                        link.addEventListener('click', handleLinkClick);
                     });
            }
        };
    }
 
    // Expose the library to the global scope
    window.RedirectConfirm = RedirectConfirm;
 
})(window);