import chalk from 'chalk';
import path from 'node:path';
import { z } from 'zod';
import { sectionHeader, stripAnsi } from './tabular.js';
import { DocumentStructureSchema } from '../structure/types.js';
const colorize = {
    header: s => chalk.bold(s),
    code: s => chalk.cyan(s),
    title: s => s,
    page: s => chalk.dim(s),
    dim: s => chalk.dim(s),
    summary: s => chalk.dim(s),
};
const identity = s => s;
const noColor = Object.fromEntries(Object.keys(colorize).map(k => [k, identity]));
export const StructureResultSchema = z.object({
    file: z.string(),
    structure: DocumentStructureSchema,
    markdown: z.string(),
});
export const TreeOptionsSchema = z.object({
    ci: z.boolean().optional(),
});
function formatPageRange(node) {
    if (node.startPage == null)
        return '';
    if (node.endPage != null && node.endPage !== node.startPage) {
        return `p.${node.startPage}-${node.endPage}`;
    }
    return `p.${node.startPage}`;
}
function formatTreeNode(node, width, c) {
    const indent = '  '.repeat(node.level - 1);
    const code = node.structureCode || '';
    const left = `${indent}${c.code(code)}   ${c.title(node.title)}`;
    const pageStr = formatPageRange(node);
    if (!pageStr)
        return left;
    const leftLen = stripAnsi(left).length;
    const rightStr = c.page(pageStr);
    const rightLen = pageStr.length;
    const gap = Math.max(2, width - leftLen - rightLen - 2);
    const dots = c.dim('.'.repeat(gap));
    return `${left} ${dots} ${rightStr}`;
}
export function formatStructureTree(result, options = {}) {
    const { ci = false } = options;
    const c = ci ? noColor : colorize;
    const { structure } = result;
    const fileName = path.basename(result.file);
    const width = 70;
    const lines = [];
    lines.push('');
    lines.push(c.header(sectionHeader(`Structure: ${fileName}`, width, ci)));
    if (structure.totalNodes === 0) {
        lines.push(c.dim('  (no headings found)'));
    }
    else {
        function renderNodes(nodes) {
            for (const node of nodes) {
                lines.push(formatTreeNode(node, width, c));
                renderNodes(node.children);
            }
        }
        renderNodes(structure.rootNodes);
    }
    // Summary line
    const rootCount = structure.rootNodes.length;
    const parts = [];
    parts.push(`${rootCount} section${rootCount !== 1 ? 's' : ''}`);
    if (structure.totalNodes !== rootCount) {
        parts.push(`${structure.totalNodes} nodes`);
    }
    if (structure.maxDepth > 1) {
        parts.push(`max depth ${structure.maxDepth}`);
    }
    lines.push('');
    lines.push(c.summary(parts.join(', ')));
    return lines.join('\n');
}
export function formatStructureJson(results) {
    return results.map(r => ({
        file: r.file,
        totalNodes: r.structure.totalNodes,
        maxDepth: r.structure.maxDepth,
        nodes: r.structure.rootNodes,
    }));
}
//# sourceMappingURL=tree.js.map