import { visit } from "unist-util-visit";
const FILENAME_PATTERN = /(?:^|\s)(?:title|filename)=(?:"([^"]*)"|'([^']*)')/;
const HIGHLIGHT_PATTERN = /(?:^|\s)\{([\d,\s-]+)\}(?=\s|$)/;
export function parseCodeMeta(meta) {
    const filenameMatch = meta?.match(FILENAME_PATTERN);
    const highlightMatch = meta?.match(HIGHLIGHT_PATTERN);
    const highlightedLines = new Set();
    for (const part of highlightMatch?.[1]?.split(",") ?? []) {
        const [startValue, endValue] = part.trim().split("-", 2);
        const start = Number(startValue);
        const end = endValue === undefined ? start : Number(endValue);
        if (!Number.isInteger(start) || !Number.isInteger(end) || start < 1)
            continue;
        for (let line = start; line <= end && line <= 10_000; line += 1) {
            highlightedLines.add(line);
        }
    }
    return {
        filename: filenameMatch?.[1] ?? filenameMatch?.[2],
        highlightedLines,
        showLineNumbers: true,
    };
}
export function remarkCodeMeta() {
    return (tree) => {
        visit(tree, "code", (node) => {
            if (!node.meta)
                return;
            const data = (node.data ??= {});
            data.hProperties = {
                ...data.hProperties,
                dataMeta: node.meta,
            };
        });
    };
}
