"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 plaintext_exports = {};
__export(plaintext_exports, {
  plaintextParser: () => plaintextParser
});
module.exports = __toCommonJS(plaintext_exports);
function isAllCaps(line) {
  const trimmed = line.trim();
  if (trimmed.length < 3) return false;
  return /[A-Z]/.test(trimmed) && trimmed === trimmed.toUpperCase() && /^[A-Z\s\d\-_:]+$/.test(trimmed);
}
function isDashUnderline(line) {
  const trimmed = line.trim();
  return trimmed.length >= 3 && /^[-=~]+$/.test(trimmed);
}
function extractHeadings(content) {
  const lines = content.split("\n");
  const headings = [];
  let byteOffset = 0;
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    const lineBytes = Buffer.byteLength(line, "utf8") + 1;
    const trimmed = line.trim();
    if (isAllCaps(trimmed) && (i === 0 || lines[i - 1].trim() === "")) {
      headings.push({
        title: trimmed,
        level: 1,
        byteOffset
      });
      byteOffset += lineBytes;
      continue;
    }
    if (trimmed.length > 0 && i + 1 < lines.length && isDashUnderline(lines[i + 1])) {
      const underlineChar = lines[i + 1].trim()[0];
      headings.push({
        title: trimmed,
        level: underlineChar === "=" ? 1 : 2,
        byteOffset
      });
      byteOffset += lineBytes;
      continue;
    }
    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 plaintextParser = {
  name: "plaintext",
  extensions: [".txt"],
  parse(content, _filePath) {
    const headings = extractHeadings(content);
    const sections = buildHierarchy(headings, content);
    return { sections, format: "plaintext" };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  plaintextParser
});
//# sourceMappingURL=plaintext.js.map
