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

Model.cards.getArray = function() {
  const out = [];
  let categoryItems;
  for(let categoryID in this._props){

    if(categoryID !== '_VERSION_'){
      categoryItems = this._props[categoryID]
      for(let id in categoryItems){
        out.push(categoryItems[id]);
      }

    }
  }
  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) {
        Model.cards.set(card.category_id+'.'+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)
    ACTION.NAVIGATE.execute( 'Account');
});