import { find } from "../../../property-information/lib/find.js";
import { 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 { ccount } from "../../../ccount/index.js";
import { stringifyEntities } from "../../../stringify-entities/lib/index.js";
import { closing } from "../omission/closing.js";
import { opening } from "../omission/opening.js";
var constants = {
	name: [["	\n\f\r &/=>".split(""), "	\n\f\r \"&'/=>`".split("")], ["\0	\n\f\r \"&'/<=>".split(""), "\0	\n\f\r \"&'/<=>`".split("")]],
	unquoted: [["	\n\f\r &>".split(""), "\0	\n\f\r \"&'<=>`".split("")], ["\0	\n\f\r \"&'<=>`".split(""), "\0	\n\f\r \"&'<=>`".split("")]],
	single: [["&'".split(""), "\"&'`".split("")], ["\0&'".split(""), "\0\"&'`".split("")]],
	double: [["\"&".split(""), "\"&'`".split("")], ["\0\"&".split(""), "\0\"&'`".split("")]]
};
function element(e, n, r, i) {
	let a = i.schema, c = a.space === "svg" ? !1 : i.settings.omitOptionalTags, l = a.space === "svg" ? i.settings.closeEmptyElements : i.settings.voids.includes(e.tagName.toLowerCase()), d = [], f;
	a.space === "html" && e.tagName === "svg" && (i.schema = svg);
	let p = serializeAttributes(i, e.properties), m = i.all(a.space === "html" && e.tagName === "template" ? e.content : e);
	return i.schema = a, m && (l = !1), (p || !c || !opening(e, n, r)) && (d.push("<", e.tagName, p ? " " + p : ""), l && (a.space === "svg" || i.settings.closeSelfClosing) && (f = p.charAt(p.length - 1), (!i.settings.tightSelfClosing || f === "/" || f && f !== "\"" && f !== "'") && d.push(" "), d.push("/")), d.push(">")), d.push(m), !l && (!c || !closing(e, n, r)) && d.push("</" + e.tagName + ">"), d.join("");
}
function serializeAttributes(e, t) {
	let n = [], r = -1, i;
	if (t) {
		for (i in t) if (t[i] !== null && t[i] !== void 0) {
			let r = serializeAttribute(e, i, t[i]);
			r && n.push(r);
		}
	}
	for (; ++r < n.length;) {
		let t = e.settings.tightAttributes ? n[r].charAt(n[r].length - 1) : void 0;
		r !== n.length - 1 && t !== "\"" && t !== "'" && (n[r] += " ");
	}
	return n.join("");
}
function serializeAttribute(t, o, s) {
	let l = find(t.schema, o), u = t.settings.allowParseErrors && t.schema.space === "html" ? 0 : 1, d = t.settings.allowDangerousCharacters ? 0 : 1, f = t.quote, p;
	if (l.overloadedBoolean && (s === l.attribute || s === "") ? s = !0 : (l.boolean || l.overloadedBoolean) && (typeof s != "string" || s === l.attribute || s === "") && (s = !!s), s == null || s === !1 || typeof s == "number" && Number.isNaN(s)) return "";
	let m = stringifyEntities(l.attribute, Object.assign({}, t.settings.characterReferences, { subset: constants.name[u][d] }));
	return s === !0 || (s = Array.isArray(s) ? (l.commaSeparated ? stringify : stringify$1)(s, { padLeft: !t.settings.tightCommaSeparatedLists }) : String(s), t.settings.collapseEmptyAttributes && !s) ? m : (t.settings.preferUnquoted && (p = stringifyEntities(s, Object.assign({}, t.settings.characterReferences, {
		attribute: !0,
		subset: constants.unquoted[u][d]
	}))), p !== s && (t.settings.quoteSmart && ccount(s, f) > ccount(s, t.alternative) && (f = t.alternative), p = f + stringifyEntities(s, Object.assign({}, t.settings.characterReferences, {
		subset: (f === "'" ? constants.single : constants.double)[u][d],
		attribute: !0
	})) + f), m + (p && "=" + p));
}
export { element };
