/* global window */
import 'can-define-stream';
import canStream from 'can-stream';
import DefineMap from 'can-define/map/';
import route from 'can-route';
// import 'can-route-pushstate';
import Session from 'bitcentive/models/session';
// import 'bitcentive/models/fixtures/';

// viewmodel debugging
import viewModel from 'can-view-model';
window.viewModel = viewModel;

var pages = {
  home: 'public',
  dashboard: 'private',
  contributors: 'private'
};

const AppViewModel = DefineMap.extend({

  /**
   * By default, viewModel attributes will not be serialized into the URL as
   * route attributes.
   */
  '*': {
    serialize: false
  },

  // Once https://github.com/canjs/can-define-stream/issues/15 is fixed, 
  // we can move the initialSession into the session `value` function and
  // replace the `.initialSession` stream with the setStream.
  initialSession: {
    value () {
      Session.get().then(session => {
        this.initialSession = session;
      })
      .catch(err => console.log(err));
    }
  },

  /**
   * Use Session.get() to see if there's a valid JWT. If one exists, 
   * a new Session will be created.
   */
  session: {
    // value () {},
    stream () {
      return canStream.toStream(Session, 'created')
        .merge(canStream.toStream(Session, 'destroyed'))
        .map(event => event.type !== 'destroyed' ? event.args[0] : undefined)
        .merge(this.stream('.initialSession'));
    }
  },

  /**
   * Determines which page-level component is displayed.
   */
  page: {
    serialize: true,
    get (page, setPage) {
      if (page === 'logout') {
        return this.logout();
      }

      if (pages[page]) {
        // Logic for authenticated users.
        if (this.session) {
          if (page === 'home') {
            page = 'dashboard';
          }
        // Logic for non-authenticated users.
        } else {
          if (pages[page] !== 'public') {
            page = 'home';
          }
        }
      // 404 if the page isn't in the list of pages.
      } else {
        page = 'four-oh-four';
      }
      return page;
    }
  },

  /**
   * The `title` attribute is used in index.stache as the HTML title.
   */
  title: {
    value: 'Bitcentive'
  }
});

route('{page}', {page: 'home'});

export default AppViewModel;

@@ -1546,63 +1546,29 @@
     
-get (page, setPage) %7B%0A      if (page === 'logout'
+stream (setStream
 ) %7B%0A
-  
     
@@ -1580,98 +1580,71 @@
 urn 
-this.logout();%0A      %7D%0A%0A      if (pages%5Bpage%5D) %7B%0A        // Logic for authenticated users.
+setStream.combine(this.stream('.session'), (page, session) =%3E %7B
 %0A   
@@ -1652,21 +1652,16 @@
     if (
-this.
 session)
@@ -1743,54 +1743,9 @@
    %7D
-%0A        // Logic for non-authenticated users.
+ 
 %0A   
@@ -1788,19 +1788,20 @@
 ge%5D 
-!
+=
 == 'p
-ublic
+rivate
 ') %7B
@@ -1860,70 +1860,32 @@
     
-// 404 if the page isn't in the list of pages.%0A      %7D else
+  if (!pages%5Bpage%5D)
  %7B%0A
+  
     
@@ -1913,26 +1913,30 @@
 our';%0A      
-%7D%0A
+  %7D%0A  
       return
@@ -1942,16 +1942,26 @@
 n page;%0A
+      %7D);%0A
     %7D%0A  

Use stream & simplify page.