import { defineStore } from 'pinia'

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

    // 初始化扫码结果列表
    function initScanCarNumberList() {
      scanCarNumberList.value = []
    }

    // 添加扫码结果
    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 clearScanCarNumber() {
      scanCarNumberList.value = []
    }

    return {
      scanCarNumberList,

      // 方法
      initScanCarNumberList,
      addScanCarNumber,
      clearScanCarNumber,
    }
  },
  {
    persist: true,
  },
)
