import type { GameData } from '@engine/types'

export function computeEdges(gameData: GameData): { id: string; source: string; target: string; label?: string }[] {
  const result: { id: string; source: string; target: string; label?: string }[] = []
  for (const [id, scene] of Object.entries(gameData.scenes)) {
    if (scene.choices) {
      let ci = 0
      for (const c of scene.choices) {
        if (c.targetScene) {
          result.push({ id: `${id}_choice_${ci}`, source: id, target: c.targetScene, label: c.text.slice(0, 10) })
        }
        ci++
      }
    }
    if (scene.nextScene) {
      if (Array.isArray(scene.nextScene)) {
        for (let ri = 0; ri < scene.nextScene.length; ri++) {
          const r = scene.nextScene[ri]
          if (r.targetScene) {
            result.push({ id: `${id}_next_${ri}`, source: id, target: r.targetScene, label: r.conditions?.length ? '→ 条件' : '→ 默认' })
          }
        }
      } else {
        result.push({ id: `${id}_next`, source: id, target: scene.nextScene, label: '→' })
      }
    }
    if (scene.qte) {
      if (scene.qte.successScene)
        result.push({ id: `${id}_qte_s`, source: id, target: scene.qte.successScene, label: 'QTE成功' })
      if (scene.qte.failScene)
        result.push({ id: `${id}_qte_f`, source: id, target: scene.qte.failScene, label: 'QTE失败' })
    }
    if (scene.hotspots) {
      for (const h of scene.hotspots) {
        if (h.targetScene) {
          result.push({ id: `${id}_hs_${h.id}`, source: id, target: h.targetScene, label: h.label.slice(0, 10) })
        }
      }
    }
  }
  return result
}

export function computeSceneNodes(gameData: GameData): { id: string; label: string }[] {
  return Object.values(gameData.scenes).map(s => ({ id: s.id, label: s.id }))
}
