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