import { ccount as e } from "../../ccount/index.js";
import "../../devlop/lib/default.js";
import { unicodePunctuation as t, unicodeWhitespace as n } from "../../micromark-util-character/index.js";
import { findAndReplace as r } from "../../mdast-util-find-and-replace/lib/index.js";
//#region ../../node_modules/mdast-util-gfm-autolink-literal/lib/index.js
var i = "phrasing", a = [
	"autolink",
	"link",
	"image",
	"label"
];
function o() {
	return {
		transforms: [m],
		enter: {
			literalAutolink: c,
			literalAutolinkEmail: l,
			literalAutolinkHttp: l,
			literalAutolinkWww: l
		},
		exit: {
			literalAutolink: p,
			literalAutolinkEmail: f,
			literalAutolinkHttp: u,
			literalAutolinkWww: d
		}
	};
}
function s() {
	return { unsafe: [
		{
			character: "@",
			before: "[+\\-.\\w]",
			after: "[\\-.\\w]",
			inConstruct: i,
			notInConstruct: a
		},
		{
			character: ".",
			before: "[Ww]",
			after: "[\\-.\\w]",
			inConstruct: i,
			notInConstruct: a
		},
		{
			character: ":",
			before: "[ps]",
			after: "\\/",
			inConstruct: i,
			notInConstruct: a
		}
	] };
}
function c(e) {
	this.enter({
		type: "link",
		title: null,
		url: "",
		children: []
	}, e);
}
function l(e) {
	this.config.enter.autolinkProtocol.call(this, e);
}
function u(e) {
	this.config.exit.autolinkProtocol.call(this, e);
}
function d(e) {
	this.config.exit.data.call(this, e);
	let t = this.stack[this.stack.length - 1];
	t.type, t.url = "http://" + this.sliceSerialize(e);
}
function f(e) {
	this.config.exit.autolinkEmail.call(this, e);
}
function p(e) {
	this.exit(e);
}
function m(e) {
	r(e, [[/(https?:\/\/|www(?=\.))([-.\w]+)([^ \t\r\n]*)/gi, h], [/(?<=^|\s|\p{P}|\p{S})([-.\w+]+)@([-\w]+(?:\.[-\w]+)+)/gu, g]], { ignore: ["link", "linkReference"] });
}
function h(e, t, n, r, i) {
	let a = "";
	if (!y(i) || (/^w/i.test(t) && (n = t + n, t = "", a = "http://"), !_(n))) return !1;
	let o = v(n + r);
	if (!o[0]) return !1;
	let s = {
		type: "link",
		title: null,
		url: a + t + o[0],
		children: [{
			type: "text",
			value: t + o[0]
		}]
	};
	return o[1] ? [s, {
		type: "text",
		value: o[1]
	}] : s;
}
function g(e, t, n, r) {
	return !y(r, !0) || /[-\d_]$/.test(n) ? !1 : {
		type: "link",
		title: null,
		url: "mailto:" + t + "@" + n,
		children: [{
			type: "text",
			value: t + "@" + n
		}]
	};
}
function _(e) {
	let t = e.split(".");
	return !(t.length < 2 || t[t.length - 1] && (/_/.test(t[t.length - 1]) || !/[a-zA-Z\d]/.test(t[t.length - 1])) || t[t.length - 2] && (/_/.test(t[t.length - 2]) || !/[a-zA-Z\d]/.test(t[t.length - 2])));
}
function v(t) {
	let n = /[!"&'),.:;<>?\]}]+$/.exec(t);
	if (!n) return [t, void 0];
	t = t.slice(0, n.index);
	let r = n[0], i = r.indexOf(")"), a = e(t, "("), o = e(t, ")");
	for (; i !== -1 && a > o;) t += r.slice(0, i + 1), r = r.slice(i + 1), i = r.indexOf(")"), o++;
	return [t, r];
}
function y(e, r) {
	let i = e.input.charCodeAt(e.index - 1);
	return (e.index === 0 || n(i) || t(i)) && (!r || i !== 47);
}
//#endregion
export { o as gfmAutolinkLiteralFromMarkdown, s as gfmAutolinkLiteralToMarkdown };
