Cable detection: http://blog.teledynelecroy.com/2016/02/usb-type-c-cable-dete...
Overview of USB power delivery negotiation: https://www.embedded.com/usb-type-c-and-power-delivery-101-p...
And then after the initial connection, the devices can renegotiate their roles if you want to reverse the charge.
It's great when it actually works. In the real world there are so many incompatible implementations that USB-C is a real clusterfuck.
While I don't doubt the foresight of Apple HW engineers, the possibility strikes me that this is entirely accidental and derived from the design of the USBPD spec. If so, these "stars align (but only because the spec was functionally designed)" moments are some of my favorites.