function h(n, r) {
  const e = n.pos, t = n.src.charCodeAt(e);
  if (r || t !== 126)
    return !1;
  const i = n.scanDelims(n.pos, !0);
  let s = i.length;
  const o = String.fromCharCode(t);
  if (s < 2)
    return !1;
  let c;
  s % 2 && (c = n.push("text", "", 0), c.content = o, s--);
  for (let k = 0; k < s; k += 2)
    c = n.push("text", "", 0), c.content = o + o, n.delimiters.push({
      marker: t,
      length: 0,
      // disable "rule of 3" length checks meant for emphasis
      token: n.tokens.length - 1,
      end: -1,
      open: i.can_open,
      close: i.can_close
    });
  return n.pos += i.length, !0;
}
function l(n, r) {
  let e;
  const t = [], i = r.length;
  for (let s = 0; s < i; s++) {
    const o = r[s];
    if (o.marker !== 126 || o.end === -1)
      continue;
    const c = r[o.end];
    e = n.tokens[o.token], e.type = "s_open", e.tag = "s", e.nesting = 1, e.markup = "~~", e.content = "", e = n.tokens[c.token], e.type = "s_close", e.tag = "s", e.nesting = -1, e.markup = "~~", e.content = "", n.tokens[c.token - 1].type === "text" && n.tokens[c.token - 1].content === "~" && t.push(c.token - 1);
  }
  for (; t.length; ) {
    const s = t.pop();
    let o = s + 1;
    for (; o < n.tokens.length && n.tokens[o].type === "s_close"; )
      o++;
    o--, s !== o && (e = n.tokens[o], n.tokens[o] = n.tokens[s], n.tokens[s] = e);
  }
}
function p(n) {
  const r = n.tokens_meta, e = n.tokens_meta.length;
  l(n, n.delimiters);
  for (let t = 0; t < e; t++)
    r[t] && r[t].delimiters && l(n, r[t].delimiters);
}
const u = {
  tokenize: h,
  postProcess: p
};
export {
  u as default
};
