import Vue from 'vue'
import { get as getDictDetail } from '@/api/system/dictDetail'

export default class Dict {
  constructor(dict) {
    this.dict = dict
  }

  async init(names, completeCallback) {
    if (names === undefined || names === null) {
      throw new Error('need Dict names')
    }
    const ps = []
    names.forEach((n) => {
      Vue.set(this.dict.dict, n, {})
      Vue.set(this.dict.label, n, {})
      Vue.set(this.dict, n, [])
      ps.push(
        getDictDetail(n).then((res) => {
          this.dict[n].splice(0, 0, ...res.data.content)
          res.data.content.forEach((d) => {
            Vue.set(this.dict.dict[n], d.dictValue, d)
            Vue.set(this.dict.label[n], d.dictKey, d.dictValue)
          })
        })
      )
    })
    await Promise.all(ps)
    completeCallback()
  }
}
