import { find } from "../../property-information/lib/find.js";
import { html, svg } from "../../property-information/index.js";
import { h, s } from "../../hastscript/lib/index.js";
import { webNamespaces } from "../../web-namespaces/index.js";
import { ok } from "../../devlop/lib/default.js";
import { location } from "../../vfile-location/lib/index.js";
var own = {}.hasOwnProperty, proto = Object.prototype;
function fromParse5(e, u) {
	let d = u || {};
	return one({
		file: d.file || void 0,
		location: !1,
		schema: d.space === "svg" ? svg : html,
		verbose: d.verbose || !1
	}, e);
}
function one(e, c) {
	let l;
	switch (c.nodeName) {
		case "#comment": {
			let u = c;
			return l = {
				type: "comment",
				value: u.data
			}, patch(e, u, l), l;
		}
		case "#document":
		case "#document-fragment": {
			let u = c, d = "mode" in u ? u.mode === "quirks" || u.mode === "limited-quirks" : !1;
			if (l = {
				type: "root",
				children: all(e, c.childNodes),
				data: { quirksMode: d }
			}, e.file && e.location) {
				let c = String(e.file), u = location(c), d = u.toPoint(0), f = u.toPoint(c.length);
				l.position = {
					start: d,
					end: f
				};
			}
			return l;
		}
		case "#documentType": {
			let u = c;
			return l = { type: "doctype" }, patch(e, u, l), l;
		}
		case "#text": {
			let u = c;
			return l = {
				type: "text",
				value: u.value
			}, patch(e, u, l), l;
		}
		default: return l = element(e, c), l;
	}
}
function all(e, c) {
	let l = -1, u = [];
	for (; ++l < c.length;) {
		let d = one(e, c[l]);
		u.push(d);
	}
	return u;
}
function element(e, p) {
	let m = e.schema;
	e.schema = p.namespaceURI === webNamespaces.svg ? svg : html;
	let _ = -1, v = {};
	for (; ++_ < p.attrs.length;) {
		let e = p.attrs[_], c = (e.prefix ? e.prefix + ":" : "") + e.name;
		own.call(proto, c) || (v[c] = e.value);
	}
	let y = (e.schema.space === "svg" ? s : h)(p.tagName, v, all(e, p.childNodes));
	if (patch(e, p, y), y.tagName === "template") {
		let c = p, l = c.sourceCodeLocation, u = l && l.startTag && position(l.startTag), d = l && l.endTag && position(l.endTag), f = one(e, c.content);
		u && d && e.file && (f.position = {
			start: u.end,
			end: d.start
		}), y.content = f;
	}
	return e.schema = m, y;
}
function patch(e, c, l) {
	if ("sourceCodeLocation" in c && c.sourceCodeLocation && e.file) {
		let u = createLocation(e, l, c.sourceCodeLocation);
		u && (e.location = !0, l.position = u);
	}
}
function createLocation(c, l, u) {
	let d = position(u);
	if (l.type === "element") {
		let f = l.children[l.children.length - 1];
		if (d && !u.endTag && f && f.position && f.position.end && (d.end = Object.assign({}, f.position.end)), c.verbose) {
			let d = {}, f;
			if (u.attrs) for (f in u.attrs) own.call(u.attrs, f) && (d[find(c.schema, f).property] = position(u.attrs[f]));
			u.startTag;
			let p = position(u.startTag), m = u.endTag ? position(u.endTag) : void 0, g = { opening: p };
			m && (g.closing = m), g.properties = d, l.data = { position: g };
		}
	}
	return d;
}
function position(e) {
	let c = point({
		line: e.startLine,
		column: e.startCol,
		offset: e.startOffset
	}), l = point({
		line: e.endLine,
		column: e.endCol,
		offset: e.endOffset
	});
	return c || l ? {
		start: c,
		end: l
	} : void 0;
}
function point(e) {
	return e.line && e.column ? e : void 0;
}
export { fromParse5 };
