"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 org_exports = {};
__export(org_exports, {
  orgParser: () => orgParser
});
module.exports = __toCommonJS(org_exports);
const HEADING_RE = /^(\*{1,6})\s+(.+)$/;
function extractHeadings(content) {
  const lines = content.split("\n");
  const headings = [];
  let byteOffset = 0;
  let inBlock = false;
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    const lineBytes = Buffer.byteLength(line, "utf8") + 1;
    if (/^#\+BEGIN_/i.test(line)) {
      inBlock = true;
      byteOffset += lineBytes;
      continue;
    }
    if (/^#\+END_/i.test(line)) {
      inBlock = false;
      byteOffset += lineBytes;
      continue;
    }
    if (!inBlock) {
      const match = line.match(HEADING_RE);
      if (match) {
        let title = match[2].trim();
        title = title.replace(/^(TODO|DONE|NEXT|WAITING|CANCELLED)\s+/, "");
        title = title.replace(/^\[#[A-Z]\]\s+/, "");
        title = title.replace(/\s+:[^\s:]+(?::[^\s:]+)*:\s*$/, "");
        headings.push({
          title,
          level: match[1].length,
          lineIndex: i,
          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 orgParser = {
  name: "org",
  extensions: [".org"],
  parse(content, _filePath) {
    const headings = extractHeadings(content);
    const sections = buildHierarchy(headings, content);
    return { sections, format: "org" };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  orgParser
});
//# sourceMappingURL=org.js.map
