import { defineStore } from 'pinia'

// 定义全局设备数组的store
export const useDeviceStore = defineStore('device', {
  state: () => ({
    // 全局设备数组：包含{ modelId, modelName }结构的对象
    deviceList: []
  }),
  
  actions: {
    // 添加设备到全局数组
    addDevice(modelId, modelName) {
      // 检查是否已存在相同的modelId
      const existingIndex = this.deviceList.findIndex(device => device.modelId === modelId)
      
      if (existingIndex === -1 && modelId && modelName) {
        this.deviceList.push({
          modelId,
          modelName
        })
      }
    },
    
    // 清空设备列表
    clearDevices() {
      this.deviceList = []
    },
    
    // 获取用于Select组件的选项格式
    getSelectOptions() {
      return this.deviceList.map(device => ({
        value: device.modelId,  // 设备id作为value
        label: device.modelName // 设备名称作为label
      }))
    }
  }
})