/**
 * Markdown parser — public API.
 *
 * Re-exports types from markdown-rules.ts and provides:
 * - parseMarkdown(): full decoration extraction
 */

import type { Tree } from "@lezer/common"
import { GFM, parser } from "@lezer/markdown"
import { createMarkdownExtensions } from "@/lib/markdown-extensions"
import {
  type BlockDecoration,
  type BlockRefDecoration,
  type CalloutType,
  type DecorationRange,
  type HighlightDecoration,
  type ImageDecoration,
  type InlineMathDecoration,
  MarkdownRuleEngine,
  type PageRefDecoration,
  type ParsedDecorations,
  type PropertyInfo,
  type TagDecoration,
  type TaskState,
  type TokenDecoration,
} from "@/lib/markdown-rules"

export type {
  BlockDecoration,
  BlockRefDecoration,
  CalloutType,
  DecorationRange,
  HighlightDecoration,
  ImageDecoration,
  InlineMathDecoration,
  PageRefDecoration,
  ParsedDecorations,
  PropertyInfo,
  TagDecoration,
  TaskState,
  TokenDecoration,
}
export { MarkdownRuleEngine }

const markdownParser = parser.configure([GFM, ...createMarkdownExtensions()])

const defaultEngine = new MarkdownRuleEngine()

export function parseMarkdown(
  docText: string,
  engine: MarkdownRuleEngine = defaultEngine,
  oldTree?: Tree,
): ParsedDecorations & { tree: Tree } {
  return engine.parse(docText, markdownParser, oldTree)
}
