import { computed } from 'vue'
import { useProjectStore } from '@/store/modules/project'
import { bfsWalk } from 'simple-mind-map/src/utils'

type Widget = {
  id: string
  name: string
  type: string
}
/**
 * 获取所有 widget
 */
export const useAllWidgets = () => {
  const projectStore = useProjectStore()

  const allWidgets = computed(() => {
    const widgets: Widget[] = []
    projectStore.project?.screens?.forEach((screen) => {
      widgets.push({
        id: screen.id,
        name: screen.name,
        type: 'screen'
      })
      screen.pages.forEach((page) => {
        // 递归获取页面所有 widget
        bfsWalk(page, (child) => {
          widgets.push({
            id: child.id,
            name: child.name,
            type: child.type
          })
        })
      })
    })

    return widgets
  })

  return { allWidgets }
}
