"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function wrap(node) {
    if (typeof node === 'string') {
        return new Node(document.createElement(node));
    }
    return new Node(node);
}
exports.default = wrap;
var Node = /** @class */ (function () {
    function Node(node) {
        this.node = node;
    }
    Node.prototype.addAttributes = function (map) {
        var _this = this;
        Object.keys(map).forEach(function (key) {
            if (map.hasOwnProperty(key)) {
                _this.node.setAttribute(key, map[key]);
            }
        });
        return this;
    };
    Node.prototype.addDataAttributes = function (map) {
        var _this = this;
        Object.keys(map).forEach(function (key) {
            if (map.hasOwnProperty(key)) {
                _this.node.dataset[key] = map[key];
            }
        });
        return this;
    };
    Node.prototype.removeAttributes = function () {
        var _this = this;
        var qualifiers = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            qualifiers[_i] = arguments[_i];
        }
        qualifiers.forEach(function (qualifier) {
            _this.node.removeAttribute(qualifier);
        });
        return this;
    };
    Node.prototype.getAttribute = function (qualifier) {
        return this.node.getAttribute(qualifier);
    };
    Node.prototype.addClasses = function () {
        var _this = this;
        var classes = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            classes[_i] = arguments[_i];
        }
        classes.forEach(function (clazz) { return clazz.split(" ").forEach(function (it) { return _this.node.classList.add(it); }); });
        return this;
    };
    Node.prototype.removeClasses = function () {
        var _this = this;
        var classes = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            classes[_i] = arguments[_i];
        }
        classes.forEach(function (clazz) { return _this.node.classList.remove(clazz); });
        return this;
    };
    Node.prototype.containsClass = function (className) {
        return this.node.classList.contains(className);
    };
    Node.prototype.appendChild = function (node) {
        if (typeof node === 'string') {
            this.node.appendChild(document.createTextNode(node));
            return this;
        }
        this.node.appendChild(node.unwrap());
        return this;
    };
    Node.prototype.toggleClass = function (className) {
        if (this.node.classList.contains(className)) {
            this.node.classList.remove(className);
        }
        else {
            this.node.classList.add(className);
        }
    };
    Node.prototype.toggle = function () {
        if (this.node instanceof HTMLInputElement) {
            this.node.checked = !this.node.checked;
        }
    };
    Node.prototype.onClick = function (callback, options) {
        var onClick = function (e) {
            callback(e);
            e.stopPropagation();
            e.preventDefault();
        };
        this.node.addEventListener('click', onClick, options);
    };
    Node.prototype.unwrap = function () {
        return this.node;
    };
    Node.prototype.find = function (selector) {
        return wrap(this.node.querySelector(selector));
    };
    Node.prototype.findAll = function (selector) {
        var elements = this.node.querySelectorAll(selector);
        var array = [];
        elements.forEach(function (element) {
            array.push(wrap(element));
        });
        return array;
    };
    Node.prototype.setStyle = function (map) {
        for (var key in map) {
            this.node.style[key] = map[key];
        }
        return this;
    };
    Node.prototype.innerText = function (text) {
        if (!text)
            return this;
        this.node.innerText = text;
        return this;
    };
    Node.prototype.parent = function () {
        return this.node.parentElement ? wrap(this.node.parentElement) : null;
    };
    Node.prototype.removeChild = function (node) {
        if (node) {
            this.node.removeChild(node.unwrap());
        }
    };
    Node.prototype.setName = function (name) {
        if (!name)
            return this;
        this.node.setAttribute("name", name);
        return this;
    };
    Node.prototype.setId = function (id) {
        if (!id)
            return this;
        this.node.id = id;
        return this;
    };
    Node.prototype.setRequired = function (required) {
        if (!required)
            return this;
        this.node.setAttribute("required", "required");
        this.node.setAttribute("data-required", String(required));
        return this;
    };
    Node.prototype.getParentByClass = function (className) {
        var node = wrap(this.node);
        if (node.parent()) {
            var parent_1 = node.parent();
            while (parent_1) {
                if (parent_1.containsClass(className)) {
                    return parent_1;
                }
                if (parent_1.parent()) {
                    parent_1 = parent_1.parent();
                }
                else
                    return;
            }
        }
        return;
    };
    Node.prototype.getChildWithId = function () {
        var childWithId = Array.from(this.node.children).find(function (child) { return child && !!child.id; });
        if (childWithId) {
            return wrap(childWithId);
        }
        return null;
    };
    Node.prototype.setAriaLabel = function (label) {
        if (label) {
            this.node.setAttribute("aria-label", label);
        }
        return this;
    };
    return Node;
}());
//# sourceMappingURL=node.js.map