"use client";
import { factoryDestination as d } from "../../micromark-factory-destination/index.js";
import { factoryLabel as h } from "../../micromark-factory-label/index.js";
import { factoryTitle as M } from "../../micromark-factory-title/index.js";
import { factoryWhitespace as k } from "../../micromark-factory-whitespace/index.js";
import { markdownLineEndingOrSpace as m } from "../../micromark-util-character/index.js";
import { push as b, splice as y } from "../../micromark-util-chunked/index.js";
import { normalizeIdentifier as g } from "../../micromark-util-normalize-identifier/index.js";
import { resolveAll as E } from "../../micromark-util-resolve-all/index.js";
const j = {
  name: "labelEnd",
  resolveAll: S,
  resolveTo: w,
  tokenize: D
}, L = {
  tokenize: T
}, z = {
  tokenize: C
}, x = {
  tokenize: A
};
function S(e) {
  let u = -1;
  const a = [];
  for (; ++u < e.length; ) {
    const n = e[u][1];
    if (a.push(e[u]), n.type === "labelImage" || n.type === "labelLink" || n.type === "labelEnd") {
      const i = n.type === "labelImage" ? 4 : 2;
      n.type = "data", u += i;
    }
  }
  return e.length !== a.length && y(e, 0, e.length, a), e;
}
function w(e, u) {
  let a = e.length, n = 0, i, t, o, l;
  for (; a--; )
    if (i = e[a][1], t) {
      if (i.type === "link" || i.type === "labelLink" && i._inactive)
        break;
      e[a][0] === "enter" && i.type === "labelLink" && (i._inactive = !0);
    } else if (o) {
      if (e[a][0] === "enter" && (i.type === "labelImage" || i.type === "labelLink") && !i._balanced && (t = a, i.type !== "labelLink")) {
        n = 2;
        break;
      }
    } else i.type === "labelEnd" && (o = a);
  const p = {
    type: e[t][1].type === "labelLink" ? "link" : "image",
    start: {
      ...e[t][1].start
    },
    end: {
      ...e[e.length - 1][1].end
    }
  }, f = {
    type: "label",
    start: {
      ...e[t][1].start
    },
    end: {
      ...e[o][1].end
    }
  }, c = {
    type: "labelText",
    start: {
      ...e[t + n + 2][1].end
    },
    end: {
      ...e[o - 2][1].start
    }
  };
  return l = [["enter", p, u], ["enter", f, u]], l = b(l, e.slice(t + 1, t + n + 3)), l = b(l, [["enter", c, u]]), l = b(l, E(u.parser.constructs.insideSpan.null, e.slice(t + n + 4, o - 3), u)), l = b(l, [["exit", c, u], e[o - 2], e[o - 1], ["exit", f, u]]), l = b(l, e.slice(o + 1)), l = b(l, [["exit", p, u]]), y(e, t, e.length, l), e;
}
function D(e, u, a) {
  const n = this;
  let i = n.events.length, t, o;
  for (; i--; )
    if ((n.events[i][1].type === "labelImage" || n.events[i][1].type === "labelLink") && !n.events[i][1]._balanced) {
      t = n.events[i][1];
      break;
    }
  return l;
  function l(s) {
    return t ? t._inactive ? r(s) : (o = n.parser.defined.includes(g(n.sliceSerialize({
      start: t.end,
      end: n.now()
    }))), e.enter("labelEnd"), e.enter("labelMarker"), e.consume(s), e.exit("labelMarker"), e.exit("labelEnd"), p) : a(s);
  }
  function p(s) {
    return s === 40 ? e.attempt(L, c, o ? c : r)(s) : s === 91 ? e.attempt(z, c, o ? f : r)(s) : o ? c(s) : r(s);
  }
  function f(s) {
    return e.attempt(x, c, r)(s);
  }
  function c(s) {
    return u(s);
  }
  function r(s) {
    return t._balanced = !0, a(s);
  }
}
function T(e, u, a) {
  return n;
  function n(r) {
    return e.enter("resource"), e.enter("resourceMarker"), e.consume(r), e.exit("resourceMarker"), i;
  }
  function i(r) {
    return m(r) ? k(e, t)(r) : t(r);
  }
  function t(r) {
    return r === 41 ? c(r) : d(e, o, l, "resourceDestination", "resourceDestinationLiteral", "resourceDestinationLiteralMarker", "resourceDestinationRaw", "resourceDestinationString", 32)(r);
  }
  function o(r) {
    return m(r) ? k(e, p)(r) : c(r);
  }
  function l(r) {
    return a(r);
  }
  function p(r) {
    return r === 34 || r === 39 || r === 40 ? M(e, f, a, "resourceTitle", "resourceTitleMarker", "resourceTitleString")(r) : c(r);
  }
  function f(r) {
    return m(r) ? k(e, c)(r) : c(r);
  }
  function c(r) {
    return r === 41 ? (e.enter("resourceMarker"), e.consume(r), e.exit("resourceMarker"), e.exit("resource"), u) : a(r);
  }
}
function C(e, u, a) {
  const n = this;
  return i;
  function i(l) {
    return h.call(n, e, t, o, "reference", "referenceMarker", "referenceString")(l);
  }
  function t(l) {
    return n.parser.defined.includes(g(n.sliceSerialize(n.events[n.events.length - 1][1]).slice(1, -1))) ? u(l) : a(l);
  }
  function o(l) {
    return a(l);
  }
}
function A(e, u, a) {
  return n;
  function n(t) {
    return e.enter("reference"), e.enter("referenceMarker"), e.consume(t), e.exit("referenceMarker"), i;
  }
  function i(t) {
    return t === 93 ? (e.enter("referenceMarker"), e.consume(t), e.exit("referenceMarker"), e.exit("reference"), u) : a(t);
  }
}
export {
  j as labelEnd
};
