function connectionsEnabled(store) {
  return !!store && store.autoConnect !== false;
}

function disconnectConnections(options) {
  const state = options || {};
  const peers = state.peers || {};
  const directConnecting = state.directConnecting || {};
  const clearPeer = state.clearPeer || (() => {});
  let disconnected = 0;

  Object.keys(peers).forEach(hostId => {
    const peer = peers[hostId];
    clearPeer(peer);
    if (peer && peer.socket && !peer.socket.destroyed) {
      peer.socket.destroy();
      disconnected += 1;
    }
  });

  Object.keys(directConnecting).forEach(address => {
    const socket = directConnecting[address];
    if (socket && !socket.destroyed) {
      socket.destroy();
      disconnected += 1;
    }
    delete directConnecting[address];
  });

  return disconnected;
}

module.exports = {
  connectionsEnabled,
  disconnectConnections,
};
