Theory of the ergodic divertor