// import Vue from 'vue'
let resultComps = []
const requireComponent = require.context(
  './', // 在当前目录下查找
  true, // 遍历子文件夹
  /\.vue$/ // 正则匹配 以 .vue结尾的文件
)
resultComps = handleData(requireComponent.keys())
function findKey(data) {
  const allkey = []
  const finalData = []
  const obj = {}
  data.forEach(el => {
    const comp = requireComponent(el)
    const item = comp.default
    if (!(item.modelRelationObjs.length === 0 || (item.modelRelationObjs.length === 1 && item.modelRelationObjs[0] === ''))) {
      item.modelRelationObjs.forEach(key => {
        if (allkey.indexOf(key) <= -1) {
          allkey.push(key)
          finalData.push({
            [key]: []
          })
          obj[key] = []
        }
      })
    }
  })
  return { finalData, allkey, obj }
}
function handleData(data) {
  if (!data) return
  const { finalData, obj } = findKey(data)
  data.forEach(el => {
    const comp = requireComponent(el)
    const item = comp.default
    const mfileName = el.split('/')[el.split('/').length - 1]
    // Vue.component(mfileName.slice(0, -4), comp.default)
    if (item.modelRelationObjs.length === 0 || (item.modelRelationObjs.length === 1 && item.modelRelationObjs[0] === '')) {
      if (!obj['commonComponent']) {
        obj['commonComponent'] = []
      }
      obj['commonComponent'].push({
        name: mfileName.slice(0, -4),
        displayName: item.displayName,
        modelRelationObjs: item.modelRelationObjs,
        codePath: el.slice(1)
      })
    } else {
      item.modelRelationObjs.forEach(key => {
        const index = finalData.findIndex(y => y[key])
        if (index > -1) {
          finalData[index][key].push({
            name: mfileName.slice(0, -4),
            displayName: item.displayName,
            modelRelationObjs: item.modelRelationObjs,
            codePath: el.slice(1)
          })
          obj[key] = finalData[index][key]
        }
      })
    }
  })
  return obj
}
export default resultComps