import { ccount as f } from "../../../../ccount@2.0.1/node_modules/ccount/index.es.js";
import { ok as k } from "../../../../devlop@1.1.0/node_modules/devlop/lib/default.es.js";
import { unicodeWhitespace as p, unicodePunctuation as d } from "../../../../micromark-util-character@2.1.1/node_modules/micromark-util-character/index.es.js";
import { findAndReplace as g } from "../../../../mdast-util-find-and-replace@3.0.2/node_modules/mdast-util-find-and-replace/lib/index.es.js";
const a = "phrasing", u = ["autolink", "link", "image", "label"];
function b() {
  return {
    transforms: [y],
    enter: {
      literalAutolink: w,
      literalAutolinkEmail: s,
      literalAutolinkHttp: s,
      literalAutolinkWww: s
    },
    exit: {
      literalAutolink: L,
      literalAutolinkEmail: m,
      literalAutolinkHttp: x,
      literalAutolinkWww: A
    }
  };
}
function I() {
  return {
    unsafe: [
      {
        character: "@",
        before: "[+\\-.\\w]",
        after: "[\\-.\\w]",
        inConstruct: a,
        notInConstruct: u
      },
      {
        character: ".",
        before: "[Ww]",
        after: "[\\-.\\w]",
        inConstruct: a,
        notInConstruct: u
      },
      {
        character: ":",
        before: "[ps]",
        after: "\\/",
        inConstruct: a,
        notInConstruct: u
      }
    ]
  };
}
function w(t) {
  this.enter({ type: "link", title: null, url: "", children: [] }, t);
}
function s(t) {
  this.config.enter.autolinkProtocol.call(this, t);
}
function x(t) {
  this.config.exit.autolinkProtocol.call(this, t);
}
function A(t) {
  this.config.exit.data.call(this, t);
  const e = this.stack[this.stack.length - 1];
  k(e.type === "link"), e.url = "http://" + this.sliceSerialize(t);
}
function m(t) {
  this.config.exit.autolinkEmail.call(this, t);
}
function L(t) {
  this.exit(t);
}
function y(t) {
  g(
    t,
    [
      [/(https?:\/\/|www(?=\.))([-.\w]+)([^ \t\r\n]*)/gi, P],
      [new RegExp("(?<=^|\\s|\\p{P}|\\p{S})([-.\\w+]+)@([-\\w]+(?:\\.[-\\w]+)+)", "gu"), E]
    ],
    { ignore: ["link", "linkReference"] }
  );
}
function P(t, e, i, n, o) {
  let l = "";
  if (!h(o) || (/^w/i.test(e) && (i = e + i, e = "", l = "http://"), !W(i)))
    return !1;
  const r = _(i + n);
  if (!r[0]) return !1;
  const c = {
    type: "link",
    title: null,
    url: l + e + r[0],
    children: [{ type: "text", value: e + r[0] }]
  };
  return r[1] ? [c, { type: "text", value: r[1] }] : c;
}
function E(t, e, i, n) {
  return (
    // Not an expected previous character.
    !h(n, !0) || // Label ends in not allowed character.
    /[-\d_]$/.test(i) ? !1 : {
      type: "link",
      title: null,
      url: "mailto:" + e + "@" + i,
      children: [{ type: "text", value: e + "@" + i }]
    }
  );
}
function W(t) {
  const e = t.split(".");
  return !(e.length < 2 || e[e.length - 1] && (/_/.test(e[e.length - 1]) || !/[a-zA-Z\d]/.test(e[e.length - 1])) || e[e.length - 2] && (/_/.test(e[e.length - 2]) || !/[a-zA-Z\d]/.test(e[e.length - 2])));
}
function _(t) {
  const e = /[!"&'),.:;<>?\]}]+$/.exec(t);
  if (!e)
    return [t, void 0];
  t = t.slice(0, e.index);
  let i = e[0], n = i.indexOf(")");
  const o = f(t, "(");
  let l = f(t, ")");
  for (; n !== -1 && o > l; )
    t += i.slice(0, n + 1), i = i.slice(n + 1), n = i.indexOf(")"), l++;
  return [t, i];
}
function h(t, e) {
  const i = t.input.charCodeAt(t.index - 1);
  return (t.index === 0 || p(i) || d(i)) && // If it’s an email, the previous character should not be a slash.
  (!e || i !== 47);
}
export {
  b as gfmAutolinkLiteralFromMarkdown,
  I as gfmAutolinkLiteralToMarkdown
};
//# sourceMappingURL=index.es.js.map
