import _Navigation from './_Navigation.js';
import _User from './_User.js';

var MenuItem = function(){
return { view: function(vnode){
  var menuitem = vnode.attrs.menuitem;
  var attrs = {}
  if(menuitem.component !== null || menuitem.run !== null){ // note to self for continuation: make a proper onclick handler to both navigateTo and run the 'run', or either one if only one is provided
    attrs.onclick = function(){
      if(menuitem.component !== null){ _Navigation.navigateTo(menuitem.component,menuitem.attrs); }
      if(menuitem.run !== null){ menuitem.run(); }
      }
    }
  return m("a.btn.btn-link", attrs, [
          menuitem.label,
          m('.list',
            menuitem.children.map(function(mi){
              if(!menuitem.restricted || _User.isLoggedIn()){
                return m(MenuItem, {menuitem: mi});
                }
              }))
          ]);
  }
};
};

export default MenuItem;