Mexican President Enrique Peña Nieto and most of his government were miles away from Mexico City on Wednesday, sitting on a grandstand under the hot midday sun, watching an aviation demonstration at a military base, when the news started to filter through.

In cellphone messages and traded remarks, they learned that President Trump planned within days to sign an order triggering U.S. withdrawal from NAFTA, the trilateral agreement that has regulated trade in North America for decades.

At about the same time in Washington, Canadian Ambassador David MacNaughton was learning the same thing. Though taken aback by the timing, he was not quite as astounded that the U.S. president would make such a move with no warning.

“I’ve learned not to be surprised by very much these days,” MacNaughton later reflected.

On Thursday, a second bombshell exploded. Trump, in an Oval Office interview with The Washington Post, served notice that he may terminate a bilateral trade agreement with South Korea, another major U.S. ally and trading partner. “It’s a horrible deal,” he said.

(Daron Taylor/The Washington Post)

[Inside Trump’s sudden shift on NAFTA]

Since taking office, Trump has set out to unsettle and surprise his allies and adversaries alike, and by that measure he has succeeded. He wants stalwart friends such as Canada and Mexico to be on edge about what the U.S. president might decide to do on any given day. The aim of his campaign, after all, was to thoroughly disrupt the world’s established political and economic order.

The keeper of the flame — of Trump’s nationalist populism, pugilistic tactics and theatrical flair — is Stephen K. Bannon, the ideological warrior and chief White House strategist. It was Bannon, along with trade hard-liner Peter Navarro, a protectionist who has warned for years of deficits with China, who led the administration’s internal push for Trump to terminate the North American Free Trade Agreement.

Bannon and other nationalists have argued that the United States’ trade relationships are — or should be — inextricably linked to its military alliances and commitments. The United States is “upside down” with Canada, Germany, Japan, Mexico, South Korea and other key partners that sell more than they buy from us, a senior administration official said.

Rebalancing these relationships, one by one, has been a focus of Trump’s first 100 days in office. “There isn’t a day that goes by that the president doesn’t discuss some aspect of trade,” Commerce Secretary Wilbur Ross said Friday. “It’s all a continuous activity because trade is so important to the economy, it’s so important to the administration’s four-point plan, and it’s so important to the promises he made during the campaign.”.

A farm worker arranges boxes with freshly harvested lemons in Buenavista Tomatlan, Mexico. The Trump administration is looking to re-examine NAFTA in a move that has jarred sellers of Mexican products. (Stringer/Reuters)

Ross spoke at the White House, where he announced in a briefing that the president on Saturday would sign an executive order directing a 180-day study of all trade agreements — including membership in the World Trade Organization — to which the United States is a party.

Trump learned during the campaign that trade deals were an easy scapegoat for the multitude of problems that ail the country, especially in communities across the industrial Midwest that have been decimated by the forces of globalization.

As he stumped in one hollowed-out factory town after another, Trump galvanized working-class voters by vowing to undo the alleged injustices of NAFTA and other agreements that he charged were a result of American politicians being outsmarted by foreigners. He is expected to turn to this theme Saturday, when he returns as president to Harrisburg, Pa., for an evening rally and delivers what advisers have said will be a fiery speech.

Trump’s lead adviser on trade issues is Ross, a billionaire investor and industrialist and a longtime Trump friend.

Other key players include Gary Cohn, head of the National Economic Council and a former Goldman Sachs director who was said to have driven a hard line in recent talks with China; Robert E. Lighthizer, Trump’s nominee for U.S. trade representative, who is expected to play a more behind-the-scenes role on implementation; as well as Bannon, Navarro and Jared Kushner, the president’s son-in-law and senior adviser.

By late Wednesday, the NAFTA crisis had been averted. In evening telephone calls, Trump separately assured Peña Nieto and Canadian Prime Minister Justin Trudeau that the United States was willing to renegotiate the agreement.

All that was left of the withdrawal plan — whose existence Trump confirmed to The Post — was a mood of triumphalism at the White House, and a renewed wave of unease and indignation across the United States’ neighbors.

While Bannon and Navarro advised Trump to stay the course on ending NAFTA, the president was ultimately dissuaded by interventions from Ross and other Cabinet officials, lawmakers and business leaders who said a precipitous move would do far more harm than good to U.S. workers, the economy and Trump’s political standing in export states that voted for him, according to several senior administration officials.

The president had a somewhat different narrative, one in which he magnanimously responded to desperate pleas from Canada and Mexico. “I can always terminate” the agreement, he told The Post. “I was all set to terminate.”

But “I respect both of those people, I like both of those people,” Trump said of Peña Nieto and Trudeau. “They called me up, they said, ‘Could we try negotiating?’ I said, ‘Absolutely, yes.’ If we can’t come to a satisfactory conclusion, we’ll terminate NAFTA.”

[The places in American most exposed to a trade war]

In Mexico and Canada, however, officials said there is a price, in government-to-government relations and public opinion, for what they see as Trump’s posturing.

Mexican officials said they were doubly surprised by word that Trump was prepared on Saturday to activate the six-month withdrawal notification specified in the agreement and by the fact that they were not told about it before it suddenly became public. The upheaval came just as they had begun to believe that relations were normalizing after the tumultuous campaign and transition, and the first months of the administration.

The contentious issue of a border wall with Mexico had virtually disappeared, at least until Trump insisted last week that part of its cost be included in a continuing resolution to avert a government shutdown.

A senior Mexican official said that what Mexico saw as Trump’s suggestion that Peña Nieto “called to beg him — ‘Please do not, blah, blah, blah,’ ” was false. “I know that my president never said that,” insisted the official, who spoke on the condition of anonymity about the fraught diplomatic relationship.

“He will always have to say whatever he wants to say,” the official said of Trump. “We’ve already gotten too familiar with him and his narratives and his spin. . . . No matter how much you strategize and plan and talk to your counterparts . . . there’s always the pending cloud of the tweet. Nobody should get particularly comfortable, particularly at the government level.”

Mexico maintains that it is, if anything, more eager than the United States to proceed with NAFTA renegotiations. Under Mexican law, sitting officials must resign their positions six months before an election in which they plan to run. Midyear elections in 2018 mean that at least some of the current government’s negotiators will be stepping down before the end of this year.

The U.S. side should have the same interest in completing a renegotiation before Peña Nieto ends his term, said Larry Rubin, a businessman and president of the American Society of Mexico. “Since nobody knows who will be the next president — it could be someone on the left or someone on the right — the best bet for the Trump administration is to have negotiations completed before then,” Rubin said.

MacNaughton, the Canadian ambassador, said Canada by and large was pleased with how things had progressed. “We’ve established a very constructive working relationship,” he said.

But “it’s fair to say” that the rhetoric of recent days “is alarming to Canadians,” MacNaughton said. “It does have an impact, and it’s not a good impact. I understand everybody has got to get elected, but the reality is that this kind of anti-trade rhetoric can be very damaging, particularly when you think about Canada” as a major job creator in the United States.

Despite Trump’s claim Thursday that Canada has a “massive” trade surplus with the United States, figures recently released by the Office of the U.S. Trade Representative indicate a $12 billion Canadian deficit in combined goods and services.

Trudeau has moved to diversify Canada’s export markets, and this week he sent a high-level mission to China to discuss lumber exports and a free-trade agreement.

In a news conference Thursday, Trudeau took the high road on U.S. relations, talking of “tremendous opportunities” and “mutual benefit.”

At the same time, however, he said that Canada would respond to any unilateral U.S. action and was looking at “a broad range of options and paths available to us” should NAFTA renegotiation fail.