import { SERVER_PORT, SOCKET_MESSAGE_TYPE } from '../../shared-constants';

var sendMessage = function sendMessage() {
  console.warn('Connection to server was not established');
};

export var createConnection = function createConnection(onMessage) {
  var route = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "ws://127.0.0.1:".concat(SERVER_PORT, "/");
  var ws = new WebSocket(route);

  ws.onopen = function () {
    return console.log("Client connected to ".concat(route));
  };

  ws.onerror = function (error) {
    return console.log("Connection error: ".concat(error));
  };

  ws.onmessage = function (event) {
    onMessage(JSON.parse(event.data));
  };

  sendMessage = function sendMessage(msg) {
    return ws.send(msg);
  };
};
export var requestFetchFile = function requestFetchFile(data, namespace) {
  sendMessage(JSON.stringify({
    type: SOCKET_MESSAGE_TYPE.CLIENT_REQUEST_FETCH_FILE,
    namespace: namespace,
    data: data
  }));
};
export var openFileInEditor = function openFileInEditor(data, namespace) {
  sendMessage(JSON.stringify({
    type: SOCKET_MESSAGE_TYPE.CLIENT_OPEN_FILE_IN_EDITOR,
    namespace: namespace,
    data: data
  }));
};