"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var rst_exports = {};
__export(rst_exports, {
  rstParser: () => rstParser
});
module.exports = __toCommonJS(rst_exports);
const ADORNMENT_CHARS = new Set("= - ` : . ' \" ~ ^ _ * + #".split(" "));
function isAdornmentLine(line) {
  const trimmed = line.trim();
  if (trimmed.length < 3) return false;
  const char = trimmed[0];
  if (!ADORNMENT_CHARS.has(char)) return false;
  return trimmed.split("").every((c) => c === char);
}
function extractHeadings(content) {
  const lines = content.split("\n");
  const headings = [];
  const adornmentOrder = [];
  let byteOffset = 0;
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    const lineBytes = Buffer.byteLength(line, "utf8") + 1;
    if (i + 1 < lines.length && line.trim().length > 0 && !isAdornmentLine(line)) {
      const nextLine = lines[i + 1];
      if (isAdornmentLine(nextLine) && nextLine.trim().length >= line.trim().length) {
        const adornChar = nextLine.trim()[0];
        let hasOverline = false;
        if (i > 0 && isAdornmentLine(lines[i - 1]) && lines[i - 1].trim()[0] === adornChar) {
          hasOverline = true;
        }
        const key = hasOverline ? `over_${adornChar}` : adornChar;
        let levelIdx = adornmentOrder.indexOf(key);
        if (levelIdx === -1) {
          adornmentOrder.push(key);
          levelIdx = adornmentOrder.length - 1;
        }
        headings.push({
          title: line.trim(),
          level: levelIdx + 1,
          lineIndex: i,
          byteOffset: hasOverline ? byteOffset - (Buffer.byteLength(lines[i - 1], "utf8") + 1) : byteOffset
        });
      }
    }
    byteOffset += lineBytes;
  }
  return headings;
}
function buildHierarchy(headings, content) {
  const contentBytes = Buffer.byteLength(content, "utf8");
  if (headings.length === 0) {
    return [{
      title: "(document)",
      level: 0,
      byteStart: 0,
      byteEnd: contentBytes,
      content,
      children: []
    }];
  }
  const sections = [];
  for (let i = 0; i < headings.length; i++) {
    const start = headings[i].byteOffset;
    const end = i + 1 < headings.length ? headings[i + 1].byteOffset : contentBytes;
    const sectionContent = Buffer.from(content, "utf8").slice(start, end).toString("utf8");
    sections.push({ ...headings[i], byteEnd: end, content: sectionContent });
  }
  const root = [];
  const stack = [];
  for (const s of sections) {
    const parsed = {
      title: s.title,
      level: s.level,
      byteStart: s.byteOffset,
      byteEnd: s.byteEnd,
      content: s.content,
      children: []
    };
    while (stack.length > 0 && stack[stack.length - 1].level >= s.level) {
      stack.pop();
    }
    if (stack.length === 0) {
      root.push(parsed);
    } else {
      stack[stack.length - 1].section.children.push(parsed);
    }
    stack.push({ section: parsed, level: s.level });
  }
  return root;
}
const rstParser = {
  name: "rst",
  extensions: [".rst"],
  parse(content, _filePath) {
    const headings = extractHeadings(content);
    const sections = buildHierarchy(headings, content);
    return { sections, format: "rst" };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  rstParser
});
//# sourceMappingURL=rst.js.map
