import {Store} from 'mesosphere-shared-reactjs';

var AppDispatcher = require('../events/AppDispatcher');
import ActionTypes from '../constants/ActionTypes';
import EventTypes from '../constants/EventTypes';
var GetSetMixin = require('../mixins/GetSetMixin');

var SidebarStore = Store.createStore({
  storeID: 'sidebar',

  mixins: [GetSetMixin],

  init: function () {
    this.set({
      isOpen: false,
      versions: {}
    });
  },

  emitChange: function (eventName) {
    this.emit(eventName);
  },

  addChangeListener: function (eventName, callback) {
    this.on(eventName, callback);
  },

  removeChangeListener: function (eventName, callback) {
    this.removeListener(eventName, callback);
  },

  dispatcherIndex: AppDispatcher.register(function (payload) {
    var source = payload.source;
    if (source !== ActionTypes.SIDEBAR_ACTION) {
      return false;
    }

    var action = payload.action;

    switch (action.type) {
      case ActionTypes.REQUEST_SIDEBAR_CLOSE:
      case ActionTypes.REQUEST_SIDEBAR_OPEN:
        var oldIsOpen = SidebarStore.get('isOpen');
        var isOpen = action.data;

        // only emitting on change
        if (oldIsOpen !== isOpen) {
          SidebarStore.set({isOpen});
          SidebarStore.emitChange(EventTypes.SIDEBAR_CHANGE);
        }
        break;
      case ActionTypes.REQUEST_CLI_INSTRUCTIONS:
        SidebarStore.emitChange(EventTypes.SHOW_CLI_INSTRUCTIONS);
        break;
      case ActionTypes.REQUEST_VERSIONS_SUCCESS:
        SidebarStore.set({versions: action.data});
        SidebarStore.emitChange(EventTypes.SHOW_VERSIONS_SUCCESS);
        break;
      case ActionTypes.REQUEST_VERSIONS_ERROR:
        SidebarStore.emitChange(EventTypes.SHOW_VERSIONS_ERROR);
        break;
      case ActionTypes.SIDEBAR_WIDTH_CHANGE:
        SidebarStore.emitChange(EventTypes.SIDEBAR_WIDTH_CHANGE);
        break;
    }

    return true;
  })

});

module.exports = SidebarStore;

import BaseStore from './BaseStore';
import AppDispatcher from '../events/AppDispatcher';
import ActionTypes from '../constants/ActionTypes';
import EventTypes from '../constants/EventTypes';

class SidebarStore extends BaseStore {
  constructor() {
    super(...arguments);

    this.dispatcherIndex = AppDispatcher.register((payload) => {
      var source = payload.source;
      if (source !== ActionTypes.SIDEBAR_ACTION) {
        return false;
      }

      var action = payload.action;

      switch (action.type) {
        case ActionTypes.REQUEST_SIDEBAR_CLOSE:
        case ActionTypes.REQUEST_SIDEBAR_OPEN:
          var oldIsOpen = this.get('isOpen');
          var isOpen = action.data;

          // only emitting on change
          if (oldIsOpen !== isOpen) {
            this.set({isOpen});
            this.emitChange(EventTypes.SIDEBAR_CHANGE);
          }
          break;
        case ActionTypes.REQUEST_CLI_INSTRUCTIONS:
          this.emitChange(EventTypes.SHOW_CLI_INSTRUCTIONS);
          break;
        case ActionTypes.REQUEST_VERSIONS_SUCCESS:
          this.set({versions: action.data});
          this.emitChange(EventTypes.SHOW_VERSIONS_SUCCESS);
          break;
        case ActionTypes.REQUEST_VERSIONS_ERROR:
          this.emitChange(EventTypes.SHOW_VERSIONS_ERROR);
          break;
        case ActionTypes.SIDEBAR_WIDTH_CHANGE:
          this.emitChange(EventTypes.SIDEBAR_WIDTH_CHANGE);
          break;
      }

      return true;
    });
  }

  init() {
    this.set({
      isOpen: false,
      versions: {}
    });
  }

  // TODO: DCOS-7430 - Remove emitChange method
  emitChange(eventName) {
    this.emit(eventName);
  }

  addChangeListener(eventName, callback) {
    this.on(eventName, callback);
  }

  removeChangeListener(eventName, callback) {
    this.removeListener(eventName, callback);
  }

  get storeID() {
    return 'sidebar';
  }

}

module.exports = new SidebarStore();

Update SidebarStore to es6 class