import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import React from 'react';
import { connect } from 'react-redux';
import { createConnection } from './connection';
import { SOCKET_MESSAGE_TYPE } from '../constants';
import { setInitialSourceData, setChangedSourceData, setPredefinedState, updateFiles } from './actions';

var DataBusContainer =
/*#__PURE__*/
function (_React$Component) {
  _inherits(DataBusContainer, _React$Component);

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

    return _possibleConstructorReturn(this, _getPrototypeOf(DataBusContainer).apply(this, arguments));
  }

  _createClass(DataBusContainer, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var standalone = this.props.standalone;

      if (standalone) {
        return this.setupStandalone();
      }

      return this.setupLocal();
    }
  }, {
    key: "setupStandalone",
    value: function setupStandalone() {
      var _this$props = this.props,
          setPredefinedState = _this$props.setPredefinedState,
          predefinedState = _this$props.predefinedState;
      predefinedState && setPredefinedState(predefinedState);
    }
  }, {
    key: "setupLocal",
    value: function setupLocal() {
      createConnection(this.onSocketMessage.bind(this));
    }
  }, {
    key: "onSocketMessage",
    value: function onSocketMessage(message) {
      var type = message.type,
          data = message.data,
          namespace = message.namespace;
      console.log("Message '".concat(type, "' from server received"));

      switch (type) {
        case SOCKET_MESSAGE_TYPE.SOURCE_INIT_SOURCE_FILES_SYNC:
          return this.props.setInitialSourceData(data, namespace);

        case SOCKET_MESSAGE_TYPE.SOURCE_UPDATE_SOURCE_FILE_SYNC:
          return this.props.setChangedSourceData(data, namespace);

        case SOCKET_MESSAGE_TYPE.SOURCE_RESPONSE_FETCH_FILE:
          return this.props.updateFiles(data, namespace);

        default:
          console.warn("Unhandled message in client: ".concat(JSON.stringify(message)));
          break;
      }
    }
  }, {
    key: "render",
    value: function render() {
      return React.createElement("div", {
        className: "DataBus-container"
      });
    }
  }]);

  return DataBusContainer;
}(React.Component);

var mapDispatchToProps = {
  setInitialSourceData: setInitialSourceData,
  setChangedSourceData: setChangedSourceData,
  setPredefinedState: setPredefinedState,
  updateFiles: updateFiles
};
export default connect(null, mapDispatchToProps)(DataBusContainer);