import { ref, reactive } from 'vue'
import bus from '@/utils/eventBus'

const cars = ref([[], []]) // 车流
const tracked = ref(null) // 追踪车辆 id

// const carTracked = ref(null) // 追踪车辆

const roamkey = ref(null) // 漫游 key

// 事件定位
const locate = reactive({
  state: false,
  progress: 0,
  infos: null
})


const cameraCtrls = reactive({
  orgDistance: 0, // 初始视距
  scale: 1, // 放大倍数
  mode: 'icon' // 模式
})

// 追踪车辆
const carTracked = computed(() => {

  if (!tracked.value) return null

  const xcar = cars.value.flat().find(item => item.id === tracked.value)

  if (!xcar) {
    return null
  }

  if (xcar.over_miles) {
    tracked.value = null
    return null
  }

  return xcar

})

// 内存校验
const sceneCheck = (key) => {
  
  if (carTracked.value) {
    if (key !== 'track') {
      ElMessage.warning('车辆追踪中，请稍后！')
      return false
    }
  }

  if (roamkey.value) {
    if (key !== 'roam') {
      ElMessage.warning('隧道漫游中，请稍后！')
      return false
    }
  }

  if (locate.state) {
    if (key !== 'locate') {
      ElMessage.warning('事件定位中，请稍后！')
      return false
    }
  }

  return true

}

export {

  cars,
  tracked,
  carTracked,
  cameraCtrls,
  locate,

  bus,
  sceneCheck

}