import { h, s } from "../../hastscript/lib/index.js";
import { webNamespaces } from "../../web-namespaces/index.js";
function fromDom(e, n) {
	return transform(e, n || {}) || {
		type: "root",
		children: []
	};
}
function transform(e, n) {
	let r = one(e, n);
	return r && n.afterTransform && n.afterTransform(e, r), r;
}
function one(e, n) {
	switch (e.nodeType) {
		case 1: return element(e, n);
		case 3: return text(e);
		case 8: return comment(e);
		case 9: return root(e, n);
		case 10: return doctype();
		case 11: return root(e, n);
		default: return;
	}
}
function root(e, n) {
	return {
		type: "root",
		children: all(e, n)
	};
}
function doctype() {
	return { type: "doctype" };
}
function text(e) {
	return {
		type: "text",
		value: e.nodeValue || ""
	};
}
function comment(e) {
	return {
		type: "comment",
		value: e.nodeValue || ""
	};
}
function element(i, a) {
	let o = i.namespaceURI, c = o === webNamespaces.svg ? s : h, l = o === webNamespaces.html ? i.tagName.toLowerCase() : i.tagName, u = o === webNamespaces.html && l === "template" ? i.content : i, d = i.getAttributeNames(), f = {}, p = -1;
	for (; ++p < d.length;) f[d[p]] = i.getAttribute(d[p]) || "";
	return c(l, f, all(u, a));
}
function all(e, n) {
	let r = e.childNodes, i = [], o = -1;
	for (; ++o < r.length;) {
		let e = transform(r[o], n);
		e !== void 0 && i.push(e);
	}
	return i;
}
export { fromDom };
