import { defineStore } from 'pinia'
import { useDeviceStore } from './device'

// 定义路径点store
export const usePathStore = defineStore('path', {
  state: () => ({
    // 路径点数组：包含{ modelId, deviceName }结构的对象
    pathPoints: []
  }),
  
  actions: {
    // 同步路径点数据
    syncPathPoints(pathIdList) {
      const deviceStore = useDeviceStore()
      
      // 清空当前路径点数组
      this.pathPoints = []
      
      // 根据pathIdList中的id顺序，查找对应的设备信息并同步
      pathIdList.forEach(modelId => {
        // 从deviceList中查找对应的设备信息
        const device = deviceStore.deviceList.find(d => d.modelId === modelId)
        
        if (device) {
          this.pathPoints.push({
            modelId: modelId,
            deviceName: device.modelName
          })
        } else {
          // 如果没有找到对应的设备信息，仍添加modelId但deviceName为空或默认值
          this.pathPoints.push({
            modelId: modelId,
            deviceName: `未知设备(${modelId})`
          })
        }
      })
    },
    
    // 清空路径点
    clearPathPoints() {
      this.pathPoints = []
    },
    
    // 获取路径点列表
    getPathPoints() {
      return this.pathPoints
    }
  }
})