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

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

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

  save(list: iContact2[]) {
    return this.contactPerson.bulkAdd(list)
  }

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

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