Change the order of the layer in the stack.
@param {Object} state Current state.
@param {Object} action Action to handle.

@returns {Object} The new state.
function orderLayer(state, action) {
  let layer = null, target = null;
  for (let i = 0, ii = state.layers.length; i < ii; i++) {
    if (state.layers[i].id === action.layerId) {
      layer = state.layers[i];
    }
    if (state.layers[i].id === action.targetId) {
      target = state.layers[i];
    }
  }

  if (layer !== null) {
    let targetId = action.targetId;
    let targetGroup = getGroup(target);
    let layerGroup = getGroup(layer);
    if (layerGroup !== targetGroup) {
      targetId = changeTarget(state, action);
    }
    if (targetId !== false) {
      return placeLayer(state, layer, targetId);
    }
  }
  return state;
}