import { UserError } from "toolcraft";
import { loadMarkdownDocument } from "./document.js";
export function createReadMarkdown(dependencies = {}) {
    return async function readMarkdown(params) {
        if (params.depth !== undefined && (!Number.isInteger(params.depth) || params.depth < 0)) {
            throw new UserError("invalid depth: expected a non-negative integer");
        }
        const { frontmatter, sections } = await loadMarkdownDocument(params.file, dependencies);
        return {
            file: params.file,
            frontmatter,
            sections: sections
                .filter((section) => params.depth === undefined || section.depth <= params.depth)
                .map(({ depth, number, title }) => ({ depth, number, title }))
        };
    };
}
export const readMarkdown = createReadMarkdown();
