"use client";
import { factorySpace as b } from "../../micromark-factory-space/index.js";
import { markdownLineEndingOrSpace as s } from "../../micromark-util-character/index.js";
import { normalizeIdentifier as p } from "../../micromark-util-normalize-identifier/index.js";
import { blankLine as D } from "../../micromark-core-commonmark/lib/blank-line.js";
const x = {
  tokenize: d,
  partial: !0
};
function P() {
  return {
    document: {
      91: {
        name: "gfmFootnoteDefinition",
        tokenize: L,
        continuation: {
          tokenize: z
        },
        exit: c
      }
    },
    text: {
      91: {
        name: "gfmFootnoteCall",
        tokenize: y
      },
      93: {
        name: "gfmPotentialFootnoteCall",
        add: "after",
        tokenize: M,
        resolveTo: S
      }
    }
  };
}
function M(t, r, n) {
  const o = this;
  let u = o.events.length;
  const l = o.parser.gfmFootnotes || (o.parser.gfmFootnotes = []);
  let a;
  for (; u--; ) {
    const m = o.events[u][1];
    if (m.type === "labelImage") {
      a = m;
      break;
    }
    if (m.type === "gfmFootnoteCall" || m.type === "labelLink" || m.type === "label" || m.type === "image" || m.type === "link")
      break;
  }
  return f;
  function f(m) {
    if (!a || !a._balanced)
      return n(m);
    const g = p(o.sliceSerialize({
      start: a.end,
      end: o.now()
    }));
    return g.codePointAt(0) !== 94 || !l.includes(g.slice(1)) ? n(m) : (t.enter("gfmFootnoteCallLabelMarker"), t.consume(m), t.exit("gfmFootnoteCallLabelMarker"), r(m));
  }
}
function S(t, r) {
  let n = t.length;
  for (; n--; )
    if (t[n][1].type === "labelImage" && t[n][0] === "enter") {
      t[n][1];
      break;
    }
  t[n + 1][1].type = "data", t[n + 3][1].type = "gfmFootnoteCallLabelMarker";
  const o = {
    type: "gfmFootnoteCall",
    start: Object.assign({}, t[n + 3][1].start),
    end: Object.assign({}, t[t.length - 1][1].end)
  }, u = {
    type: "gfmFootnoteCallMarker",
    start: Object.assign({}, t[n + 3][1].end),
    end: Object.assign({}, t[n + 3][1].end)
  };
  u.end.column++, u.end.offset++, u.end._bufferIndex++;
  const l = {
    type: "gfmFootnoteCallString",
    start: Object.assign({}, u.end),
    end: Object.assign({}, t[t.length - 1][1].start)
  }, a = {
    type: "chunkString",
    contentType: "string",
    start: Object.assign({}, l.start),
    end: Object.assign({}, l.end)
  }, f = [
    // Take the `labelImageMarker` (now `data`, the `!`)
    t[n + 1],
    t[n + 2],
    ["enter", o, r],
    // The `[`
    t[n + 3],
    t[n + 4],
    // The `^`.
    ["enter", u, r],
    ["exit", u, r],
    // Everything in between.
    ["enter", l, r],
    ["enter", a, r],
    ["exit", a, r],
    ["exit", l, r],
    // The ending (`]`, properly parsed and labelled).
    t[t.length - 2],
    t[t.length - 1],
    ["exit", o, r]
  ];
  return t.splice(n, t.length - n + 1, ...f), t;
}
function y(t, r, n) {
  const o = this, u = o.parser.gfmFootnotes || (o.parser.gfmFootnotes = []);
  let l = 0, a;
  return f;
  function f(i) {
    return t.enter("gfmFootnoteCall"), t.enter("gfmFootnoteCallLabelMarker"), t.consume(i), t.exit("gfmFootnoteCallLabelMarker"), m;
  }
  function m(i) {
    return i !== 94 ? n(i) : (t.enter("gfmFootnoteCallMarker"), t.consume(i), t.exit("gfmFootnoteCallMarker"), t.enter("gfmFootnoteCallString"), t.enter("chunkString").contentType = "string", g);
  }
  function g(i) {
    if (
      // Too long.
      l > 999 || // Closing brace with nothing.
      i === 93 && !a || // Space or tab is not supported by GFM for some reason.
      // `\n` and `[` not being supported makes sense.
      i === null || i === 91 || s(i)
    )
      return n(i);
    if (i === 93) {
      t.exit("chunkString");
      const k = t.exit("gfmFootnoteCallString");
      return u.includes(p(o.sliceSerialize(k))) ? (t.enter("gfmFootnoteCallLabelMarker"), t.consume(i), t.exit("gfmFootnoteCallLabelMarker"), t.exit("gfmFootnoteCall"), r) : n(i);
    }
    return s(i) || (a = !0), l++, t.consume(i), i === 92 ? F : g;
  }
  function F(i) {
    return i === 91 || i === 92 || i === 93 ? (t.consume(i), l++, g) : g(i);
  }
}
function L(t, r, n) {
  const o = this, u = o.parser.gfmFootnotes || (o.parser.gfmFootnotes = []);
  let l, a = 0, f;
  return m;
  function m(e) {
    return t.enter("gfmFootnoteDefinition")._container = !0, t.enter("gfmFootnoteDefinitionLabel"), t.enter("gfmFootnoteDefinitionLabelMarker"), t.consume(e), t.exit("gfmFootnoteDefinitionLabelMarker"), g;
  }
  function g(e) {
    return e === 94 ? (t.enter("gfmFootnoteDefinitionMarker"), t.consume(e), t.exit("gfmFootnoteDefinitionMarker"), t.enter("gfmFootnoteDefinitionLabelString"), t.enter("chunkString").contentType = "string", F) : n(e);
  }
  function F(e) {
    if (
      // Too long.
      a > 999 || // Closing brace with nothing.
      e === 93 && !f || // Space or tab is not supported by GFM for some reason.
      // `\n` and `[` not being supported makes sense.
      e === null || e === 91 || s(e)
    )
      return n(e);
    if (e === 93) {
      t.exit("chunkString");
      const C = t.exit("gfmFootnoteDefinitionLabelString");
      return l = p(o.sliceSerialize(C)), t.enter("gfmFootnoteDefinitionLabelMarker"), t.consume(e), t.exit("gfmFootnoteDefinitionLabelMarker"), t.exit("gfmFootnoteDefinitionLabel"), k;
    }
    return s(e) || (f = !0), a++, t.consume(e), e === 92 ? i : F;
  }
  function i(e) {
    return e === 91 || e === 92 || e === 93 ? (t.consume(e), a++, F) : F(e);
  }
  function k(e) {
    return e === 58 ? (t.enter("definitionMarker"), t.consume(e), t.exit("definitionMarker"), u.includes(l) || u.push(l), b(t, h, "gfmFootnoteDefinitionWhitespace")) : n(e);
  }
  function h(e) {
    return r(e);
  }
}
function z(t, r, n) {
  return t.check(D, r, t.attempt(x, r, n));
}
function c(t) {
  t.exit("gfmFootnoteDefinition");
}
function d(t, r, n) {
  const o = this;
  return b(t, u, "gfmFootnoteDefinitionIndent", 5);
  function u(l) {
    const a = o.events[o.events.length - 1];
    return a && a[1].type === "gfmFootnoteDefinitionIndent" && a[2].sliceSerialize(a[1], !0).length === 4 ? r(l) : n(l);
  }
}
export {
  P as gfmFootnote
};
