import { find } from "../../property-information/lib/find.js";
import { html, svg } from "../../property-information/index.js";
import { stringify } from "../../comma-separated-tokens/index.js";
import { stringify as stringify$1 } from "../../space-separated-tokens/index.js";
import { webNamespaces } from "../../web-namespaces/index.js";
import { ok } from "../../devlop/lib/default.js";
import { zwitch } from "../../zwitch/index.js";
var emptyOptions = {}, own = {}.hasOwnProperty, one = zwitch("type", { handlers: {
	root,
	element,
	text,
	comment,
	doctype
} });
function toParse5(e, m) {
	let h = (m || emptyOptions).space;
	return one(e, h === "svg" ? svg : html);
}
function root(e, f) {
	let p = {
		nodeName: "#document",
		mode: (e.data || {}).quirksMode ? "quirks" : "no-quirks",
		childNodes: []
	};
	return p.childNodes = all(e.children, p, f), patch(e, p), p;
}
function fragment(e, f) {
	let p = {
		nodeName: "#document-fragment",
		childNodes: []
	};
	return p.childNodes = all(e.children, p, f), patch(e, p), p;
}
function doctype(e) {
	let f = {
		nodeName: "#documentType",
		name: "html",
		publicId: "",
		systemId: "",
		parentNode: null
	};
	return patch(e, f), f;
}
function text(e) {
	let f = {
		nodeName: "#text",
		value: e.value,
		parentNode: null
	};
	return patch(e, f), f;
}
function comment(e) {
	let f = {
		nodeName: "#comment",
		data: e.value,
		parentNode: null
	};
	return patch(e, f), f;
}
function element(e, f) {
	let m = f, h = m;
	e.type === "element" && e.tagName.toLowerCase() === "svg" && m.space === "html" && (h = svg);
	let _ = [], v;
	if (e.properties) {
		for (v in e.properties) if (v !== "children" && own.call(e.properties, v)) {
			let f = createProperty(h, v, e.properties[v]);
			f && _.push(f);
		}
	}
	let y = h.space, b = {
		nodeName: e.tagName,
		tagName: e.tagName,
		attrs: _,
		namespaceURI: webNamespaces[y],
		childNodes: [],
		parentNode: null
	};
	return b.childNodes = all(e.children, b, h), patch(e, b), e.tagName === "template" && e.content && (b.content = fragment(e.content, h)), b;
}
function createProperty(f, p, _) {
	let v = find(f, p);
	if (_ === !1 || _ == null || typeof _ == "number" && Number.isNaN(_) || !_ && v.boolean) return;
	Array.isArray(_) && (_ = v.commaSeparated ? stringify(_) : stringify$1(_));
	let y = {
		name: v.attribute,
		value: _ === !0 ? "" : String(_)
	};
	if (v.space && v.space !== "html" && v.space !== "svg") {
		let e = y.name.indexOf(":");
		e < 0 ? y.prefix = "" : (y.name = y.name.slice(e + 1), y.prefix = v.attribute.slice(0, e)), y.namespace = webNamespaces[v.space];
	}
	return y;
}
function all(e, f, p) {
	let m = -1, h = [];
	if (e) for (; ++m < e.length;) {
		let g = one(e[m], p);
		g.parentNode = f, h.push(g);
	}
	return h;
}
function patch(e, f) {
	let p = e.position;
	p && p.start && p.end && (p.start.offset, p.end.offset, f.sourceCodeLocation = {
		startLine: p.start.line,
		startCol: p.start.column,
		startOffset: p.start.offset,
		endLine: p.end.line,
		endCol: p.end.column,
		endOffset: p.end.offset
	});
}
export { toParse5 };
