/**
 * @module
 * @prototype {Function} destroy
 */

import { SmoothScrollbar } from '../smooth_scrollbar';
import { setStyle } from '../utils';
import { sbList } from '../shared';

export { SmoothScrollbar };

/**
 * @method
 * @api
 * Remove all scrollbar listeners and event handlers
 * Reset
 */
SmoothScrollbar.prototype.destroy = function() {
    const { __listeners, __handlers, targets } = this;
    const { container, content } = targets;

    __handlers.forEach(({ evt, elem, fn }) => {
        elem.removeEventListener(evt, fn);
    });

    __handlers.length = __listeners.length = 0;

    this.scrollTo(0, 0, 300, () => {
        // stop render
        cancelAnimationFrame(this.__timerID.render);

        // remove form sbList
        sbList.delete(container);

        // check if element has been removed from DOM
        if (!container.parentNode) {
            return;
        }

        // reset scroll position
        setStyle(container, {
            overflow: ''
        });

        container.scrollTop = container.scrollLeft = 0;

        // reset content
        const childNodes = [...content.childNodes];

        container.innerHTML = '';

        childNodes.forEach((el) => container.appendChild(el));
    });
};

@@ -286,14 +286,73 @@
 s%0A *
- Reset
+%0A * @param %7BBoolean%7D isRemoval: whether node is removing from DOM
 %0A */
@@ -397,16 +397,25 @@
 unction(
+isRemoval
 ) %7B%0A    
@@ -510,16 +510,39 @@
 rgets;%0A%0A
+    // remove handlers%0A
     __ha
@@ -686,49 +686,8 @@
 0;%0A%0A
-    this.scrollTo(0, 0, 300, () =%3E %7B%0A    
     
@@ -701,24 +701,37 @@
  render%0A    
+this.stop();%0A
     cancelAn
@@ -768,28 +768,24 @@
 nder);%0A%0A    
- 
-   
 // remove fo
@@ -794,20 +794,16 @@
  sbList%0A
-    
     sbLi
@@ -821,24 +821,108 @@
 ontainer);%0A%0A
+    if (isRemoval) return;%0A%0A    // restore DOM%0A    this.scrollTo(0, 0, 300, () =%3E %7B%0A
         // c

add `isRemoval` param