"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const DocumentFragment_js_1 = __importDefault(require("../document-fragment/DocumentFragment.cjs"));
const PropertySymbol = __importStar(require("../../PropertySymbol.cjs"));
const XMLParser_js_1 = __importDefault(require("../../xml-parser/XMLParser.cjs"));
const XMLSerializer_js_1 = __importDefault(require("../../xml-serializer/XMLSerializer.cjs"));
/**
 * ShadowRoot.
 */
class ShadowRoot extends DocumentFragment_js_1.default {
    // Events
    onslotchange = null;
    // Internal properties
    [PropertySymbol.adoptedStyleSheets] = [];
    [PropertySymbol.mode] = 'open';
    [PropertySymbol.host] = null;
    [PropertySymbol.clonable] = false;
    [PropertySymbol.delegatesFocus] = false;
    [PropertySymbol.serializable] = false;
    [PropertySymbol.slotAssignment] = 'named';
    /**
     * Returns mode.
     *
     * @returns Mode.
     */
    get mode() {
        return this[PropertySymbol.mode];
    }
    /**
     * Returns host.
     *
     * @returns Host.
     */
    get host() {
        return this[PropertySymbol.host];
    }
    /**
     * Returns clonable.
     *
     * @returns Clonable.
     */
    get clonable() {
        return this[PropertySymbol.clonable];
    }
    /**
     * Returns delegates focus.
     *
     * @returns Delegates focus.
     */
    get delegatesFocus() {
        // TODO: Implement delegates focus
        return this[PropertySymbol.delegatesFocus];
    }
    /**
     * Returns serializable.
     *
     * @returns Serializable.
     */
    get serializable() {
        return this[PropertySymbol.serializable];
    }
    /**
     * Returns slot assignment.
     *
     * @returns Slot assignment.
     */
    get slotAssignment() {
        return this[PropertySymbol.slotAssignment];
    }
    /**
     * The element that's currently in full screen mode for this shadow tree.
     *
     * @returns Fullscreen element.
     */
    get fullscreenElement() {
        // TODO: Implement fullscreen element
        return null;
    }
    /**
     * Returns the Element within the shadow tree that is currently being presented in picture-in-picture mode.
     *
     * @returns Picture-in-picture element.
     */
    get pictureInPictureElement() {
        // TODO: Implement picture-in-picture element
        return null;
    }
    /**
     * Returns the Element set as the target for mouse events while the pointer is locked. null if lock is pending, pointer is unlocked, or if the target is in another tree.
     *
     * @returns Pointer lock element.
     */
    get pointerLockElement() {
        // TODO: Implement pointer lock element
        return null;
    }
    /**
     * Returns inner HTML.
     *
     * @returns HTML.
     */
    get innerHTML() {
        const xmlSerializer = new XMLSerializer_js_1.default();
        let xml = '';
        for (const node of this[PropertySymbol.nodeArray]) {
            xml += xmlSerializer.serializeToString(node);
        }
        return xml;
    }
    /**
     * Sets inner HTML.
     *
     * @param html HTML.
     */
    set innerHTML(html) {
        const childNodes = this[PropertySymbol.nodeArray];
        while (childNodes.length) {
            this.removeChild(childNodes[0]);
        }
        XMLParser_js_1.default.parse(this[PropertySymbol.ownerDocument], html, { rootNode: this });
    }
    /**
     * Returns adopted style sheets.
     *
     * @returns Adopted style sheets.
     */
    get adoptedStyleSheets() {
        return this[PropertySymbol.adoptedStyleSheets];
    }
    /**
     * Sets adopted style sheets.
     *
     * @param value Adopted style sheets.
     */
    set adoptedStyleSheets(value) {
        this[PropertySymbol.adoptedStyleSheets] = value;
    }
    /**
     * Returns active element.
     *
     * @returns Active element.
     */
    get activeElement() {
        let activeElement = this[PropertySymbol.ownerDocument][PropertySymbol.activeElement];
        let rootNode = activeElement?.getRootNode();
        if (!rootNode || rootNode === this[PropertySymbol.ownerDocument]) {
            return null;
        }
        if (rootNode === this) {
            return activeElement;
        }
        while (rootNode && rootNode !== this) {
            activeElement = rootNode.host;
            rootNode = activeElement.getRootNode();
        }
        return activeElement;
    }
    /**
     * Returns an array of all Animation objects currently in effect, whose target elements are descendants of the shadow tree.
     *
     * @returns Array of animations.
     */
    getAnimations() {
        // TODO: Implement getAnimations()
        return [];
    }
    /**
     * Parses a string of HTML into a document fragment, without sanitization, which then replaces the shadowroot's original subtree. The HTML string may include declarative shadow roots, which would be parsed as template elements the HTML was set using ShadowRoot.innerHTML.
     *
     * @param html HTML.
     */
    setHTMLUnsafe(html) {
        // TODO: Implement support for declarative shadow roots
        const childNodes = this[PropertySymbol.nodeArray];
        while (childNodes.length) {
            this.removeChild(childNodes[0]);
        }
        XMLParser_js_1.default.parse(this[PropertySymbol.ownerDocument], html, { rootNode: this });
    }
    /**
     * Converts to string.
     *
     * @returns String.
     */
    toString() {
        return this.innerHTML;
    }
    /**
     * @override
     */
    [PropertySymbol.cloneNode](deep = false) {
        const clone = super[PropertySymbol.cloneNode](deep);
        clone[PropertySymbol.mode] = this[PropertySymbol.mode];
        clone[PropertySymbol.clonable] = this[PropertySymbol.clonable];
        clone[PropertySymbol.delegatesFocus] = this[PropertySymbol.delegatesFocus];
        clone[PropertySymbol.serializable] = this[PropertySymbol.serializable];
        clone[PropertySymbol.slotAssignment] = this[PropertySymbol.slotAssignment];
        return clone;
    }
}
exports.default = ShadowRoot;
//# sourceMappingURL=ShadowRoot.cjs.map