import { iUserinfo } from '@/service/UserService/types'
import { MyAppDatabase } from './index'

export default class UserInfoDBService {
  static instance: UserInfoDBService
  private userInfo: Dexie.Table<iUserinfo, number>
  static getInstance() {
    if (!UserInfoDBService.instance) {
      UserInfoDBService.instance = new UserInfoDBService()
    }
    return UserInfoDBService.instance
  }

  constructor() {
    const db = new MyAppDatabase()
    this.userInfo = db.userInfo
  }

  save(list: iUserinfo[]) {
    return this.userInfo.bulkAdd(list)
  }

  async findByList(addressList: string[]) {
    const list = await this.userInfo
      .filter((i) => {
        return addressList.includes(i.addr)
      })
      .toArray()

    const notFoundList = addressList.filter(
      (i) => list.findIndex((item) => item?.addr === i) === -1,
    )
    return {
      foundList: list,
      notFoundList,
    }
  }
}
