import type { StorageInterface } from './StorageInterface'

export class LocalStorageService implements StorageInterface {
  async saveUserDataText(fileName: string, content: any) {
    localStorage.setItem(`net.xintanalabs.domino.${fileName}`, content)
  }

  async saveUserDataJson(fileName: string, content: any) {
    localStorage.setItem(`net.xintanalabs.domino.${fileName}`, JSON.stringify(content))
  }

  async saveConfigData(content: any) {
    localStorage.setItem('net.xintanalabs.domino.config', JSON.stringify(content))
  }

  async readUserDataText(fileName: string) {
    const content: string | null = localStorage.getItem(`net.xintanalabs.domino.${fileName}`)
    return content || ''
  }

  async readUserDataJson(fileName: string) {
    const content: string | null = localStorage.getItem(`net.xintanalabs.domino.${fileName}`)
    return JSON.parse(content || 'null')
  }

  async readConfigData() {
    const content: string | null = localStorage.getItem('net.xintanalabs.domino.config')
    return JSON.parse(content || '{}')
  }

  async deleteUserDataText(fileName: string) {
    localStorage.removeItem(`net.xintanalabs.domino.${fileName}`)
  }

  async deleteUserDataJson(fileName: string) {
    localStorage.removeItem(`net.xintanalabs.domino.${fileName}`)
  }
}
