'use strict';

exports.__esModule = true;

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

/* eslint no-restricted-syntax: 0 */
var preventDefault = function preventDefault(e) {
    if (typeof e.preventDefault !== 'undefined') {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
};

var stopPropagation = function stopPropagation(e) {
    if (typeof e.stopPropagation !== 'undefined') {
        e.stopPropagation();
    } else {
        e.cancelBubble = true;
    }
};

// http://blog.garstasio.com/you-dont-need-jquery/events/#sending-custom-events
var dispatchEvent = function dispatchEvent(el, eventType) {
    var evt = document.createEvent('Event');
    evt.initEvent(eventType, true, true); // can bubble, and is cancellable
    el.dispatchEvent(evt);
};

// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Compatibility
var addEventListener = function addEventListener(target, type, listener) {
    if (target.addEventListener) {
        // Standard
        target.addEventListener(type, listener, false);
    } else if (target.attachEvent) {
        // IE8
        // In Internet Explorer versions before IE 9, you have to use attachEvent rather than the standard addEventListener.
        target.attachEvent('on' + type, listener);
    }
};

// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener
var removeEventListener = function removeEventListener(target, type, listener) {
    if (target.removeEventListener) {
        // Standard
        target.removeEventListener(type, listener, false);
    } else if (target.detachEvent) {
        // IE8
        // In Internet Explorer versions before IE 9, you have to use detachEvent rather than the standard removeEventListener.
        target.detachEvent('on' + type, listener);
    }
};

var hasClass = function hasClass(el, className) {
    if (!el) {
        return false;
    }
    var classes = el.className.split(' ');
    return classes.indexOf(className) >= 0;
};

var addClass = function addClass(el, className) {
    if (!el) {
        return '';
    }
    if (!hasClass(el, className)) {
        var classes = el.className.split(' ');
        el.className = classes.concat(className).join(' ');
    }
    return el.className;
};

var removeClass = function removeClass(el, className) {
    if (!el) {
        return '';
    }
    if (hasClass(el, className)) {
        var classes = el.className.split(' ');
        el.className = classes.filter(function (c) {
            return c !== className;
        }).join(' ');
    }
    return el.className;
};

var toggleClass = function toggleClass(el, className) {
    if (!el) {
        return;
    }
    if (hasClass(el, className)) {
        removeClass(el, className);
    } else {
        addClass(el, className);
    }
};

var classNames = function classNames() {
    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
    }

    var classNames = [];
    args.forEach(function (arg) {
        if (Array.isArray(arg)) {
            classNames = classNames.concat(arg);
        } else if ((typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) === 'object') {
            Object.keys(arg).forEach(function (className) {
                var ok = arg[className];
                if (!!ok) {
                    classNames.push(className);
                }
            });
        } else {
            classNames.push(arg);
        }
    });
    return classNames.join(' ');
};

// http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object

//Returns true if it is a DOM element
var isDOMElement = function isDOMElement(o) {
    if ((typeof HTMLElement === 'undefined' ? 'undefined' : _typeof(HTMLElement)) === 'object') {
        return o instanceof HTMLElement;
    }
    return o && (typeof o === 'undefined' ? 'undefined' : _typeof(o)) === 'object' && o.nodeType === 1 && typeof o.nodeName === 'string';
};

// Returns true if it is a DOM node
var isDOMNode = function isDOMNode(o) {
    if ((typeof Node === 'undefined' ? 'undefined' : _typeof(Node)) === 'object') {
        return o instanceof Node;
    }
    return o && (typeof o === 'undefined' ? 'undefined' : _typeof(o)) === 'object' && typeof o.nodeType === 'number' && typeof o.nodeName === 'string';
};

var quoteattr = function quoteattr(s, preserveCR) {
    preserveCR = preserveCR ? '&#13;' : '\n';
    return ('' + s). /* Forces the conversion to string. */
    replace(/&/g, '&amp;') /* This MUST be the 1st replacement. */
    .replace(/'/g, '&apos;') /* The 4 other predefined entities, required. */
    .replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
    /*
     * You may add other replacements here for HTML only
     * (but it's not necessary).
     * Or for XML, only if the named entities are defined in its DTD.
     */
    .replace(/\r\n/g, preserveCR) /* Must be before the next replacement. */
    .replace(/[\r\n]/g, preserveCR);
};

/**
 * Example #1:
 * =========================================================================
 * <a id="mymail href="http://mymail.example.com">My Mail</a>
 *
 * buildHTML('a', 'My Mail', {
 *     id: 'mymail',
 *     href: 'http://mymail.example.com'
 * });
 *
 * Example #2:
 * =========================================================================
 * <input id="myinput" type="text" value="myvalue" />
 *
 * buildHTML('input', {
 *   id: 'myinput',
 *   type: 'text',
 *   value: 'myvalue'
 * });
 *
 */
var buildHTML = function buildHTML(tag, html, attrs) {
    switch (tag) {
        case 'select':
            if ((typeof html === 'undefined' ? 'undefined' : _typeof(html)) === 'object') {
                var options = html || {};
                html = '';
                for (var value in options) {
                    if (!options.hasOwnProperty(value)) {
                        continue;
                    }
                    html += buildHTML('option', options[value] || '', { value: value });
                }
            }
            break;

        default:
            if ((typeof html === 'undefined' ? 'undefined' : _typeof(html)) === 'object') {
                attrs = html;
                html = undefined;
            }
            break;
    }

    var h = '<' + tag;
    for (var attr in attrs) {
        if (!attrs.hasOwnProperty(attr)) {
            continue;
        }
        if (typeof attrs[attr] !== 'undefined') {
            h += ' ' + attr + '="' + quoteattr(attrs[attr]) + '"';
        }
    }
    h += typeof html !== 'undefined' ? '>' + html + '</' + tag + '>' : '/>';

    return h;
};

exports.preventDefault = preventDefault;
exports.stopPropagation = stopPropagation;
exports.dispatchEvent = dispatchEvent;
exports.addEventListener = addEventListener;
exports.removeEventListener = removeEventListener;
exports.hasClass = hasClass;
exports.addClass = addClass;
exports.removeClass = removeClass;
exports.toggleClass = toggleClass;
exports.classNames = classNames;
exports.isDOMElement = isDOMElement;
exports.isDOMNode = isDOMNode;
exports.quoteattr = quoteattr;
exports.buildHTML = buildHTML;