import {MenuType} from "@/module/menu";
import HttpApi from "@/api";
import {Vue} from "vue-property-decorator";

interface MenuOption {
  title: string,
  icon: string,
  children: MenuOption[],
  type: string,

  [key: string]: any
}

const config = require('../../../public/config/menu.json')
const menu: MenuType[] = config.menu
const menuList: MenuType[] = []

function analysisMenu(menus: MenuType[]) {
  for (let i = 0; i < menus.length; i++) {
    const menuItem = menus[i]
    if (menuItem.open) {
      if (!menuItem.children.length) {
        // if (menuItem.nameEn === 'My Fleets' && !Vue.prototype.$myFleet.length) {
        //   continue
        // }
        menuList.push(menuItem)
      } else {
        menuList.push(analysisMenuChildren(menuItem))
      }
    }

  }
}

function analysisMenuChildren(menu: MenuType) {
  const menuTemp: MenuType = JSON.parse(JSON.stringify(menu));
  menuTemp.children = []
  for (let child of menu.children) {
    if (child.open) {
      if (!child.children.length) {
        menuTemp.children.push(child)
      } else {
        menuTemp.children.push(analysisMenuChildren(child))
      }
    }
  }
  return menuTemp
}

analysisMenu(menu)

HttpApi.getAreaMenuList({}).then((res: any) => {
  if (res) {
    const tempMenuAll: MenuOption[] = []
    res.forEach((item: any, index1: number) => {
      const tempData: MenuOption = {
        title: item.name,
        id: item.id,
        index: 'area-' + index1,
        icon: '',
        children: [],
        type: 'AreaListTemp',
        hasMini: false
      }
      if (item.tsCustomAreaList) {
        item.tsCustomAreaList.forEach((res: any, index: any) => {
          const tempData2: MenuOption = {
            title: '',
            icon: '',
            children: [],
            type: 'AreaListTemp',
            hasMini: false
          }
          tempData2.title = res.name
          tempData2.area = res.area
          tempData2.cLat = res.cLat
          tempData2.cLon = res.cLon
          tempData2.cagid = res.cagid
          tempData2.check = res.check
          tempData2.id = res.id
          tempData2.useType = res.useType
          tempData2.index = 'area-' + index1 + '-' + index
          tempData.children.push(tempData2)
        })
      }
      tempMenuAll.push(tempData)
    })

    menuList.forEach((item: any) => {
      if (item.title === '区域列表') {
        item.children = tempMenuAll
      }
    })
  }
})
export {menuList}
