Not really "on a whim." The implications/consequences of purchasing and using the S-400 were clear.
It was a clear choice made by Turkey and the decision cost the US much more than Turkey ever invested in the program anyways.
https://en.wikipedia.org/wiki/Lockheed_Martin_F-35_Lightning...
https://www.defensenews.com/air/2019/07/17/turkey-officially...