import $ from 'jquery';
import { GetYoDigits } from './foundation.core.utils';
import { Plugin } from './foundation.core.plugin';

/**
 * SmoothScroll module.
 * @module foundation.smoothScroll
 */
class SmoothScroll extends Plugin {
  /**
   * Creates a new instance of SmoothScroll.
   * @class
   * @name SmoothScroll
   * @fires SmoothScroll#init
   * @param {Object} element - jQuery object to add the trigger to.
   * @param {Object} options - Overrides to the default plugin settings.
   */
    _setup(element, options) {
        this.$element = element;
        this.options = $.extend({}, SmoothScroll.defaults, this.$element.data(), options);
        this.className = 'SmoothScroll'; // ie9 back compat

        this._init();
    }

    /**
     * Initialize the SmoothScroll plugin
     * @private
     */
    _init() {
        const id = this.$element[0].id || GetYoDigits(6, 'smooth-scroll');
        this.$element.attr({ id });

        this._events();
    }

    /**
     * Initializes events for SmoothScroll.
     * @private
     */
    _events() {
        const handleLinkClick = (e) => {
            // Follow the link it does not point to an anchor.
            if (!$(e.currentTarget).is('a[href^="#"]')) return;

            const arrival = e.currentTarget.getAttribute('href');

            this._inTransition = true;

            SmoothScroll.scrollToLoc(arrival, this.options, () => {
                this._inTransition = false;
            });

            e.preventDefault();
        };

        this.$element.on('click.zf.smoothScroll', handleLinkClick)
        this.$element.on('click.zf.smoothScroll', 'a[href^="#"]', handleLinkClick);
    }

    /**
     * Function to scroll to a given location on the page.
     * @param {String} loc - A properly formatted jQuery id selector. Example: '#foo'
     * @param {Object} options - The options to use.
     * @param {Function} callback - The callback function.
     * @static
     * @function
     */
    static scrollToLoc(loc, options = SmoothScroll.defaults, callback) {
        const $loc = $(loc);

        // Do nothing if target does not exist to prevent errors
        if (!$loc.length) return false;

        var scrollPos = Math.round($loc.offset().top - options.threshold / 2 - options.offset);

        $('html, body').stop(true).animate(
            { scrollTop: scrollPos },
            options.animationDuration,
            options.animationEasing,
            () => {
                if (typeof callback === 'function'){
                    callback();
                }
            }
        );
    }
}

/**
 * Default settings for plugin.
 */
SmoothScroll.defaults = {
  /**
   * Amount of time, in ms, the animated scrolling should take between locations.
   * @option
   * @type {number}
   * @default 500
   */
  animationDuration: 500,
  /**
   * Animation style to use when scrolling between locations. Can be `'swing'` or `'linear'`.
   * @option
   * @type {string}
   * @default 'linear'
   * @see {@link https://api.jquery.com/animate|Jquery animate}
   */
  animationEasing: 'linear',
  /**
   * Number of pixels to use as a marker for location changes.
   * @option
   * @type {number}
   * @default 50
   */
  threshold: 50,
  /**
   * Number of pixels to offset the scroll of the page on item click if using a sticky nav bar.
   * @option
   * @type {number}
   * @default 0
   */
  offset: 0
}

export {SmoothScroll}

@@ -1071,45 +1071,365 @@
     
-const handleLinkClick = (e) =%3E %7B%0A    
+this.$element.on('click.zf.smoothScroll', this._handleLinkClick)%0A        this.$element.on('click.zf.smoothScroll', 'a%5Bhref%5E=%22#%22%5D', this._handleLinkClick);%0A    %7D%0A%0A    /**%0A     * Handle the given event to smoothly scroll to the anchor pointed by the event target.%0A     * @param %7B*%7D e - event%0A     * @function%0A     * @private%0A     */%0A    _handleLinkClick(e) %7B%0A
     
@@ -1483,20 +1483,16 @@
 anchor.%0A
-    
         
@@ -1548,28 +1548,24 @@
 n;%0A%0A        
- 
-   
 const arriva
@@ -1607,36 +1607,32 @@
 ref');%0A%0A        
-    
 this._inTransiti
@@ -1651,20 +1651,16 @@
         
- 
-   
 SmoothSc
@@ -1715,28 +1715,24 @@
             
-    
 this._inTran
@@ -1759,21 +1759,13 @@
     
- 
-   
 %7D);%0A%0A
-    
     
@@ -1796,172 +1796,10 @@
     
- 
-   %7D;%0A%0A        this.$element.on('click.zf.smoothScroll', handleLinkClick)%0A        this.$element.on('click.zf.smoothScroll', 'a%5Bhref%5E=%22#%22%5D', handleLinkClick);%0A    
 %7D
+;
 %0A%0A  

move SmoothScroll click event handling to its own function