import esm_default from "../../@ungap/structured-clone/esm/index.js";
import { position } from "../../unist-util-position/lib/index.js";
import { defaultSchema } from "./schema.js";
var own = {}.hasOwnProperty;
function sanitize(e, l) {
	let d = {
		type: "root",
		children: []
	}, f = transform({
		schema: l ? {
			...defaultSchema,
			...l
		} : defaultSchema,
		stack: []
	}, e);
	return f && (Array.isArray(f) ? f.length === 1 ? d = f[0] : d.children = f : d = f), d;
}
function transform(e, l) {
	if (l && typeof l == "object") {
		let u = l;
		switch (typeof u.type == "string" ? u.type : "") {
			case "comment": return comment(e, u);
			case "doctype": return doctype(e, u);
			case "element": return element(e, u);
			case "root": return root(e, u);
			case "text": return text(e, u);
			default:
		}
	}
}
function comment(e, l) {
	if (e.schema.allowComments) {
		let e = typeof l.value == "string" ? l.value : "", u = e.indexOf("-->"), d = {
			type: "comment",
			value: u < 0 ? e : e.slice(0, u)
		};
		return patch(d, l), d;
	}
}
function doctype(e, l) {
	if (e.schema.allowDoctypes) {
		let e = { type: "doctype" };
		return patch(e, l), e;
	}
}
function element(e, l) {
	let u = typeof l.tagName == "string" ? l.tagName : "";
	e.stack.push(u);
	let f = children(e, l.children), p = properties(e, l.properties);
	e.stack.pop();
	let m = !1;
	if (u && u !== "*" && (!e.schema.tagNames || e.schema.tagNames.includes(u)) && (m = !0, e.schema.ancestors && own.call(e.schema.ancestors, u))) {
		let l = e.schema.ancestors[u], d = -1;
		for (m = !1; ++d < l.length;) e.stack.includes(l[d]) && (m = !0);
	}
	if (!m) return e.schema.strip && !e.schema.strip.includes(u) ? f : void 0;
	let h = {
		type: "element",
		tagName: u,
		properties: p,
		children: f
	};
	return patch(h, l), h;
}
function root(e, l) {
	let u = {
		type: "root",
		children: children(e, l.children)
	};
	return patch(u, l), u;
}
function text(e, l) {
	let u = {
		type: "text",
		value: typeof l.value == "string" ? l.value : ""
	};
	return patch(u, l), u;
}
function children(e, l) {
	let u = [];
	if (Array.isArray(l)) {
		let d = l, f = -1;
		for (; ++f < d.length;) {
			let l = transform(e, d[f]);
			l && (Array.isArray(l) ? u.push(...l) : u.push(l));
		}
	}
	return u;
}
function properties(e, l) {
	let u = e.stack[e.stack.length - 1], f = e.schema.attributes, p = e.schema.required, m = f && own.call(f, u) ? f[u] : void 0, h = f && own.call(f, "*") ? f["*"] : void 0, g = l && typeof l == "object" ? l : {}, _ = {}, v;
	for (v in g) if (own.call(g, v)) {
		let l = g[v], u = propertyValue(e, findDefinition(m, v), v, l);
		u ??= propertyValue(e, findDefinition(h, v), v, l), u != null && (_[v] = u);
	}
	if (p && own.call(p, u)) {
		let e = p[u];
		for (v in e) own.call(e, v) && !own.call(_, v) && (_[v] = e[v]);
	}
	return _;
}
function propertyValue(e, l, u, d) {
	return l ? Array.isArray(d) ? propertyValueMany(e, l, u, d) : propertyValuePrimitive(e, l, u, d) : void 0;
}
function propertyValueMany(e, l, u, d) {
	let f = -1, p = [];
	for (; ++f < d.length;) {
		let m = propertyValuePrimitive(e, l, u, d[f]);
		(typeof m == "number" || typeof m == "string") && p.push(m);
	}
	return p;
}
function propertyValuePrimitive(e, l, u, d) {
	if (!(typeof d != "boolean" && typeof d != "number" && typeof d != "string") && safeProtocol(e, u, d)) {
		if (typeof l == "object" && l.length > 1) {
			let e = !1, u = 0;
			for (; ++u < l.length;) {
				let f = l[u];
				if (f && typeof f == "object" && "flags" in f) {
					if (f.test(String(d))) {
						e = !0;
						break;
					}
				} else if (f === d) {
					e = !0;
					break;
				}
			}
			if (!e) return;
		}
		return e.schema.clobber && e.schema.clobberPrefix && e.schema.clobber.includes(u) ? e.schema.clobberPrefix + d : d;
	}
}
function safeProtocol(e, l, u) {
	let f = e.schema.protocols && own.call(e.schema.protocols, l) ? e.schema.protocols[l] : void 0;
	if (!f || f.length === 0) return !0;
	let p = String(u), m = p.indexOf(":"), h = p.indexOf("?"), g = p.indexOf("#"), _ = p.indexOf("/");
	if (m < 0 || _ > -1 && m > _ || h > -1 && m > h || g > -1 && m > g) return !0;
	let v = -1;
	for (; ++v < f.length;) {
		let e = f[v];
		if (m === e.length && p.slice(0, e.length) === e) return !0;
	}
	return !1;
}
function patch(u, d) {
	let f = position(d);
	d.data && (u.data = esm_default(d.data)), f && (u.position = f);
}
function findDefinition(e, l) {
	let u, d = -1;
	if (e) for (; ++d < e.length;) {
		let f = e[d], p = typeof f == "string" ? f : f[0];
		if (p === l) return f;
		p === "data*" && (u = f);
	}
	if (l.length > 4 && l.slice(0, 4).toLowerCase() === "data") return u;
}
export { sanitize };
