import {inject, Injectable} from '@angular/core';
import {CookieService} from 'ngx-cookie-service';
import {KeyringAPI} from '@chatenium/chatenium-sdk/storage/keyring';

@Injectable({
  providedIn: 'root',
})
export class Keyring {
  cookieService = inject(CookieService)

  getApi(): KeyringAPI {
    return {
      set: (key, value) => {
        this.cookieService.set(key, value)
      },
      get: key => {
        return new Promise(r => r(this.cookieService.get(key)))
      },
      delete: key => {
        return this.cookieService.delete(key)
      },
      getAll: () => {
        return new Promise(r => {
          const cookies = this.cookieService.getAll()
          let resp: string[] = []
          for (const key in cookies) {
            resp.push(key)
          }
          r(resp)
        })
      }
    }
  }
}
