import { CUSTOM_ELEMENTS, END, NEXT } from "../shared/symbols.js";
import { customElements } from "../interface/custom-element-registry.js";
import { NodeList } from "../interface/node-list.js";
import { htmlClasses } from "../shared/register-html-class.js";
import { HTMLElement } from "./element.js";
import { Document } from "../interface/document.js";
//#region node_modules/linkedom/esm/html/document.js
var createHTMLElement = (ownerDocument, builtin, localName, options) => {
	if (!builtin && htmlClasses.has(localName)) return new (htmlClasses.get(localName))(ownerDocument, localName);
	const { [CUSTOM_ELEMENTS]: { active, registry } } = ownerDocument;
	if (active) {
		const ce = builtin ? options.is : localName;
		if (registry.has(ce)) {
			const { Class } = registry.get(ce);
			const element = new Class(ownerDocument, localName);
			customElements.set(element, { connected: false });
			return element;
		}
	}
	return new HTMLElement(ownerDocument, localName);
};
/**
* @implements globalThis.HTMLDocument
*/
var HTMLDocument = class extends Document {
	constructor() {
		super("text/html");
	}
	get all() {
		const nodeList = new NodeList();
		let { [NEXT]: next, [END]: end } = this;
		while (next !== end) {
			switch (next.nodeType) {
				case 1:
					nodeList.push(next);
					break;
			}
			next = next[NEXT];
		}
		return nodeList;
	}
	/**
	* @type HTMLHeadElement
	*/
	get head() {
		const { documentElement } = this;
		let { firstElementChild } = documentElement;
		if (!firstElementChild || firstElementChild.tagName !== "HEAD") {
			firstElementChild = this.createElement("head");
			documentElement.prepend(firstElementChild);
		}
		return firstElementChild;
	}
	/**
	* @type HTMLBodyElement
	*/
	get body() {
		const { head } = this;
		let { nextElementSibling } = head;
		if (!nextElementSibling || nextElementSibling.tagName !== "BODY") {
			nextElementSibling = this.createElement("body");
			head.after(nextElementSibling);
		}
		return nextElementSibling;
	}
	/**
	* @type HTMLTitleElement
	*/
	get title() {
		const { head } = this;
		return head.getElementsByTagName("title").at(0)?.textContent || "";
	}
	set title(textContent) {
		const { head } = this;
		let title = head.getElementsByTagName("title").at(0);
		if (title) title.textContent = textContent;
		else head.insertBefore(this.createElement("title"), head.firstChild).textContent = textContent;
	}
	createElement(localName, options) {
		const builtin = !!(options && options.is);
		const element = createHTMLElement(this, builtin, localName, options);
		if (builtin) element.setAttribute("is", options.is);
		return element;
	}
};
//#endregion
export { HTMLDocument };
