export const findMarkdownByPath = (tree, pathString) => {
    if (!tree || !pathString) return null;
    
    const pathSegments = pathString.split('/').filter(segment => segment.length > 0);

    if (pathSegments.length === 0) {
        const rootIndex = tree.children?.find(
            child => child.type === 'markdown' && child.title === 'index'
        );
        return rootIndex || null;
    }
    let currentNode = tree;

    for (let i = 0; i < pathSegments.length; i++) {
        const segment = pathSegments[i];

        const childPath = currentNode.children?.find(
            child => child.type === 'path' && child.name === segment
        );
        
        if (!childPath) {
            if (i === pathSegments.length - 1) {
                const markdownNode = currentNode.children?.find(
                    child => child.type === 'markdown' && child.title === segment
                );
                return markdownNode || null;
            }
            return null;
        }
        
        currentNode = childPath;
    }
    

    const indexMarkdown = currentNode.children?.find(
        child => child.type === 'markdown' && child.title === 'index'
    );
    
    return indexMarkdown || null;
};

export const getMarkdownIdByPath = (tree, pathString) => {
    const markdownNode = findMarkdownByPath(tree, pathString);
    return markdownNode?.id || null;
};