'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _class, _temp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _localStorage = require('../utils/localStorage');

var _styles = require('../styles');

var _styles2 = _interopRequireDefault(_styles);

var _DevTools = require('../containers/DevTools');

var _DevTools2 = _interopRequireDefault(_DevTools);

var _Dispatcher = require('./monitors/Dispatcher');

var _Dispatcher2 = _interopRequireDefault(_Dispatcher);

var _createRemoteStore = require('../store/createRemoteStore');

var _ButtonBar = require('../components/ButtonBar');

var _ButtonBar2 = _interopRequireDefault(_ButtonBar);

var _Instances = require('../components/Instances');

var _Instances2 = _interopRequireDefault(_Instances);

var _MonitorSelector = require('../components/MonitorSelector');

var _MonitorSelector2 = _interopRequireDefault(_MonitorSelector);

var _SyncToggle = require('../components/SyncToggle');

var _SyncToggle2 = _interopRequireDefault(_SyncToggle);

var _TestGenerator = require('../components/TestGenerator');

var _TestGenerator2 = _interopRequireDefault(_TestGenerator);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var App = (_temp = _class = function (_Component) {
  _inherits(App, _Component);

  function App() {
    _classCallCheck(this, App);

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(App).call(this));

    _this.handleInstancesChanged = function (instance, name, toRemove) {
      var instances = _this.state.instances;
      if (toRemove) {
        delete instances[instance];
        _this.store.liftedStore.deleteInstance(instance);
        if (_this.state.instance === instance) {
          (0, _createRemoteStore.updateStoreInstance)(null);
          _this.setState({ instance: null, shouldSync: false, instances: instances });
          return;
        }
      } else {
        instances[instance] = name || instance;
        (0, _createRemoteStore.startMonitoring)(instance);
      }
      _this.setState({ instances: instances });
    };

    _this.handleSelectInstance = function (event, index, instance) {
      (0, _createRemoteStore.updateStoreInstance)(instance);
      _this.setState({ instance: instance, shouldSync: false });
    };

    _this.handleSelectMonitor = function (event, index, value) {
      _this.setState({ monitor: (0, _localStorage.saveToStorage)('select-monitor', value) });
    };

    _this.handleSyncToggle = function () {
      var shouldSync = !_this.state.shouldSync;
      (0, _createRemoteStore.enableSync)(shouldSync);
      _this.setState({ shouldSync: shouldSync });
    };

    _this.openModal = _this.openModal.bind(_this);
    _this.closeModal = _this.closeModal.bind(_this);
    _this.toggleDispatcher = _this.toggleDispatcher.bind(_this);
    _this.toggleSlider = _this.toggleSlider.bind(_this);
    _this.saveSettings = _this.saveSettings.bind(_this);
    _this.clearError = _this.clearError.bind(_this);
    _this.handleError = _this.handleError.bind(_this);
    return _this;
  }

  _createClass(App, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      var _this2 = this;

      this.state = {
        monitor: (0, _localStorage.getFromStorage)('select-monitor') || this.props.selectMonitor || 'default',
        modalIsOpen: false,
        dispatcherIsOpen: false,
        sliderIsOpen: true,
        instances: {},
        instance: null,
        error: null,
        shouldSync: false
      };
      this.socketOptions = (0, _localStorage.getSettings)() || this.props.socketOptions;
      this.store = this.createStore();
      this.testComponent = function (props) {
        return _react2.default.createElement(_TestGenerator2.default, _extends({
          name: _this2.state.instances[_this2.state.instance || _this2.store.liftedStore.getInstance()],
          isRedux: _this2.store.isRedux(),
          testTemplates: _this2.props.testTemplates,
          selectedTemplate: _this2.props.selectedTemplate,
          useCodemirror: _this2.props.useCodemirror
        }, props));
      };
    }
  }, {
    key: 'createStore',
    value: function createStore() {
      return (0, _createRemoteStore.createRemoteStore)(this.socketOptions, this.handleInstancesChanged, this.handleError, this.state.instance);
    }
  }, {
    key: 'saveSettings',
    value: function saveSettings(isLocal, options) {
      this.socketOptions = (0, _localStorage.saveObjToStorage)(!isLocal, ['hostname', 'port', 'secure'], options) || undefined;
      this.store = this.createStore();
      this.closeModal();
    }
  }, {
    key: 'toggleDispatcher',
    value: function toggleDispatcher() {
      this.setState({ dispatcherIsOpen: !this.state.dispatcherIsOpen });
    }
  }, {
    key: 'handleError',
    value: function handleError(error) {
      this.setState({ error: error });
    }
  }, {
    key: 'clearError',
    value: function clearError() {
      this.setState({ error: null });
    }
  }, {
    key: 'toggleSlider',
    value: function toggleSlider() {
      this.setState({ sliderIsOpen: !this.state.sliderIsOpen });
    }
  }, {
    key: 'openModal',
    value: function openModal(content) {
      this.modalContent = content;
      this.setState({ modal: this.modal, modalIsOpen: true });
    }
  }, {
    key: 'closeModal',
    value: function closeModal() {
      this.modalContent = null;
      this.setState({ modalIsOpen: false });
    }
  }, {
    key: 'render',
    value: function render() {
      var monitor = this.state.monitor;

      var key = (this.socketOptions ? this.socketOptions.hostname : '') + this.state.instance;
      return _react2.default.createElement(
        'div',
        { style: _styles2.default.container },
        _react2.default.createElement(
          'div',
          { style: _styles2.default.buttonBar },
          _react2.default.createElement(_MonitorSelector2.default, { selected: this.state.monitor, onSelect: this.handleSelectMonitor }),
          _react2.default.createElement(_Instances2.default, {
            instances: this.state.instances, onSelect: this.handleSelectInstance,
            selected: this.state.instance
          }),
          _react2.default.createElement(_SyncToggle2.default, {
            on: this.state.shouldSync,
            onClick: this.handleSyncToggle,
            style: !this.state.instance && { display: 'none' }
          })
        ),
        _react2.default.createElement(_DevTools2.default, {
          monitor: monitor,
          store: this.store,
          testComponent: this.testComponent,
          key: monitor + '-' + key
        }),
        this.state.sliderIsOpen && _react2.default.createElement(
          'div',
          { style: _styles2.default.sliderMonitor },
          _react2.default.createElement(_DevTools2.default, { monitor: 'SliderMonitor', store: this.store, key: 'Slider-' + key })
        ),
        this.state.dispatcherIsOpen && this.store.liftedStore.getInstance() && _react2.default.createElement(_Dispatcher2.default, {
          store: this.store,
          error: this.state.error,
          clearError: this.clearError,
          key: 'Dispatcher-' + key
        }),
        _react2.default.createElement(_ButtonBar2.default, {
          openModal: this.openModal, closeModal: this.closeModal,
          toggleDispatcher: this.toggleDispatcher,
          dispatcherIsOpen: this.state.dispatcherIsOpen,
          toggleSlider: this.toggleSlider,
          sliderIsOpen: this.state.sliderIsOpen,
          saveSettings: this.saveSettings,
          importState: _createRemoteStore.importState, exportState: _createRemoteStore.exportState,
          socketOptions: this.socketOptions,
          noSettings: this.props.noSettings
        })
      );
    }
  }]);

  return App;
}(_react.Component), _class.propTypes = {
  selectMonitor: _react.PropTypes.string,
  testTemplates: _react.PropTypes.array,
  useCodemirror: _react.PropTypes.bool,
  selectedTemplate: _react.PropTypes.number,
  socketOptions: _react.PropTypes.shape({
    hostname: _react.PropTypes.string,
    port: _react.PropTypes.oneOfType([_react2.default.PropTypes.string, _react2.default.PropTypes.number]),
    autoReconnect: _react.PropTypes.bool,
    secure: _react.PropTypes.bool
  }),
  noSettings: _react.PropTypes.bool
}, _temp);
exports.default = App;
module.exports = exports['default'];