// noinspection JSIgnoredPromiseFromCall
export default {
  data() {
    return {
      checkedList: [],
      formedMajors: {},
      majorTree: []
    }
  },
  computed: {
    majorTreeChildren() {
      // extend majorTree with children property. use name as key, and children as value.
      // put it here to avoid re-compute and re-build big-data instead of in InjectionMixin.
      const result = {}
      this.majorTree.forEach(category => {
        category.children.forEach(group => {
          result[group.name] = group.children.map(item => item.name)
        })
      })
      return result
    },
    searchingMajors() {
      // below is a structure of searchingMajors, waiting for override.
      return {
        // top search condition checked major list, for search result major highlight mark.
        checkedList: this.checkedList,
        majorTreeChildren: this.majorTreeChildren,
        // Key: majorId, Value: currentSearching copy while major is selected.
        formedMajors: this.formedMajors
      }
    }
  },
  provide() {
    return {
      getMajorTree: () => this.majorTree,
      fetchSearchingMajors: () => this.searchingMajors,
      updateCheckedList: this.updateCheckedList,
      snapshotSearchingMajorWhenApply: this.snapshotSearchingMajorWhenApply,
      isSearchingMajorFired: this.isSearchingMajorFired,
      isFormedMajorFired: this.isFormedMajorFired,
      ensureMajorFullTree: this.ensureMajorFullTree
    }
  },
  methods: {
    updateCheckedList(checkedList) {
      this.checkedList = checkedList
    },
    snapshotSearchingMajorWhenApply(major) {
      // keep currentSearching if major selected, remove if not.
      if (major.selected) {
        if (this.isSearchingMajorFired(major)) {
          // make a copy of currentSearching, and keep it.
          this.$set(this.formedMajors, major.id, [...this.checkedList])
          return
        }
      }
      // remove it if exists.
      this.$delete(this.formedMajors, major.id)
    },
    isSearchingMajorFired(major) {
      return this.checkedList.includes(major.marjorName) ||
        this.checkedList.some(item => this.majorTreeChildren[item]?.includes(major.marjorName))
    },
    isFormedMajorFired(major) {
      return this.formedMajors[major.id]?.includes(major.marjorName) ||
        this.formedMajors[major.id]?.some(item => this.majorTreeChildren[item]?.includes(major.marjorName))
    },
    async ensureMajorFullTree(batch) {
      const params = {level: 3, batch}
      this.majorTree = await this.$store.cache.dispatch('cachedData/getMajorFullTree', params)
    },
    resolveFormedMajorsFromSavedData(data) {
      const results = {}
      const highlighted = data.detail?.batch?.highlightMajorIds || []
      data.detail?.batch?.wishes?.forEach(group => {
        group.marjors.forEach(major => {
          // Do not use ===, id is not concerned with type of string or number
          if (highlighted.some(id => id == major.id)) {
            results[major.id] = [major.name]
          }
        })
      })
      return results
    }
  }
}
