import cacheMixin from '@/components/Cache/mx-cache-mixin.js'

export default {
  mixins: [cacheMixin],
  data() {
    return {
      classTree: [],
      classTreeReady: false
    }
  },
  computed: {
    standardClassTree() {
      return this.classTree.map(g => ({
        label: g.name,
        value: g.grade,
        children: g.classList.map(c => ({
          label: c.className,
          value: c.classId
        }))
      }))
    }
  },
  beforeMount() {
    this.loadTranslateClassTree()
  },
  methods: {
    async loadTranslateClassTree() {
      this.classTree = await this.getClassTreeByCache()
      this.classTreeReady = true
      this.translateClassTreeReady()
    },
    translateClassTreeReady() {
      //to be overrided for non-computed needs
    },
    getFullClassName(classId) {
      if (!classId) return ''
      const nodes = []
      this.classTree.forEach(grade => {
        const match = grade.classList.find(c => c.classId == classId)
        if (match) {
          nodes.push(grade.name)
          nodes.push(match.className)
        }
      })
      return nodes.join('/')
    },
    getClassName(classId) {
      if (!classId) return ''
      const nodes = []
      this.classTree.forEach(grade => {
        const match = grade.classList.find(c => c.classId == classId)
        if (match) nodes.push(match.className)
      })
      return nodes.join('/')
    }
  }
}
