/**
 * @author Elmar Abdurayimov <e.abdurayimov@gmail.com>
 * @copyright 2020 PK
 */

(function( $ ) {
    var Manager = function() {
        var self = this;

        self.isValid = function() {
            return !!$( '#modalExitPopup' ).length;
        };

        self.init = function() {
            if( !self.isValid() ) return;

            $( '#modalExitPopup' ).exitModal( { numberToShown: 1 } );

            $( '#processingModal' )
                .on( "show.bs.modal", self.disable )
                .on( "hide.bs.modal", self.enable );

            $( document ).on( 'DOMNodeInserted', function( e ) {
                if( !$( e.target ).hasClass( 'paypal-checkout-sandbox' ) ) return;

                self.disable();
            } );

            $( document ).on( 'DOMNodeRemoved', function( e ) {
                if( !$( e.target ).hasClass( 'paypal-checkout-sandbox' ) ) return;

                self.enable();
            } );
        };

        self.enable = function() {
            if( !self.isValid() ) return;

            $( '#modalExitPopup' ).data( 'exitModal' ).settings.pageYValueForEventFired = 10;
        };

        self.disable = function() {
            if( !self.isValid() ) return;

            $( '#modalExitPopup' ).data( 'exitModal' ).settings.pageYValueForEventFired = -10000;
        };
    };

    window.ExitIntentPopupManager = (new Manager);
})( jQuery );