It was all there on paper in black and white, down to the precise number of centrifuges: the terms of a potential "fix" that President Donald Trump had demanded for the United States to stay in the Iran nuclear deal.

Dragged kicking and screaming into five months of negotiations, America's closest allies in Europe had finally agreed in principle to the toughest of Trump's demands. They conceded that some expectation could be put into place in perpetuity that Iran should never get closer than one year from building a bomb. All that was left was to figure out creative language for how that constraint would be phrased that everyone could support.

Trump walked away from the deal anyway. Announcing the U.S. was out, he called the 2015 pact his predecessor brokered "defective at its core" and said the U.S. would immediately re-impose sanctions lifted under the deal.

"We can't allow a deal to hurt the world," Trump added Wednesday, as the world scrambled to figure out what comes next.

Behind the scenes, though, the Trump administration had been actively preparing for a pullout since January, when Trump declared that he would withdraw if an "add-on" deal wasn't reached. To many U.S. officials, it was as clear then as now that the president would not be swayed to accept even a toughened-up version of the accord.

This account of how Trump withdrew from the deal draws on interviews Wednesday with a dozen White House officials, senior State Department officials, foreign diplomats and outside advisers to the Trump administration involved in the negotiations. Most were not authorized to comment publicly and spoke on condition of anonymity.

___

Trump had just celebrated the anniversary of becoming president in January when he issued his ultimatum: If there's no fix to the deal by May 12, the U.S. would be out. There was no chance that three of the deal's members — Iran, Russia and China — would consider changes, so Trump focused on the Europeans — Germany, the U.K. and France — in hopes that the rest would go along once a fix was agreed to by the rest.

"This is a last chance," Trump said.

Right away, a team led by Brian Hook, the State Department's policy chief, began intensive negotiations with the Europeans on the issues Trump insisted must be fixed: new penalties on Iran's ballistic missile inspections, expanded access for U.N. nuclear inspectors and an extension of the restrictions on Iran's enrichment beyond the current life of the deal.

Before long, the U.S. found the Europeans were amenable to dealing with the first two. The third was a nonstarter. After all, the terms of the 2015 deal explicitly say that the restrictions "sunset" over time. Any extension without Iran's explicit consent would put the Europeans themselves in breach of the deal.

A supplemental agreement was drafted, and tweaked, and tweaked again, even as negotiations continued about what mechanism to use to hold the Iranians to the restrictions indefinitely. At least one draft included footnotes specifying that the same nuclear parameters in the 2015 deal should continue to be in place: no more than 5,060 IR-1 centrifuges, no uranium stockpiles larger than 300 kilograms, no enrichment beyond 3.67 percent and no advanced centrifuges, according to an individual who read the draft.

___

At the United Nations, U.S. Ambassador Nikki Haley led a parallel effort to get France and the U.K. to toughen up on other Iranian behavior, such as its support for Hezbollah militants in Lebanon and for Shiite Houthi rebels in Yemen. Haley's argument to the Europeans: Helping us with these side issues can only help you make your case to Trump to stay in the deal.

But at the White House, senior staffers were skeptical that anything would satisfy Trump. After all, the president had already told aides he refused to waive sanctions on Iran again. So White House and National Security Council staff began laying the groundwork for a U.S. withdrawal, even as the negotiations with the Europeans were underway.

___

As the May deadline drew closer, the Europeans grew increasingly alarmed that Trump seemed determined to scrap the deal. And so began a parade of visits by their leaders to the White House to make the case in person.

First came French President Emmanuel Macron, the European leader closest to Trump. Not only did he raise the issue during a state visit, but he also took the extraordinary step of hammering the point in a speech to a joint session of Congress.

"We signed it, both the United States and France," Macron said of the pact. "That is why we cannot say we should get rid of it like that."

The Germans followed days later, with Chancellor Angela Merkel emphasizing Europe's openness to working with Trump to crack down more comprehensively on Iran. The closing pitch was left to British Foreign Secretary Boris Johnson, who even appeared on Trump-friendly "Fox & Friends" to urge him not to walk away.

Johnson and the others came to Washington armed with clever solutions to the remaining hang-up over extending restrictions on Iran permanently. The Europeans were firm on one point: They could not unilaterally impose on Iran what it had not agreed to in the deal. But there were ideas to use other mechanisms that don't expire, such as supervision of Iran's civil nuclear needs, to ensure it stayed within the bounds and didn't approach a bomb.

___

By the time Johnson arrived, it became clear that the negotiations, while still ongoing, were futile. And on Monday, Trump tweeted that he'd announce his decision at 2 p.m. Tuesday — almost a week before his deadline.

His decision was kept closely quarantined until the end, with even most White House, State Department and Treasury Department officials unsure what he'd decided. The State Department and Treasury prepared three versions of the public statements and technical guidance that would have to be released with his decision: one for staying in, one for full withdrawal and one midway option in which only some sanctions would be immediately re-imposed, potentially preserving the possibility that the U.S. could later reverse course and stay in.

Trump's administration also didn't explicitly tell the Europeans he was withdrawing. In a call with Macron just ahead of his announcement, Trump made clear he was still ardently opposed to the deal but left Macron guessing about precisely what he would do.

He and the other Europeans learned when everyone else did: on Tuesday, when Trump appeared on live television in the Diplomatic Reception Room and said he was out.

"The fact is this was a horrible, one-sided deal that should have never, ever been made," Trump said. "It didn't bring calm, it didn't bring peace, and it never will."

___

Associated Press writer Jill Colvin contributed to this report.

___

Reach Josh Lederman on Twitter at http://twitter.com/joshledermanAP