/* eslint-disable no-empty */
export class Searchs {
  saveKey = 'searchs'

  initSearch = {
    form: {},
    page: {},
  }

  searchs = {}

  constructor(key) {
    this.key = key
    this.searchs = this.parse()
  }

  save() {
    localStorage.setItem(this.saveKey, JSON.stringify(this.searchs))
  }

  parse() {
    let json = {...initSearch}
    try {
      const val = localStorage.getItem(this.saveKey)
      json = JSON.parse(val) || json
    } catch (error) {}
    return json
  }

  get(key) {
    const k = (key || this.key)
    if (!this.searchs[k]) {
      this.searchs[k] = {...initSearch}
    }
    return this.searchs[k]
  }

  remove(type) {
    if (this.searchs && this.searchs[this.key]) {
      type ? this.searchs[this.key][type] : this.searchs[this.key]
      this.save()
    }
    return this.searchs
  }

  getSearchs() {
    return this.searchs
  }

  removeByKey(key) {
    delete this.searchs[key]
    this.save()
    return this.searchs
  }

  removeAll() {
    this.searchs = {}
    localStorage.setItem(this.saveKey, JSON.stringify(this.searchs))
    return this.searchs
  }

  update(data, key, type) {
    this.searchs = this.parse()
    const k = (key || this.key)
    if (!this.searchs[k]) {
      this.searchs[k] = {...initSearch}
    }

    if (type) {
      this.searchs[k][type] = data
    } else {
      this.searchs[k] = data
    }
    this.save()
    return this.searchs
  }
}

const initSearch = {
  form: {},
  page: {},
}


