'use strict';

const {createStore} = require('fluxible/addons');
const debug = require('../utils/DebugConsole')('store:app');

module.exports = createStore({

  storeName: 'AppStore',

  handlers: {
    SET_ROUTE: 'handleSetRoute',
    SET_CURRENT_ACCOUNT: 'handleSetAccount'
  },

  initialize() {
    this.currentRoute = null;
    this.currentAccount = null;
  },

  handleSetRoute(route) {
    debug('handleSetRoute');
    if (!this.currentRoute || route.path !== this.currentRoute.path) {
      this.currentRoute = route;
      this.emitChange();
    }
  },

  handleSetAccount(user) {
    debug('handleSetAccount');
    this.currentAccount = user;
    this.emitChange();
  },

  getState() {
    debug('return application status');
    return {
      route: this.currentRoute,
      account: this.currentAccount
    };
  },

  dehydrate() {
    return this.getState();
  },

  rehydrate(state) {
    this.currentRoute = state.route;
    this.currentAccount = state.account;
  }

});
