"use client";
import { markdownLineEnding as V } from "../../micromark-util-character/index.js";
import { push as W, splice as X } from "../../micromark-util-chunked/index.js";
import { resolveAll as Y } from "../../micromark-util-resolve-all/index.js";
function re(d, l, o) {
  let n = {
    _bufferIndex: -1,
    _index: 0,
    line: o && o.line || 1,
    column: o && o.column || 1,
    offset: o && o.offset || 0
  };
  const f = {}, u = [];
  let s = [], h = [];
  const S = {
    attempt: m(J),
    check: m(P),
    consume: D,
    enter: G,
    exit: H,
    interrupt: m(P, {
      interrupt: !0
    })
  }, t = {
    code: null,
    containerState: {},
    defineSkip: R,
    events: [],
    now: v,
    parser: d,
    previous: null,
    sliceSerialize: M,
    sliceStream: j,
    write: L
  };
  let O = l.tokenize.call(t, S);
  return l.resolveAll && u.push(l), t;
  function L(e) {
    return s = W(s, e), q(), s[s.length - 1] !== null ? [] : (T(l, 0), t.events = Y(u, t.events, t), t.events);
  }
  function M(e, r) {
    return $(j(e), r);
  }
  function j(e) {
    return Z(s, e);
  }
  function v() {
    const {
      _bufferIndex: e,
      _index: r,
      line: c,
      column: a,
      offset: p
    } = n;
    return {
      _bufferIndex: e,
      _index: r,
      line: c,
      column: a,
      offset: p
    };
  }
  function R(e) {
    f[e.line] = e.column, g();
  }
  function q() {
    let e;
    for (; n._index < s.length; ) {
      const r = s[n._index];
      if (typeof r == "string")
        for (e = n._index, n._bufferIndex < 0 && (n._bufferIndex = 0); n._index === e && n._bufferIndex < r.length; )
          z(r.charCodeAt(n._bufferIndex));
      else
        z(r);
    }
  }
  function z(e) {
    O = O(e);
  }
  function D(e) {
    V(e) ? (n.line++, n.column = 1, n.offset += e === -3 ? 2 : 1, g()) : e !== -1 && (n.column++, n.offset++), n._bufferIndex < 0 ? n._index++ : (n._bufferIndex++, n._bufferIndex === // Points w/ non-negative `_bufferIndex` reference
    // strings.
    /** @type {string} */
    s[n._index].length && (n._bufferIndex = -1, n._index++)), t.previous = e;
  }
  function G(e, r) {
    const c = r || {};
    return c.type = e, c.start = v(), t.events.push(["enter", c, t]), h.push(c), c;
  }
  function H(e) {
    const r = h.pop();
    return r.end = v(), t.events.push(["exit", r, t]), r;
  }
  function J(e, r) {
    T(e, r.from);
  }
  function P(e, r) {
    r.restore();
  }
  function m(e, r) {
    return c;
    function c(a, p, _) {
      let C, k, B, y;
      return Array.isArray(a) ? (
        /* c8 ignore next 1 */
        A(a)
      ) : "tokenize" in a ? (
        // Looks like a construct.
        A([
          /** @type {Construct} */
          a
        ])
      ) : N(a);
      function N(i) {
        return w;
        function w(x) {
          const b = x !== null && i[x], I = x !== null && i.null, U = [
            // To do: add more extension tests.
            /* c8 ignore next 2 */
            ...Array.isArray(b) ? b : b ? [b] : [],
            ...Array.isArray(I) ? I : I ? [I] : []
          ];
          return A(U)(x);
        }
      }
      function A(i) {
        return C = i, k = 0, i.length === 0 ? _ : E(i[k]);
      }
      function E(i) {
        return w;
        function w(x) {
          return y = K(), B = i, i.partial || (t.currentConstruct = i), i.name && t.parser.constructs.disable.null.includes(i.name) ? F() : i.tokenize.call(
            // If we do have fields, create an object w/ `context` as its
            // prototype.
            // This allows a “live binding”, which is needed for `interrupt`.
            r ? Object.assign(Object.create(t), r) : t,
            S,
            Q,
            F
          )(x);
        }
      }
      function Q(i) {
        return e(B, y), p;
      }
      function F(i) {
        return y.restore(), ++k < C.length ? E(C[k]) : _;
      }
    }
  }
  function T(e, r) {
    e.resolveAll && !u.includes(e) && u.push(e), e.resolve && X(t.events, r, t.events.length - r, e.resolve(t.events.slice(r), t)), e.resolveTo && (t.events = e.resolveTo(t.events, t));
  }
  function K() {
    const e = v(), r = t.previous, c = t.currentConstruct, a = t.events.length, p = Array.from(h);
    return {
      from: a,
      restore: _
    };
    function _() {
      n = e, t.previous = r, t.currentConstruct = c, t.events.length = a, h = p, g();
    }
  }
  function g() {
    n.line in f && n.column < 2 && (n.column = f[n.line], n.offset += f[n.line] - 1);
  }
}
function Z(d, l) {
  const o = l.start._index, n = l.start._bufferIndex, f = l.end._index, u = l.end._bufferIndex;
  let s;
  if (o === f)
    s = [d[o].slice(n, u)];
  else {
    if (s = d.slice(o, f), n > -1) {
      const h = s[0];
      typeof h == "string" ? s[0] = h.slice(n) : s.shift();
    }
    u > 0 && s.push(d[f].slice(0, u));
  }
  return s;
}
function $(d, l) {
  let o = -1;
  const n = [];
  let f;
  for (; ++o < d.length; ) {
    const u = d[o];
    let s;
    if (typeof u == "string")
      s = u;
    else switch (u) {
      case -5: {
        s = "\r";
        break;
      }
      case -4: {
        s = `
`;
        break;
      }
      case -3: {
        s = `\r
`;
        break;
      }
      case -2: {
        s = l ? " " : "	";
        break;
      }
      case -1: {
        if (!l && f) continue;
        s = " ";
        break;
      }
      default:
        s = String.fromCharCode(u);
    }
    f = u === -2, n.push(s);
  }
  return n.join("");
}
export {
  re as createTokenizer
};
