Single-electron analysis of the space-charge effect in free-electron lasers