import { SKIP as e } from "../../unist-util-visit-parents/lib/index.js";
import { visit as t } from "../../unist-util-visit/lib/index.js";
//#region ../../node_modules/rehype-harden/dist/index.js
var n = {
	indicator: "indicator",
	textOnly: "text-only",
	remove: "remove"
};
function r({ defaultOrigin: e = "", allowedLinkPrefixes: r = [], allowedImagePrefixes: i = [], allowDataImages: a = !1, allowedProtocols: o = [], blockedImageClass: s = "inline-block bg-gray-200 dark:bg-gray-700 text-gray-600 dark:text-gray-400 px-3 py-1 rounded text-sm", blockedLinkClass: c = "text-gray-500", linkBlockPolicy: u = n.indicator, imageBlockPolicy: d = n.indicator }) {
	let f = r.length && !r.every((e) => e === "*"), m = i.length && !i.every((e) => e === "*");
	if (!e && (f || m)) throw Error("defaultOrigin is required when allowedLinkPrefixes or allowedImagePrefixes are provided");
	return (n) => {
		let f = p(e, r, i, a, o, s, c, u, d);
		l(n), t(n, f);
	};
}
function i(e, t) {
	if (typeof e != "string") return null;
	try {
		return new URL(e);
	} catch {
		if (t) try {
			return new URL(e, t);
		} catch {
			return null;
		}
		if (e.startsWith("/") || e.startsWith("./") || e.startsWith("../")) try {
			return new URL(e, "http://example.com");
		} catch {
			return null;
		}
		return null;
	}
}
function a(e) {
	return typeof e == "string" ? e.startsWith("/") || e.startsWith("./") || e.startsWith("../") : !1;
}
var o = new Set([
	"https:",
	"http:",
	"irc:",
	"ircs:",
	"mailto:",
	"xmpp:",
	"blob:"
]), s = new Set([
	"javascript:",
	"data:",
	"file:",
	"vbscript:"
]);
function c(e, t, n, r = !1, c = !1, l = []) {
	if (!e) return null;
	if (typeof e == "string" && e.startsWith("#") && !c) try {
		if (new URL(e, "http://example.com").hash === e) return e;
	} catch {}
	if (typeof e == "string" && e.startsWith("data:")) return c && r && e.startsWith("data:image/") ? e : null;
	if (typeof e == "string" && e.startsWith("blob:")) {
		try {
			if (new URL(e).protocol === "blob:" && e.length > 5) {
				let t = e.substring(5);
				if (t && t.length > 0 && t !== "invalid") return e;
			}
		} catch {
			return null;
		}
		return null;
	}
	let u = i(e, n);
	if (!u || s.has(u.protocol) || !(o.has(u.protocol) || l.includes(u.protocol) || l.includes("*"))) return null;
	if (u.protocol === "mailto:" || !u.protocol.match(/^https?:$/)) return u.href;
	let d = a(e);
	return u && t.some((e) => {
		let t = i(e, n);
		return !t || t.origin !== u.origin ? !1 : u.href.startsWith(t.href);
	}) ? d ? u.pathname + u.search + u.hash : u.href : t.includes("*") ? u.protocol !== "https:" && u.protocol !== "http:" ? null : d ? u.pathname + u.search + u.hash : u.href : null;
}
function l(e) {
	if ("children" in e && Array.isArray(e.children)) {
		e.children = e.children.filter((e) => e != null);
		for (let t of e.children) l(t);
	}
}
var u = Symbol("node-seen");
function d(e, t, r) {
	return t === n.remove ? { type: "remove" } : t === n.textOnly ? {
		type: "replace",
		element: {
			type: "element",
			tagName: "span",
			properties: {},
			children: [...e.children]
		}
	} : {
		type: "replace",
		element: {
			type: "element",
			tagName: "span",
			properties: {
				title: "Blocked URL: " + String(e.properties.href),
				class: r
			},
			children: [...e.children, {
				type: "text",
				value: " [blocked]"
			}]
		}
	};
}
function f(e, t, r) {
	if (t === n.remove) return { type: "remove" };
	if (t === n.textOnly) {
		let t = String(e.properties.alt || "");
		return t ? {
			type: "replace",
			element: {
				type: "element",
				tagName: "span",
				properties: {},
				children: [{
					type: "text",
					value: t
				}]
			}
		} : { type: "remove" };
	}
	return {
		type: "replace",
		element: {
			type: "element",
			tagName: "span",
			properties: { class: r },
			children: [{
				type: "text",
				value: "[Image blocked: " + String(e.properties.alt || "No description") + "]"
			}]
		}
	};
}
var p = (n, r, i, a, o, s, l, p, m) => {
	let h = (g, _, v) => {
		if (g.type !== "element" || g[u]) return !0;
		if (g.tagName === "a") {
			let i = c(g.properties.href, r, n, !1, !1, o);
			if (i === null) {
				if (g[u] = !0, t(g, h), v && typeof _ == "number") {
					let t = d(g, p, l);
					if (t.type === "remove") return v.children.splice(_, 1), [e, _];
					v.children[_] = t.element;
				}
				return e;
			} else return g.properties.href = i, g.properties.target = "_blank", g.properties.rel = "noopener noreferrer", !0;
		}
		if (g.tagName === "img") {
			let r = c(g.properties.src, i, n, a, !0, o);
			if (r === null) {
				if (g[u] = !0, t(g, h), v && typeof _ == "number") {
					let t = f(g, m, s);
					if (t.type === "remove") return v.children.splice(_, 1), [e, _];
					v.children[_] = t.element;
				}
				return e;
			} else return g.properties.src = r, !0;
		}
		return !0;
	};
	return h;
};
//#endregion
export { r as harden };
