import { SmoothScrollbar } from './smooth_scrollbar';
import { selectors, sbList } from './shared';

import './apis/index';
import './events/index';
import './internals/index';

export default SmoothScrollbar;

/**
 * init scrollbar on given element
 *
 * @param {Element} elem: target element
 * @param {Object} options: scrollbar options
 *
 * @return {Scrollbar} scrollbar instance
 */
SmoothScrollbar.init = (elem, options) => {
    if (!elem || elem.nodeType !== 1) {
        throw new TypeError(`expect element to be DOM Element, but got ${typeof elem}`);
    }

    if (sbList.has(elem)) return sbList.get(elem);

    elem.setAttribute('data-scrollbar', '');

    const children = [...elem.children];

    elem.innerHTML = `
        <article class="scroll-content"></article>
        <aside class="scrollbar-track scrollbar-track-x">
            <div class="scrollbar-thumb scrollbar-thumb-x"></div>
        </aside>
        <aside class="scrollbar-track scrollbar-track-y">
            <div class="scrollbar-thumb scrollbar-thumb-y"></div>
        </aside>
    `;

    const content = elem.querySelector('.scroll-content');

    children.forEach((el) => content.appendChild(el));

    return new SmoothScrollbar(elem, options);
};

/**
 * init scrollbars on pre-defined selectors
 *
 * @param {Object} options: scrollbar options
 *
 * @return {Array} a collection of scrollbar instances
 */
SmoothScrollbar.initAll = (options) => {
    return [...document.querySelectorAll(selectors)].map((el) => {
        return SmoothScrollbar.init(el, options);
    });
};

/**
 * check if scrollbar exists on given element
 *
 * @return {Boolean}
 */
SmoothScrollbar.has = (elem) => {
    return sbList.has(elem);
};

/**
 * get scrollbar instance through given element
 *
 * @param {Element} elem: target scrollbar container
 *
 * @return {Scrollbar}
 */
SmoothScrollbar.get = (elem) => {
    return sbList.get(elem);
};

/**
 * get all scrollbar instances
 *
 * @return {Array} a collection of scrollbars
 */
SmoothScrollbar.getAll = () => {
    return [...sbList.values()];
};

/**
 * destroy scrollbar on given element
 *
 * @param {Element} elem: target scrollbar container
 */
SmoothScrollbar.destroy = (elem) => {
    return SmoothScrollbar.has(elem) && SmoothScrollbar.get(elem).destroy();
};

/**
 * destroy all scrollbars in scrollbar instances
 */
SmoothScrollbar.destroyAll = () => {
    sbList.forEach((sb) => {
        sb.destroy();
    });
};
@@ -706,20 +706,67 @@
 %5D;%0A%0A    
-elem
+const div = document.createElement('div');%0A%0A    div
 .innerHT
@@ -1113,16 +1113,78 @@
     %60;%0A%0A
+    %5B...div.children%5D.forEach((el) =%3E elem.appendChild(el));%0A%0A
     cons

fix ie replace child elements error