import { z } from 'zod';
export const PageMappingSchema = z.object({
    pageNumber: z.number(),
    startChar: z.number(),
    endChar: z.number(),
});
export const StructureNodeSchema = z.object({
    nodeId: z.string(),
    title: z.string(),
    level: z.number(),
    startChar: z.number(),
    endChar: z.number(),
    startLine: z.number(),
    startPage: z.number().optional(),
    endPage: z.number().optional(),
    parentNodeId: z.string().optional(),
    structureCode: z.string().optional(),
    wordCount: z.number().optional(),
    tokenEstimate: z.number().optional(),
    children: z.lazy(() => z.array(StructureNodeSchema)),
});
export const DocumentStructureSchema = z.object({
    rootNodes: z.array(StructureNodeSchema),
    pageMappings: z.array(PageMappingSchema),
    totalNodes: z.number(),
    maxDepth: z.number(),
});
export function flatten(nodes) {
    const result = [];
    function walk(list) {
        for (const node of list) {
            result.push(node);
            walk(node.children);
        }
    }
    walk(nodes);
    return result;
}
export function getNodeById(nodes, nodeId) {
    for (const node of flatten(nodes)) {
        if (node.nodeId === nodeId)
            return node;
    }
    return undefined;
}
export function getNodeByPath(nodes, structureCode) {
    for (const node of flatten(nodes)) {
        if (node.structureCode === structureCode)
            return node;
    }
    return undefined;
}
export function toDict(structure) {
    function nodeToDict(node) {
        return {
            nodeId: node.nodeId,
            title: node.title,
            level: node.level,
            startChar: node.startChar,
            endChar: node.endChar,
            startLine: node.startLine,
            startPage: node.startPage,
            endPage: node.endPage,
            parentNodeId: node.parentNodeId,
            structureCode: node.structureCode,
            wordCount: node.wordCount,
            tokenEstimate: node.tokenEstimate,
            children: node.children.map(nodeToDict),
        };
    }
    return {
        rootNodes: structure.rootNodes.map(nodeToDict),
        pageMappings: structure.pageMappings,
        totalNodes: structure.totalNodes,
        maxDepth: structure.maxDepth,
    };
}
export function fromDict(data) {
    return DocumentStructureSchema.parse(data);
}
//# sourceMappingURL=types.js.map