import { loadMarkdownDocument, sliceMarkdownBytes } from "./document.js";
import { resolveSection } from "./resolve.js";
import { UserError } from "toolcraft";
export function createReadSection(dependencies = {}) {
    return async function readSection(params) {
        if (params.section.trim().length === 0) {
            throw new UserError("invalid section: expected a non-empty section id");
        }
        const { source, sections } = await loadMarkdownDocument(params.file, dependencies);
        const section = resolveSection(sections, params.section);
        const end = params.includeChildren === false ? section.bodyEndNoChildren : section.bodyEnd;
        return {
            file: params.file,
            section: {
                depth: section.depth,
                number: section.number,
                title: section.title
            },
            markdown: sliceMarkdownBytes(source, section.headingStart, end)
        };
    };
}
export const readSection = createReadSection();
