import { preferences } from '@kit.ArkData'
import { ExpertData } from '../models/RequestDefaultModel'
import { logger } from '../utils/logger'

export const AUTH_STORE_KEY = 'perfactAuth'

class PerfactAuth {
  store: preferences.Preferences | null = null

  getStore() {
    if (!this.store) {
      this.store = preferences.getPreferencesSync(getContext(), { name: AUTH_STORE_KEY })
    }
    return this.store
  }

  async setUser(phone:string,user: ExpertData) {

    await this.getStore().put(phone, JSON.stringify(user))
    await this.getStore().flush()
  }

  async updateUser(phone:string,user: ExpertData) {

    await this.getStore().put(phone, JSON.stringify(user))
    await this.getStore().flush()
  }

  async delUser(phone:string) {
    await this.getStore().put(phone, '{}')
    await this.getStore().flush()
  }



  getUser(phone:string) {
    // logger.info('个人资料initUser'+this.getStore().getSync(phone,'{}'));
    return JSON.parse(this.getStore().getSync(phone,'{}')+'') as ExpertData
  }
}

export const perfactAuth = new PerfactAuth()