"use client";
import { factorySpace as d } from "../../micromark-factory-space/index.js";
import { markdownSpace as g, markdownLineEnding as y } from "../../micromark-util-character/index.js";
const S = {
  name: "setextUnderline",
  resolveTo: h,
  tokenize: s
};
function h(e, a) {
  let i = e.length, t, l, r;
  for (; i--; )
    if (e[i][0] === "enter") {
      if (e[i][1].type === "content") {
        t = i;
        break;
      }
      e[i][1].type === "paragraph" && (l = i);
    } else
      e[i][1].type === "content" && e.splice(i, 1), !r && e[i][1].type === "definition" && (r = i);
  const p = {
    type: "setextHeading",
    start: {
      ...e[t][1].start
    },
    end: {
      ...e[e.length - 1][1].end
    }
  };
  return e[l][1].type = "setextHeadingText", r ? (e.splice(l, 0, ["enter", p, a]), e.splice(r + 1, 0, ["exit", e[t][1], a]), e[t][1].end = {
    ...e[r][1].end
  }) : e[t][1] = p, e.push(["exit", p, a]), e;
}
function s(e, a, i) {
  const t = this;
  let l;
  return r;
  function r(n) {
    let u = t.events.length, f;
    for (; u--; )
      if (t.events[u][1].type !== "lineEnding" && t.events[u][1].type !== "linePrefix" && t.events[u][1].type !== "content") {
        f = t.events[u][1].type === "paragraph";
        break;
      }
    return !t.parser.lazy[t.now().line] && (t.interrupt || f) ? (e.enter("setextHeadingLine"), l = n, p(n)) : i(n);
  }
  function p(n) {
    return e.enter("setextHeadingLineSequence"), o(n);
  }
  function o(n) {
    return n === l ? (e.consume(n), o) : (e.exit("setextHeadingLineSequence"), g(n) ? d(e, x, "lineSuffix")(n) : x(n));
  }
  function x(n) {
    return n === null || y(n) ? (e.exit("setextHeadingLine"), a(n)) : i(n);
  }
}
export {
  S as setextUnderline
};
