Change the visibility of a layer given in the action.
@param {Object} state Current state.
@param {Object} action Action to handle.

@returns {Object} The new state.
function setVisibility(state, action) {
  let updated = false;
  const updated_layers = [];

  for (let i = 0, ii = state.layers.length; i < ii; i++) {
    const layer = state.layers[i];
    if (layer.id === action.layerId) {
      updated_layers.push({
        ...layer,
        layout: {
          ...layer.layout,
          visibility: action.visibility,
        },
      });
      updated = true;
    } else {
      updated_layers.push(layer);
    }
  }
  if (updated) {
    return Object.assign({}, state, {
      layers: updated_layers,
    }, incrementVersion(state.metadata, LAYER_VERSION_KEY));
  }
  // if nothing was updated, return the default state.
  return state;
}