import { webNamespaces as e } from "../../web-namespaces/index.js";
import { zwitch as t } from "../../zwitch/index.js";
import { visit as n } from "../../unist-util-visit/lib/index.js";
import r from "../../@ungap/structured-clone/esm/index.js";
import { fromParse5 as i } from "../../hast-util-from-parse5/lib/index.js";
import { toParse5 as a } from "../../hast-util-to-parse5/lib/index.js";
import { htmlVoidElements as o } from "../../html-void-elements/index.js";
import { TokenType as s } from "../node_modules/parse5/dist/common/token.js";
import { getTagID as c } from "../node_modules/parse5/dist/common/html.js";
import { TokenizerMode as l } from "../node_modules/parse5/dist/tokenizer/index.js";
import { Parser as u } from "../node_modules/parse5/dist/parser/index.js";
import "../node_modules/parse5/dist/index.js";
import { pointEnd as d, pointStart as f } from "../../unist-util-position/lib/index.js";
//#region ../../node_modules/hast-util-raw/lib/index.js
var p = /<(\/?)(iframe|noembed|noframes|plaintext|script|style|textarea|title|xmp)(?=[\t\n\f\r />])/gi, m = new Set([
	"mdxFlowExpression",
	"mdxJsxFlowElement",
	"mdxJsxTextElement",
	"mdxTextExpression",
	"mdxjsEsm"
]), h = {
	sourceCodeLocationInfo: !0,
	scriptingEnabled: !1
};
function g(e, r) {
	let a = A(e), o = t("type", {
		handlers: {
			root: v,
			element: y,
			text: b,
			comment: C,
			doctype: x,
			raw: w
		},
		unknown: T
	}), s = {
		parser: a ? new u(h) : u.getFragmentParser(void 0, h),
		handle(e) {
			o(e, s);
		},
		stitches: !1,
		options: r || {}
	};
	o(e, s), E(s, f());
	let c = i(a ? s.parser.document : s.parser.getFragment(), { file: s.options.file });
	return s.stitches && n(c, "comment", function(e, t, n) {
		let r = e;
		if (r.value.stitch && n && t !== void 0) {
			let e = n.children;
			return e[t] = r.value.stitch, t;
		}
	}), c.type === "root" && c.children.length === 1 && c.children[0].type === e.type ? c.children[0] : c;
}
function _(e, t) {
	let n = -1;
	/* istanbul ignore else - invalid nodes, see rehypejs/rehype-raw#7. */
	if (e) for (; ++n < e.length;) t.handle(e[n]);
}
function v(e, t) {
	_(e.children, t);
}
function y(e, t) {
	O(e, t), _(e.children, t), k(e, t);
}
function b(e, t) {
	t.parser.tokenizer.state > 4 && (t.parser.tokenizer.state = 0);
	let n = {
		type: s.CHARACTER,
		chars: e.value,
		location: j(e)
	};
	E(t, f(e)), t.parser.currentToken = n, t.parser._processToken(t.parser.currentToken);
}
function x(e, t) {
	let n = {
		type: s.DOCTYPE,
		name: "html",
		forceQuirks: !1,
		publicId: "",
		systemId: "",
		location: j(e)
	};
	E(t, f(e)), t.parser.currentToken = n, t.parser._processToken(t.parser.currentToken);
}
function S(e, t) {
	t.stitches = !0;
	let n = M(e);
	"children" in e && "children" in n && (n.children = g({
		type: "root",
		children: e.children
	}, t.options).children), C({
		type: "comment",
		value: { stitch: n }
	}, t);
}
function C(e, t) {
	let n = e.value, r = {
		type: s.COMMENT,
		data: n,
		location: j(e)
	};
	E(t, f(e)), t.parser.currentToken = r, t.parser._processToken(t.parser.currentToken);
}
function w(e, t) {
	/* c8 ignore next 12 -- removed in <https://github.com/inikulin/parse5/pull/897> */
	if (t.parser.tokenizer.preprocessor.html = "", t.parser.tokenizer.preprocessor.pos = -1, t.parser.tokenizer.preprocessor.lastGapPos = -2, t.parser.tokenizer.preprocessor.gapStack = [], t.parser.tokenizer.preprocessor.skipNextNewLine = !1, t.parser.tokenizer.preprocessor.lastChunkWritten = !1, t.parser.tokenizer.preprocessor.endOfChunkHit = !1, t.parser.tokenizer.preprocessor.isEol = !1, D(t, f(e)), t.parser.tokenizer.write(t.options.tagfilter ? e.value.replace(p, "&lt;$1$2") : e.value, !1), t.parser.tokenizer._runParsingLoop(), t.parser.tokenizer.state === 72 || t.parser.tokenizer.state === 78) {
		t.parser.tokenizer.preprocessor.lastChunkWritten = !0;
		let e = t.parser.tokenizer._consume();
		t.parser.tokenizer._callState(e);
	}
}
function T(e, t) {
	let n = e;
	if (t.options.passThrough && t.options.passThrough.includes(n.type)) S(n, t);
	else {
		let e = "";
		throw m.has(n.type) && (e = ". It looks like you are using MDX nodes with `hast-util-raw` (or `rehype-raw`). If you use this because you are using remark or rehype plugins that inject `'html'` nodes, then please raise an issue with that plugin, as its a bad and slow idea. If you use this because you are using markdown syntax, then you have to configure this utility (or plugin) to pass through these nodes (see `passThrough` in docs), but you can also migrate to use the MDX syntax"), Error("Cannot compile `" + n.type + "` node" + e);
	}
}
function E(e, t) {
	D(e, t);
	let n = e.parser.tokenizer.currentCharacterToken;
	n && n.location && (n.location.endLine = e.parser.tokenizer.preprocessor.line, n.location.endCol = e.parser.tokenizer.preprocessor.col + 1, n.location.endOffset = e.parser.tokenizer.preprocessor.offset + 1, e.parser.currentToken = n, e.parser._processToken(e.parser.currentToken)), e.parser.tokenizer.paused = !1, e.parser.tokenizer.inLoop = !1, e.parser.tokenizer.active = !1, e.parser.tokenizer.returnState = l.DATA, e.parser.tokenizer.charRefCode = -1, e.parser.tokenizer.consumedAfterSnapshot = -1, e.parser.tokenizer.currentLocation = null, e.parser.tokenizer.currentCharacterToken = null, e.parser.tokenizer.currentToken = null, e.parser.tokenizer.currentAttr = {
		name: "",
		value: ""
	};
}
function D(e, t) {
	if (t && t.offset !== void 0) {
		let n = {
			startLine: t.line,
			startCol: t.column,
			startOffset: t.offset,
			endLine: -1,
			endCol: -1,
			endOffset: -1
		};
		e.parser.tokenizer.preprocessor.lineStartPos = -t.column + 1, e.parser.tokenizer.preprocessor.droppedBufferSize = t.offset, e.parser.tokenizer.preprocessor.line = t.line, e.parser.tokenizer.currentLocation = n;
	}
}
function O(t, n) {
	let r = t.tagName.toLowerCase();
	if (n.parser.tokenizer.state === l.PLAINTEXT) return;
	E(n, f(t));
	let i = n.parser.openElements.current, o = "namespaceURI" in i ? i.namespaceURI : e.html;
	o === e.html && r === "svg" && (o = e.svg);
	let u = a({
		...t,
		children: []
	}, { space: o === e.svg ? "svg" : "html" }), d = {
		type: s.START_TAG,
		tagName: r,
		tagID: c(r),
		selfClosing: !1,
		ackSelfClosing: !1,
		attrs: "attrs" in u ? u.attrs : [],
		location: j(t)
	};
	n.parser.currentToken = d, n.parser._processToken(n.parser.currentToken), n.parser.tokenizer.lastStartTagName = r;
}
function k(e, t) {
	let n = e.tagName.toLowerCase();
	if (!t.parser.tokenizer.inForeignNode && o.includes(n) || t.parser.tokenizer.state === l.PLAINTEXT) return;
	E(t, d(e));
	let r = {
		type: s.END_TAG,
		tagName: n,
		tagID: c(n),
		selfClosing: !1,
		ackSelfClosing: !1,
		attrs: [],
		location: j(e)
	};
	t.parser.currentToken = r, t.parser._processToken(t.parser.currentToken), n === t.parser.tokenizer.lastStartTagName && (t.parser.tokenizer.state === l.RCDATA || t.parser.tokenizer.state === l.RAWTEXT || t.parser.tokenizer.state === l.SCRIPT_DATA) && (t.parser.tokenizer.state = l.DATA);
}
function A(e) {
	let t = e.type === "root" ? e.children[0] : e;
	return !!(t && (t.type === "doctype" || t.type === "element" && t.tagName.toLowerCase() === "html"));
}
function j(e) {
	let t = f(e) || {
		line: void 0,
		column: void 0,
		offset: void 0
	}, n = d(e) || {
		line: void 0,
		column: void 0,
		offset: void 0
	};
	return {
		startLine: t.line,
		startCol: t.column,
		startOffset: t.offset,
		endLine: n.line,
		endCol: n.column,
		endOffset: n.offset
	};
}
function M(e) {
	return "children" in e ? r({
		...e,
		children: []
	}) : r(e);
}
//#endregion
export { g as raw };
