import c from "./ruler.js";
import k from "./rules_inline/state_inline.js";
import g from "./rules_inline/text.js";
import a from "./rules_inline/linkify.js";
import d from "./rules_inline/newline.js";
import w from "./rules_inline/escape.js";
import _ from "./rules_inline/backticks.js";
import u from "./rules_inline/strikethrough.js";
import h from "./rules_inline/emphasis.js";
import x from "./rules_inline/link.js";
import y from "./rules_inline/image.js";
import b from "./rules_inline/autolink.js";
import v from "./rules_inline/html_inline.js";
import P from "./rules_inline/entity.js";
import z from "./rules_inline/balance_pairs.js";
import N from "./rules_inline/fragments_join.js";
const m = [
  ["text", g],
  ["linkify", a],
  ["newline", d],
  ["escape", w],
  ["backticks", _],
  ["strikethrough", u.tokenize],
  ["emphasis", h.tokenize],
  ["link", x],
  ["image", y],
  ["autolink", b],
  ["html_inline", v],
  ["entity", P]
], f = [
  ["balance_pairs", z],
  ["strikethrough", u.postProcess],
  ["emphasis", h.postProcess],
  // rules for pairs separate '**' into its own text tokens, which may be left unused,
  // rule below merges unused segments back with the rest of the text
  ["fragments_join", N]
];
function p() {
  this.ruler = new c();
  for (let o = 0; o < m.length; o++)
    this.ruler.push(m[o][0], m[o][1]);
  this.ruler2 = new c();
  for (let o = 0; o < f.length; o++)
    this.ruler2.push(f[o][0], f[o][1]);
}
p.prototype.skipToken = function(o) {
  const i = o.pos, t = this.ruler.getRules(""), s = t.length, l = o.md.options.maxNesting, n = o.cache;
  if (typeof n[i] < "u") {
    o.pos = n[i];
    return;
  }
  let r = !1;
  if (o.level < l) {
    for (let e = 0; e < s; e++)
      if (o.level++, r = t[e](o, !0), o.level--, r) {
        if (i >= o.pos)
          throw new Error("inline rule didn't increment state.pos");
        break;
      }
  } else
    o.pos = o.posMax;
  r || o.pos++, n[i] = o.pos;
};
p.prototype.tokenize = function(o) {
  const i = this.ruler.getRules(""), t = i.length, s = o.posMax, l = o.md.options.maxNesting;
  for (; o.pos < s; ) {
    const n = o.pos;
    let r = !1;
    if (o.level < l) {
      for (let e = 0; e < t; e++)
        if (r = i[e](o, !1), r) {
          if (n >= o.pos)
            throw new Error("inline rule didn't increment state.pos");
          break;
        }
    }
    if (r) {
      if (o.pos >= s)
        break;
      continue;
    }
    o.pending += o.src[o.pos++];
  }
  o.pending && o.pushPending();
};
p.prototype.parse = function(o, i, t, s) {
  const l = new this.State(o, i, t, s);
  this.tokenize(l);
  const n = this.ruler2.getRules(""), r = n.length;
  for (let e = 0; e < r; e++)
    n[e](l);
};
p.prototype.State = k;
export {
  p as default
};
