import CateView from './components/CateView.vue'
import Index from './components/Index.vue'
import Issues from './components/Issues.vue'

const langs = ['zh']

const genRouteMap = (routeMap, lang, isChild) => routeMap.reduce((res, item) => {
    if (typeof Object.assign != 'function') {
        Object.assign = function(target) {
          'use strict';
          if (target == null) {
            throw new TypeError('Cannot convert undefined or null to object');
          }
          target = Object(target);
          for (var index = 1; index < arguments.length; index++) {
            var source = arguments[index];
            if (source != null) {
              for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                  target[key] = source[key];
                }
              }
            }
          }
          return target;
        };
      }
    const route = {
        path: isChild ? item.path : `/${lang}/${item.path}`,
        name: item.name[lang],
        component: typeof item.component === 'string' ? () =>import (`./md/${lang}/${item.component}.md`) : item.component,
        meta: Object.assign({ lang }, item.meta),
        children: genRouteMap(item.children || [], lang, true)
    }
    res.push(route)
    return res
}, [])

const routeMap = [{
        // 首页
        path: 'index',
        name: {
            'zh': '文档'
        },
        component: 'index',
        meta: {
            hidden: true
        }
    },
    {
        // 开始
        path: 'start',
        component: CateView,
        name: {
            zh: '开始'
        },
        children: [{
                path: 'installation',
                component: 'start-installation',
                name: {
                    zh: '安装'
                }
            },
            {
                path: 'usage',
                component: 'start-usage',
                name: {
                    zh: '快速上手'
                }
            },
            {
                path: 'base',
                component: 'start-base',
                name: {
                    zh: '基础'
                }
            }
        ]
    },
    {
        path: 'map',
        name: {
            zh: '地图'
        },
        component: CateView,
        children: [{
                path: 'map',
                component: 'sl-map',
                name: {
                    zh: '地图容器'
                }
            },
            {
                path: 'view',
                component: 'sl-view',
                name: {
                    zh: '地图视图'
                }
            }
        ]
    }
]

export default [{
        path: '/',
        component: Index
    },
    {
        path: '/issues',
        name: 'ISSUE',
        component: Issues,
        meta: {
            hidden: true
        }
    },
    ...langs.reduce((map, lang) => map.concat(genRouteMap(routeMap, lang)), [])
]