import { asciiAlpha, asciiAlphanumeric, markdownLineEnding, markdownLineEndingOrSpace, markdownSpace } from "../../micromark-util-character/index.js";
import { blankLine } from "./blank-line.js";
import { htmlBlockNames, htmlRawNames } from "../../micromark-util-html-tag-name/index.js";
const htmlFlow = {
	concrete: !0,
	name: "htmlFlow",
	resolveTo: resolveToHtmlFlow,
	tokenize: tokenizeHtmlFlow
};
var blankLineBefore = {
	partial: !0,
	tokenize: tokenizeBlankLineBefore
}, nonLazyContinuationStart = {
	partial: !0,
	tokenize: tokenizeNonLazyContinuationStart
};
function resolveToHtmlFlow(e) {
	let i = e.length;
	for (; i-- && !(e[i][0] === "enter" && e[i][1].type === "htmlFlow"););
	return i > 1 && e[i - 2][1].type === "linePrefix" && (e[i][1].start = e[i - 2][1].start, e[i + 1][1].start = e[i - 2][1].start, e.splice(i - 2, 2)), e;
}
function tokenizeHtmlFlow(c, u, d) {
	let f = this, p, m, h, g, _;
	return v;
	function v(e) {
		return y(e);
	}
	function y(e) {
		return c.enter("htmlFlow"), c.enter("htmlFlowData"), c.consume(e), b;
	}
	function b(i) {
		return i === 33 ? (c.consume(i), x) : i === 47 ? (c.consume(i), m = !0, w) : i === 63 ? (c.consume(i), p = 3, f.interrupt ? u : G) : asciiAlpha(i) ? (c.consume(i), h = String.fromCharCode(i), T) : d(i);
	}
	function x(i) {
		return i === 45 ? (c.consume(i), p = 2, S) : i === 91 ? (c.consume(i), p = 5, g = 0, C) : asciiAlpha(i) ? (c.consume(i), p = 4, f.interrupt ? u : G) : d(i);
	}
	function S(e) {
		return e === 45 ? (c.consume(e), f.interrupt ? u : G) : d(e);
	}
	function C(e) {
		return e === "CDATA[".charCodeAt(g++) ? (c.consume(e), g === 6 ? f.interrupt ? u : L : C) : d(e);
	}
	function w(i) {
		return asciiAlpha(i) ? (c.consume(i), h = String.fromCharCode(i), T) : d(i);
	}
	function T(e) {
		if (e === null || e === 47 || e === 62 || markdownLineEndingOrSpace(e)) {
			let i = e === 47, a = h.toLowerCase();
			return !i && !m && htmlRawNames.includes(a) ? (p = 1, f.interrupt ? u(e) : L(e)) : htmlBlockNames.includes(h.toLowerCase()) ? (p = 6, i ? (c.consume(e), E) : f.interrupt ? u(e) : L(e)) : (p = 7, f.interrupt && !f.parser.lazy[f.now().line] ? d(e) : m ? D(e) : O(e));
		}
		return e === 45 || asciiAlphanumeric(e) ? (c.consume(e), h += String.fromCharCode(e), T) : d(e);
	}
	function E(e) {
		return e === 62 ? (c.consume(e), f.interrupt ? u : L) : d(e);
	}
	function D(e) {
		return markdownSpace(e) ? (c.consume(e), D) : F(e);
	}
	function O(i) {
		return i === 47 ? (c.consume(i), F) : i === 58 || i === 95 || asciiAlpha(i) ? (c.consume(i), k) : markdownSpace(i) ? (c.consume(i), O) : F(i);
	}
	function k(e) {
		return e === 45 || e === 46 || e === 58 || e === 95 || asciiAlphanumeric(e) ? (c.consume(e), k) : A(e);
	}
	function A(e) {
		return e === 61 ? (c.consume(e), j) : markdownSpace(e) ? (c.consume(e), A) : O(e);
	}
	function j(e) {
		return e === null || e === 60 || e === 61 || e === 62 || e === 96 ? d(e) : e === 34 || e === 39 ? (c.consume(e), _ = e, M) : markdownSpace(e) ? (c.consume(e), j) : N(e);
	}
	function M(e) {
		return e === _ ? (c.consume(e), _ = null, P) : e === null || markdownLineEnding(e) ? d(e) : (c.consume(e), M);
	}
	function N(e) {
		return e === null || e === 34 || e === 39 || e === 47 || e === 60 || e === 61 || e === 62 || e === 96 || markdownLineEndingOrSpace(e) ? A(e) : (c.consume(e), N);
	}
	function P(e) {
		return e === 47 || e === 62 || markdownSpace(e) ? O(e) : d(e);
	}
	function F(e) {
		return e === 62 ? (c.consume(e), I) : d(e);
	}
	function I(e) {
		return e === null || markdownLineEnding(e) ? L(e) : markdownSpace(e) ? (c.consume(e), I) : d(e);
	}
	function L(e) {
		return e === 45 && p === 2 ? (c.consume(e), V) : e === 60 && p === 1 ? (c.consume(e), H) : e === 62 && p === 4 ? (c.consume(e), K) : e === 63 && p === 3 ? (c.consume(e), G) : e === 93 && p === 5 ? (c.consume(e), W) : markdownLineEnding(e) && (p === 6 || p === 7) ? (c.exit("htmlFlowData"), c.check(blankLineBefore, q, R)(e)) : e === null || markdownLineEnding(e) ? (c.exit("htmlFlowData"), R(e)) : (c.consume(e), L);
	}
	function R(e) {
		return c.check(nonLazyContinuationStart, z, q)(e);
	}
	function z(e) {
		return c.enter("lineEnding"), c.consume(e), c.exit("lineEnding"), B;
	}
	function B(e) {
		return e === null || markdownLineEnding(e) ? R(e) : (c.enter("htmlFlowData"), L(e));
	}
	function V(e) {
		return e === 45 ? (c.consume(e), G) : L(e);
	}
	function H(e) {
		return e === 47 ? (c.consume(e), h = "", U) : L(e);
	}
	function U(i) {
		if (i === 62) {
			let e = h.toLowerCase();
			return htmlRawNames.includes(e) ? (c.consume(i), K) : L(i);
		}
		return asciiAlpha(i) && h.length < 8 ? (c.consume(i), h += String.fromCharCode(i), U) : L(i);
	}
	function W(e) {
		return e === 93 ? (c.consume(e), G) : L(e);
	}
	function G(e) {
		return e === 62 ? (c.consume(e), K) : e === 45 && p === 2 ? (c.consume(e), G) : L(e);
	}
	function K(e) {
		return e === null || markdownLineEnding(e) ? (c.exit("htmlFlowData"), q(e)) : (c.consume(e), K);
	}
	function q(e) {
		return c.exit("htmlFlow"), u(e);
	}
}
function tokenizeNonLazyContinuationStart(e, i, o) {
	let s = this;
	return c;
	function c(i) {
		return markdownLineEnding(i) ? (e.enter("lineEnding"), e.consume(i), e.exit("lineEnding"), l) : o(i);
	}
	function l(e) {
		return s.parser.lazy[s.now().line] ? o(e) : i(e);
	}
}
function tokenizeBlankLineBefore(e, i, a) {
	return o;
	function o(o) {
		return e.enter("lineEnding"), e.consume(o), e.exit("lineEnding"), e.attempt(blankLine, i, a);
	}
}
export { htmlFlow };
