import { defineStore } from 'pinia'

export const useForwardStore = defineStore('forward', {
  state: () => ({
    // 转发配置状态
    serialToMQTT: true,  // 串口数据转发到MQTT
    mqttToSerial: true   // MQTT数据转发到串口
  }),
  
  actions: {
    // 设置串口到MQTT的转发状态
    setSerialToMQTT(value) {
      this.serialToMQTT = value
    },
    
    // 设置MQTT到串口的转发状态
    setMQTTToSerial(value) {
      this.mqttToSerial = value
    },
    
    // 切换串口到MQTT的转发状态
    toggleSerialToMQTT() {
      this.serialToMQTT = !this.serialToMQTT
    },
    
    // 切换MQTT到串口的转发状态
    toggleMQTTToSerial() {
      this.mqttToSerial = !this.mqttToSerial
    },
    
    // 获取完整的转发配置
    getForwardConfig() {
      return {
        serial_to_mqtt: this.serialToMQTT,
        mqtt_to_serial: this.mqttToSerial
      }
    },
    
    // 从API返回的数据中更新状态
    updateFromAPIResponse(data) {
      if (data.serial_to_mqtt !== undefined) {
        this.serialToMQTT = data.serial_to_mqtt
      }
      if (data.mqtt_to_serial !== undefined) {
        this.mqttToSerial = data.mqtt_to_serial
      }
    }
  }
})