import { asciiAlpha, asciiAlphanumeric, asciiControl, markdownLineEndingOrSpace, unicodePunctuation, unicodeWhitespace } from "../../micromark-util-character/index.js";
var wwwPrefix = {
	tokenize: tokenizeWwwPrefix,
	partial: !0
}, domain = {
	tokenize: tokenizeDomain,
	partial: !0
}, path = {
	tokenize: tokenizePath,
	partial: !0
}, trail = {
	tokenize: tokenizeTrail,
	partial: !0
}, emailDomainDotTrail = {
	tokenize: tokenizeEmailDomainDotTrail,
	partial: !0
}, wwwAutolink = {
	name: "wwwAutolink",
	tokenize: tokenizeWwwAutolink,
	previous: previousWww
}, protocolAutolink = {
	name: "protocolAutolink",
	tokenize: tokenizeProtocolAutolink,
	previous: previousProtocol
}, emailAutolink = {
	name: "emailAutolink",
	tokenize: tokenizeEmailAutolink,
	previous: previousEmail
}, text = {};
function gfmAutolinkLiteral() {
	return { text };
}
for (var code = 48; code < 123;) text[code] = emailAutolink, code++, code === 58 ? code = 65 : code === 91 && (code = 97);
text[43] = emailAutolink, text[45] = emailAutolink, text[46] = emailAutolink, text[95] = emailAutolink, text[72] = [emailAutolink, protocolAutolink], text[104] = [emailAutolink, protocolAutolink], text[87] = [emailAutolink, wwwAutolink], text[119] = [emailAutolink, wwwAutolink];
function tokenizeEmailAutolink(m, h, g) {
	let _ = this, v, y;
	return b;
	function b(e) {
		return !gfmAtext(e) || !previousEmail.call(_, _.previous) || previousUnbalanced(_.events) ? g(e) : (m.enter("literalAutolink"), m.enter("literalAutolinkEmail"), x(e));
	}
	function x(e) {
		return gfmAtext(e) ? (m.consume(e), x) : e === 64 ? (m.consume(e), C) : g(e);
	}
	function C(e) {
		return e === 46 ? m.check(emailDomainDotTrail, T, w)(e) : e === 45 || e === 95 || asciiAlphanumeric(e) ? (y = !0, m.consume(e), C) : T(e);
	}
	function w(e) {
		return m.consume(e), v = !0, C;
	}
	function T(p) {
		return y && v && asciiAlpha(_.previous) ? (m.exit("literalAutolinkEmail"), m.exit("literalAutolink"), h(p)) : g(p);
	}
}
function tokenizeWwwAutolink(e, p, m) {
	let h = this;
	return g;
	function g(p) {
		return p !== 87 && p !== 119 || !previousWww.call(h, h.previous) || previousUnbalanced(h.events) ? m(p) : (e.enter("literalAutolink"), e.enter("literalAutolinkWww"), e.check(wwwPrefix, e.attempt(domain, e.attempt(path, _), m), m)(p));
	}
	function _(m) {
		return e.exit("literalAutolinkWww"), e.exit("literalAutolink"), p(m);
	}
}
function tokenizeProtocolAutolink(p, v, x) {
	let S = this, C = "", w = !1;
	return T;
	function T(e) {
		return (e === 72 || e === 104) && previousProtocol.call(S, S.previous) && !previousUnbalanced(S.events) ? (p.enter("literalAutolink"), p.enter("literalAutolinkHttp"), C += String.fromCodePoint(e), p.consume(e), E) : x(e);
	}
	function E(m) {
		if (asciiAlpha(m) && C.length < 5) return C += String.fromCodePoint(m), p.consume(m), E;
		if (m === 58) {
			let e = C.toLowerCase();
			if (e === "http" || e === "https") return p.consume(m), D;
		}
		return x(m);
	}
	function D(e) {
		return e === 47 ? (p.consume(e), w ? O : (w = !0, D)) : x(e);
	}
	function O(e) {
		return e === null || asciiControl(e) || markdownLineEndingOrSpace(e) || unicodeWhitespace(e) || unicodePunctuation(e) ? x(e) : p.attempt(domain, p.attempt(path, k), x)(e);
	}
	function k(e) {
		return p.exit("literalAutolinkHttp"), p.exit("literalAutolink"), v(e);
	}
}
function tokenizeWwwPrefix(e, p, m) {
	let h = 0;
	return g;
	function g(p) {
		return (p === 87 || p === 119) && h < 3 ? (h++, e.consume(p), g) : p === 46 && h === 3 ? (e.consume(p), _) : m(p);
	}
	function _(e) {
		return e === null ? m(e) : p(e);
	}
}
function tokenizeDomain(e, p, m) {
	let v, y, b;
	return S;
	function S(p) {
		return p === 46 || p === 95 ? e.check(trail, w, C)(p) : p === null || markdownLineEndingOrSpace(p) || unicodeWhitespace(p) || p !== 45 && unicodePunctuation(p) ? w(p) : (b = !0, e.consume(p), S);
	}
	function C(p) {
		return p === 95 ? v = !0 : (y = v, v = void 0), e.consume(p), S;
	}
	function w(e) {
		return y || v || !b ? m(e) : p(e);
	}
}
function tokenizePath(e, p) {
	let m = 0, g = 0;
	return v;
	function v(b) {
		return b === 40 ? (m++, e.consume(b), v) : b === 41 && g < m ? y(b) : b === 33 || b === 34 || b === 38 || b === 39 || b === 41 || b === 42 || b === 44 || b === 46 || b === 58 || b === 59 || b === 60 || b === 63 || b === 93 || b === 95 || b === 126 ? e.check(trail, p, y)(b) : b === null || markdownLineEndingOrSpace(b) || unicodeWhitespace(b) ? p(b) : (e.consume(b), v);
	}
	function y(p) {
		return p === 41 && g++, e.consume(p), v;
	}
}
function tokenizeTrail(p, m, g) {
	return v;
	function v(e) {
		return e === 33 || e === 34 || e === 39 || e === 41 || e === 42 || e === 44 || e === 46 || e === 58 || e === 59 || e === 63 || e === 95 || e === 126 ? (p.consume(e), v) : e === 38 ? (p.consume(e), b) : e === 93 ? (p.consume(e), y) : e === 60 || e === null || markdownLineEndingOrSpace(e) || unicodeWhitespace(e) ? m(e) : g(e);
	}
	function y(e) {
		return e === null || e === 40 || e === 91 || markdownLineEndingOrSpace(e) || unicodeWhitespace(e) ? m(e) : v(e);
	}
	function b(p) {
		return asciiAlpha(p) ? x(p) : g(p);
	}
	function x(m) {
		return m === 59 ? (p.consume(m), v) : asciiAlpha(m) ? (p.consume(m), x) : g(m);
	}
}
function tokenizeEmailDomainDotTrail(e, m, h) {
	return g;
	function g(p) {
		return e.consume(p), _;
	}
	function _(e) {
		return asciiAlphanumeric(e) ? h(e) : m(e);
	}
}
function previousWww(e) {
	return e === null || e === 40 || e === 42 || e === 95 || e === 91 || e === 93 || e === 126 || markdownLineEndingOrSpace(e);
}
function previousProtocol(p) {
	return !asciiAlpha(p);
}
function previousEmail(e) {
	return !(e === 47 || gfmAtext(e));
}
function gfmAtext(e) {
	return e === 43 || e === 45 || e === 46 || e === 95 || asciiAlphanumeric(e);
}
function previousUnbalanced(e) {
	let p = e.length, m = !1;
	for (; p--;) {
		let h = e[p][1];
		if ((h.type === "labelLink" || h.type === "labelImage") && !h._balanced) {
			m = !0;
			break;
		}
		if (h._gfmAutolinkLiteralWalkedInto) {
			m = !1;
			break;
		}
	}
	return e.length > 0 && !m && (e[e.length - 1][1]._gfmAutolinkLiteralWalkedInto = !0), m;
}
export { gfmAutolinkLiteral };
