import { defineStore } from 'pinia'

/**
 * 扫码相关的仓库
 */
export const useScanStore = defineStore(
  'scan',
  () => {
    // 车辆扫码结果列表
    const scanCarNumberList = ref<{
      IMEI: string
      SN: string
    }[]>([])

    // 添加扫码结果
    function addScanCarNumber(item: { IMEI: string, SN: string }) {
      const index = scanCarNumberList.value.findIndex(
        i => i.IMEI === item.IMEI || i.SN === item.SN,
      )
      if (index === -1) {
        scanCarNumberList.value.push(item)
      }
    }

    // 删除
    function removeScanCarNumber(item: { IMEI: string, SN: string }) {
      const index = scanCarNumberList.value.findIndex(
        i => i.IMEI === item.IMEI || i.SN === item.SN,
      )
      if (index !== -1) {
        scanCarNumberList.value.splice(index, 1)
      }
    }
    //   清除扫码结果
    function clearScanCarNumber() {
      scanCarNumberList.value = []
    }

    return {
      scanCarNumberList,

      // 方法
      addScanCarNumber,
      clearScanCarNumber,
      removeScanCarNumber,
    }
  },
)
