import type { OrgFile, OrgNode } from '@/types/org'

export function generateOrgContent(file: OrgFile): string {
  let content = ''
  
  // Add metadata
  if (file.metadata) {
    for (const [key, value] of Object.entries(file.metadata)) {
      content += `#+${key}: ${value}\n`
    }
    content += '\n'
  }
  
  // Add nodes
  for (const node of file.nodes) {
    content += generateNodeContent(node)
  }
  
  return content.trim()
}

function generateNodeContent(node: OrgNode): string {
  let content = ''
  
  // Generate heading
  const stars = '*'.repeat(node.level)
  let heading = `${stars} ${node.title}`
  
  // Add tags
  if (node.tags && node.tags.length > 0) {
    heading += ` :${node.tags.join(':')}:`
  }
  
  content += heading + '\n'
  
  // Add content
  if (node.content) {
    content += node.content + '\n'
  }
  
  // Add children
  for (const child of node.children) {
    content += generateNodeContent(child)
  }
  
  return content
}