import { AsyncAuthAjax } from "./Ajax";
import { API } from "../dict/Consts";

cards.getArray = function() {
  const out = [];
  for(let key in this._props){
    if(key !== 'commit'){
      out.push(this._props[key]);
    }
  }
  return out;
};
store.sub('navigation.current', (val)=>tmpStore.set('navigation.current', val));

store.sub(['navigation.current', 'loaded.cards'], async function(page, loaded) {
  if(page !== 'Login' && !loaded){
    //try{
      const result = await AsyncAuthAjax.get( API.ENDPOINTS.GET_USER_NEW_CARDS() );

      result.forEach(function(card) {
        cards.set(card.id+'', Object.assign({},card, {seen: false}));
      });
      store.set('loaded.cards', true);

    /*}catch(e){
      console.error(e)
    }*/

  }
});

tmpStore.sub([
  'isMobile', 'navigation.current', 'loaded'
], (is, page, loaded)=>{
  if(!is && page === 'Profile' && loaded)
    store.set('navigation.current', 'Account');
});