"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SVGInstance = void 0;
const createElement_js_1 = require("../utils/createElement.js");
const functions_js_1 = require("../../shared/utils/functions.js");
class SVGInstance {
    constructor(tagName, _parent) {
        this.childInstances = [];
        this.element = (0, createElement_js_1.createElement)(tagName);
        this._parent = _parent;
    }
    get parent() {
        return this._parent;
    }
    get root() {
        let parent = this._parent;
        while ((parent === null || parent === void 0 ? void 0 : parent.parent) !== undefined) {
            parent = parent.parent;
        }
        return parent;
    }
    appendInstance(instance) {
        this.childInstances.push(instance);
        this.element.appendChild(instance.element);
        return this;
    }
    id(id) {
        if (typeof id === "string") {
            this.element.id = id;
            return this;
        }
        return this.element.id;
    }
    get innerHTML() {
        return this.element.innerHTML;
    }
    get outerHTML() {
        return this.element.outerHTML;
    }
    empty() {
        this.childInstances = [];
        return this;
    }
    addClass(classNameOrClassNames) {
        if (typeof classNameOrClassNames === "string") {
            this.element.classList.add(classNameOrClassNames);
        }
        else if (Array.isArray(classNameOrClassNames)) {
            this.element.classList.add(...classNameOrClassNames);
        }
        return this;
    }
    removeClass(classNameOrClassNames) {
        if (typeof classNameOrClassNames === "string") {
            this.element.classList.remove(classNameOrClassNames);
        }
        else if (Array.isArray(classNameOrClassNames)) {
            this.element.classList.remove(...classNameOrClassNames);
        }
        return this;
    }
    hasClass(className) {
        return this.element.classList.contains(className);
    }
    attr(attributeNameOrAttributeObjectOrUndefined, valueOrUndefined) {
        //-- Convert numeric strings to numbers
        var _a;
        const attributes = Object.entries((0, functions_js_1.convertNamedNodeMapToObject)(this.element.attributes)).reduce((previous, [key, value]) => (Object.assign(Object.assign({}, previous), { [value.name]: isNaN(+value.value) ? value.value : +value.value })), {});
        if (typeof attributeNameOrAttributeObjectOrUndefined === "undefined") {
            return attributes;
        }
        else if (typeof attributeNameOrAttributeObjectOrUndefined === "object") {
            if (Array.isArray(attributeNameOrAttributeObjectOrUndefined)) {
                return Object.fromEntries(Object.entries(attributes).filter(([key, value]) => attributeNameOrAttributeObjectOrUndefined.includes(key)));
            }
            else {
                Object.keys(attributeNameOrAttributeObjectOrUndefined).forEach(key => { this.element.setAttribute(key, attributeNameOrAttributeObjectOrUndefined[key] + ""); });
                return this;
            }
        }
        else if (typeof attributeNameOrAttributeObjectOrUndefined === "string") {
            if (typeof valueOrUndefined === "undefined") {
                return (_a = attributes[attributeNameOrAttributeObjectOrUndefined]) !== null && _a !== void 0 ? _a : null;
            }
            else {
                this.element.setAttribute(attributeNameOrAttributeObjectOrUndefined, valueOrUndefined + "");
                return this;
            }
        }
        return null;
    }
    removeAttr(attributeName) {
        this.element.removeAttribute(attributeName);
        return this;
    }
}
exports.SVGInstance = SVGInstance;
//# sourceMappingURL=SVGInstance.js.map