import { walk, nodeRichTextToTextWithWrap } from '../utils'
import { NodeData, Node } from "../types"


const getNodeText = (data: NodeData): string => {
  return data.richText ? nodeRichTextToTextWithWrap(data.text) : data.text
}

const getIndent = (level: number): string => {
  return new Array(level).fill('   ').join('')
}

// 转换成txt格式
export const transformToTxt = (root: Node): string => {
  let content = ''
  walk(
    root,
    null,
    (node: Node, parent: Node | null, isRoot: boolean, layerIndex: number) => {
      content += getIndent(layerIndex)
      content += ' ' + getNodeText(node.data)
      // 概要
      const generalization = node.data.generalization
      if (Array.isArray(generalization)) {
        content += generalization.map(item => {
          return ` [${getNodeText(item)}]`
        })
      } else if (generalization && generalization.text) {
        content += ` [${getNodeText(generalization)}]`
      }
      content += '\n\n'
    },
    () => { },
    true
  )
  return content
}