import user from '../../user/index';

import router from './router';

import targetFunction from './function';

export default {
    methods:{
        trigger: function (item,login=false) {
            if (item && item.trigger) {
                let resultData;
                if (typeof item.trigger === 'string' || (item.trigger.name || item.trigger.path || item.trigger.params)) {
                    resultData = {
                        target: item.trigger.target || item.trigger,
                        mode: 'router'
                    };
                }
                else {
                    resultData = item.trigger;
                }
                let resultLogin = resultData.login !== undefined ? resultData.login : login;
                if (resultLogin && !user.isLogin()) {
                    return this.$router.navigateTo('login');
                }
                if (this.triggers[resultData.mode]) {
                    return this.triggers[resultData.mode].trigger(resultData.target, this, item);
                }
            }
        }
    },
    created(){
        this.triggers = {
            router,
            function:targetFunction
        }
    }
}