import { parse } from "toolcraft-design";
export function parseTaskBoard(body) {
    const sectionNodes = readTaskBoardSection(body);
    const tasks = [];
    for (const node of sectionNodes) {
        collectTasks(node, tasks);
    }
    const doneCount = tasks.filter((task) => task.done).length;
    const openCount = tasks.length - doneCount;
    return {
        tasks,
        allDone: openCount === 0,
        openCount,
        doneCount
    };
}
export function hasTaskBoard(body) {
    return findTaskBoardHeadingIndex(parseRoot(body).children) !== -1;
}
function readTaskBoardSection(body) {
    const root = parseRoot(body);
    const headingIndex = findTaskBoardHeadingIndex(root.children);
    if (headingIndex === -1) {
        throw new Error('Missing "## Task Board" section');
    }
    const sectionNodes = [];
    for (let index = headingIndex + 1; index < root.children.length; index += 1) {
        const node = root.children[index];
        if (node.type === "heading" && node.depth <= 2) {
            break;
        }
        sectionNodes.push(node);
    }
    return sectionNodes;
}
function parseRoot(body) {
    const ast = parse(body).ast;
    if (ast.type !== "root") {
        throw new Error("Expected markdown root node");
    }
    return ast;
}
function findTaskBoardHeadingIndex(children) {
    return children.findIndex((node) => node.type === "heading" && node.depth === 2 && extractText(node).trim() === "Task Board");
}
function collectTasks(node, tasks) {
    if (node.type === "listItem" && typeof node.checked === "boolean") {
        tasks.push({
            text: readTaskText(node),
            done: node.checked
        });
    }
    if (!("children" in node)) {
        return;
    }
    for (const child of node.children) {
        collectTasks(child, tasks);
    }
}
function readTaskText(node) {
    let text = "";
    for (const child of node.children) {
        if (child.type === "list") {
            continue;
        }
        const childText = extractText(child).trim();
        if (childText.length === 0) {
            continue;
        }
        text = text.length === 0 ? childText : `${text}\n${childText}`;
    }
    return text;
}
function extractText(node) {
    switch (node.type) {
        case "text":
        case "inlineCode":
        case "code":
        case "html":
            return node.value;
        case "image":
            return node.alt;
        case "break":
            return "\n";
        case "footnoteReference":
            return node.label;
        case "thematicBreak":
        case "frontmatter":
            return "";
        default:
            if (!("children" in node)) {
                return "";
            }
            return node.children.map(extractText).join("");
    }
}
