
/** Dependencies */
import Vue from 'vue'
import store from './vuex/store';

window.route = require('./general/functions/router');

/** Guards */
import guards from './general/mixins/guards';

/** Validation **/
import vuelidate from 'vuelidate';

/** Input Mask **/
import VueTheMask from 'vue-the-mask';

/** Vue Filter **/
import filters from './general/filters';

/** Mixins */
import request from './general/mixins/request';
import crudHandler from './general/mixins/crudHandler';

/** Directives */
import closable from './general/directives/closable';

import Avatar from 'vue-avatar';

/** Application Injections */
Vue.use(vuelidate);
Vue.use(VueTheMask);
Vue.use(filters);

Vue.directive('closable', closable);
Vue.mixin({
    methods: {
        route: route
    },
    mixins: [request, crudHandler]
    });

/** Components Registrations */
Vue.component(Avatar);

const files = require.context('./', true, /\.vue$/i);
files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default));

const app = new Vue({
    el: '#app',
    store,
    created(){
        this.routesGuard();
        if (!this.$store.getters.isAdmin && !('company_marking' in this.$store.getters.getDecodedAccessToken.user) && this.isProtectedRoute() && !this.isWithTokenRoute()) {
            this.$store.dispatch('crudRequest', {endpoint: this.route('api.account.authentication.refresh'), method: 'get'}).then(response => {
                let success = response.ok;
                response.json().then(response => {

                    if(!success){return;}

                    this.$store.dispatch('userAuthentication', {access_token: response.payload.refresh_token, redirect_url: window.location.href});

                });
            })

        }
    },
    methods: {
        route: route
    },
    mixins: [guards]
});
