import { iOutLet, MyAppDatabase } from ".";

export default class OutletDBService {
  static instance: OutletDBService;
  outlet: Dexie.Table<iOutLet, string>;
  static getInstance() {
    if (!OutletDBService.instance)
      OutletDBService.instance = new OutletDBService();
    return OutletDBService.instance;
  }

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

  add(outlet: iOutLet) {
    return this.outlet.put(outlet);
  }

  update(outlet: Partial<Omit<iOutLet, "id">> & Pick<iOutLet, "posId">) {
    return this.outlet
      .filter((i) => i.posId === outlet.posId)
      .modify((i) => {
        outlet.name && (i.name = outlet.name);
        outlet.isDeleted && (i.isDeleted = outlet.isDeleted);
      });
  }

  delete(posId: string) {
    return this.outlet.delete(posId);
  }

  get(posId: string) {
    return this.outlet.filter((i) => i.posId === posId).first();
  }
}
