import Vue from 'vue';
import VueI18n from 'vue-i18n';
import VueRouter from 'vue-router';
import App from './App.vue';
import messages from './lang';


if (typeof customMessages !== 'undefined')
{
  _.merge(messages, customMessages);

  if (customMessages.hasOwnProperty('allLocales'))
  {
    for (var key in messages)
    {
      if (key !== 'allLocales' && messages.hasOwnProperty(key))
        _.merge(messages[key], customMessages.allLocales);
    }
  }
}


Vue.use(VueI18n);
Vue.use(VueRouter);

const i18n = new VueI18n({
  locale: navigator.language.split('-')[0],
  fallbackLocale: 'en',
  messages: messages
});


const Code = () => import('./route/Code.vue');


const router = new VueRouter({
  routes: [
    { path: '/', component: () => import('./route/Landing.vue'),
      children: [
        { path: 'c/:codeParam', component: Code, props: true },
        { path: 'u/:codeParam', component: () => import('./route/Upload.vue'), props: true },
        { path: '', component: Code }
      ]
    },
    { path: '/admin', component: () => import('./route/admin/Landing.vue'),
      children: [
        { path: 'uploads', component: () => import('./route/admin/Uploads.vue') },
        { path: 'codes', component: () => import('./route/admin/Codes.vue') },
        { path: 'profile', component: () => import('./route/admin/Profile.vue') },
        { path: 'users', component: () => import('./route/admin/Users.vue') },
        { path: '', component: () => import('./route/admin/Login.vue') }
      ]
    },
    { path: '*', redirect: '/' }
  ]
});

new Vue({
  el: '#app',
  i18n,
  router,
  render: h => h(App)
});