import { fromMarkdown } from 'mdast-util-from-markdown'
import type { Node, Parent } from 'mdast'
interface MindMapNode {
  data: {
    text: string
  }
  children: MindMapNode[]
}

const getNodeText = (node: Node): string => {
  // 优先找出其中的text类型的子节点
  const parent = node as Parent
  let textChild = (parent.children || []).find(item => {
    return item.type === 'text'
  })
  // 没有找到，那么直接使用第一个子节点
  textChild = textChild || parent.children[0] as any
  if (textChild) {
    if ('value' in textChild) {
      return textChild.value
    }
    return getNodeText(textChild)
  }
  return ''
}

// 处理list的情况
const handleList = (node: Parent): MindMapNode[] => {
  const list: MindMapNode[] = []
  const walk = (arr: Node[], newArr: MindMapNode[]): void => {
    for (let i = 0; i < arr.length; i++) {
      const cur = arr[i] as Parent
      const node: MindMapNode = {
        data: {
          text: getNodeText(cur)
        },
        children: []
      }
      newArr.push(node)
      if (cur.children.length > 1) {
        for (let j = 1; j < cur.children.length; j++) {
          const cur2 = cur.children[j]
          if (cur2.type === 'list') {
            walk((cur2 as Parent).children, node.children)
          }
        }
      }
    }
  }
  walk(node.children, list)
  return list
}

// 将markdown转换成节点树
export const transformMarkdownTo = (md: string): MindMapNode => {
  const tree = fromMarkdown(md)
  const root: MindMapNode = {
    data: { text: '' },
    children: []
  }
  const childrenQueue: MindMapNode[][] = [root.children]
  let currentChildren: MindMapNode[] = root.children
  const depthQueue: number[] = [-1]
  let currentDepth = -1

  for (let i = 0; i < tree.children.length; i++) {
    const cur = tree.children[i]
    if (cur.type === 'heading') {
      if (!cur.children[0]) continue
      // 创建新节点
      const node: MindMapNode = {
        data: {
          text: getNodeText(cur)
        },
        children: []
      }

      // ... existing code ...
      if (cur.depth > currentDepth) {
        currentChildren.push(node)
        childrenQueue.push(node.children)
        currentChildren = node.children
        depthQueue.push(cur.depth)
        currentDepth = cur.depth
      } else if (cur.depth === currentDepth) {
        childrenQueue.pop()
        currentChildren = childrenQueue[childrenQueue.length - 1]
        depthQueue.pop()
        currentDepth = depthQueue[depthQueue.length - 1]
        currentChildren.push(node)
        childrenQueue.push(node.children)
        currentChildren = node.children
        depthQueue.push(cur.depth)
        currentDepth = cur.depth
      } else {
        while (depthQueue.length) {
          childrenQueue.pop()
          currentChildren = childrenQueue[childrenQueue.length - 1]
          depthQueue.pop()
          currentDepth = depthQueue[depthQueue.length - 1]
          if (currentDepth < cur.depth) {
            currentChildren.push(node)
            childrenQueue.push(node.children)
            currentChildren = node.children
            depthQueue.push(cur.depth)
            currentDepth = cur.depth
            break
          }
        }
      }
    } else if (cur.type === 'list') {
      currentChildren.push(...handleList(cur as Parent))
    }
  }
  return root.children[0]
}