import { preferences } from '@kit.ArkData'

class PreferenceStore {
  KEY = 'gdxz_config'
  store: preferences.Preferences | null = null

  getStore() {
    if (!this.store) {
      this.store = preferences.getPreferencesSync(getContext(), { name: this.KEY })
    }
    return this.store
  }
  setItemString(keyword: string,value:string) {
    this.getStore().putSync(keyword, value)
    this.getStore().flush()
  }
  setItemBoolean(keyword: string,value:boolean) {
    this.getStore().putSync(keyword, value)
    this.getStore().flush()
  }
  delItem(keyword: string) {
    this.getStore().deleteSync(keyword)
    this.getStore().flush()
  }
  getItemString(keyword: string) {
    return this.getStore().getSync(keyword,'')
  }
  getItemBooleanT(keyword: string):boolean {
    return this.getStore().getSync(keyword,true) as boolean
  }
  getItemBooleanF(keyword: string):boolean {
    return this.getStore().getSync(keyword,false) as boolean
  }
  clear() {
    this.getStore().clearSync()
  }

  getAll() {
    const obj = this.getStore().getAllSync()
    return Object.keys(obj)
  }
}

export const preferenceStore = new PreferenceStore()