Under the new coalition, Giuseppe Conte would serve as Italy’s prime minister, as he did during the contentious 14-month League-Five Star government. (Ettore Ferrari/EPA-EFE/Shutterstock)

After weeks of shifting alliances and political jockeying, Italy is close to minting a new governing coalition: a union of two long-adversarial parties aligned in their aim to thwart the far right’s rise to power.

The leaders of the anti-establishment Five Star Movement and the mainstream center-left Democratic Party said Wednesday that they had reached a deal to form a government. They still need the blessing of President Sergio Mattarella, who is expected Thursday morning to give a formal mandate to Prime Minister Giuseppe Conte to work toward the formation of the coalition.

The deal still faces several hurdles, including negotiations over the cabinet. Five Star, as is its custom, will put the deal to an online vote of party insiders.

But the agreement taking shape is the unlikely outcome of a month of political upheaval in Rome — a period in which nationalist League party leader Matteo Salvini pulled the plug on his testy coalition with Five Star and tried to orchestrate an election in a bid to become prime minister. His gambit backfired spectacularly as other parties negotiated to form a majority and forestall his rise.

Over the past year, President Trump has spoken admiringly of Italy’s government — including its tactics against migrants, an approach originated by Salvini, who is the minister of the interior. But on Tuesday, Trump gave Conte an endorsement, writing on Twitter that Conte “loves his country greatly & works well with the USA. A very talented man who will hopefully remain Prime Minister!”

Under the new coalition, Conte would be prime minister, as he was during the 14-month League-Five Star government. But the government he represents would be significantly recast — probably being less nativist, less interested in anti-migrant rhetoric and more willing to cooperate with the European establishment. What remains to be seen, though, is whether a government composed of two weakened and internally divided parties can win over Italians and last long enough to address the country’s deep-rooted economic problems.

“This is a weak, messy, fragile solution — with a very weak democratic legitimacy,” said Giovanni Orsina, a professor of history at LUISS Guido Carli University in Rome. “These are parties that for a year now have been losing [regional and local] elections.”

Even by the standards of a country that has had more than 60 governments since World War II, the recent weeks have been jarring. In early August, Salvini seemed poised to become prime minister and turn Italy into the most solidly far-right country in Western Europe. Instead, Italy is on the verge of having a much different government: one that doesn’t include the country’s most popular party.

The political machinations that brought Five Star and the Democratic Party together were possible only because each party has been worried about how it might perform in an election. Five Star, outmaneuvered for more than a year by Salvini, has lost much of its support. The Democratic Party, meanwhile, has slowly tried to rebuild its leadership after entering the political wilderness.

The prevailing parties are taking considerable risks in working together. The Five Star Movement made its name as a rebellious outsider for the Internet age, attacking the Democratic Party as corrupt and out of touch. The Democratic Party long denounced Five Star politicians as incompetent novices with a penchant for conspiracy theories. For weeks, they have debated over the shape of a possible new government — with Democratic Party chief Nicola Zingaretti backpedaling on opposition to Conte.

“We love Italy and think it worth to try this experience,” Zingaretti told reporters Wednesday. “We mean to put an end to the season of hate, rancor and fear.”

If the two parties form a coalition and can put their animosity behind them, they will have the opportunity to govern through the end of the legislature in 2023. That prospect would leave Salvini out of power for years, potentially diminishing the momentum of a politician who had remade Italian politics in his image and become the face of the far right in Western Europe.

It was that popularity that Salvini had been hoping to leverage when he said in early August that he’d had enough of Five Star’s obstruction and he called for a no-confidence vote against Conte. “Let’s quickly give the choice back to voters,” Salvini said at the time.

Conte resigned before facing the vote. But on his way out, he — a lawyer plucked from obscurity last year by Five Star to become the coalition’s mediator-like prime minister —took some damaging jabs at Salvini. He said Salvini was “obsessive” about hard-line immigration policy and was “constantly” focused on increasing his own support. In recent weeks, support for Salvini’s League has dropped to 34 percent from 39 percent in late July, according to a poll published by the newspaper Sole 24 Ore. Polls show Conte is Italy’s most trusted politician.

Salvini said Wednesday that Five Star and Democratic Party members would “stop at nothing to keep their posts,” and he accused Five Star of branding itself a populist revolutionary party but forming a government with the “maximum defenders of the system.”

So far, financial markets have welcomed the idea of the new coalition under the assumption that its leaders in Rome will be likelier to hew to European financial rules and have a better rapport with regulators in Brussels. Salvini had made a point of pushing back against the bloc’s edicts, and though Five Star members also tend to be euroskeptical, those views will be leavened by the Democratic Party.

This week, Zingaretti had listed, among the requirements for an alliance, a “clear non-debatable Europeanist choice, and the commitment to build a deeply renewed Europe based on development, labor and solidarity.”

On migration, Italy could soften its stance — but only to an extent. Salvini, with fanfare on social media, closed Italian ports to most migrant rescue vessels and drafted laws to limit such vessels’ ability to operate in the Mediterranean. Politicians on the left have accused Salvini of fearmongering on migration. But leaders across the ideological spectrum share his view that Italy has shouldered an unfair burden and has not been helped by the rest of Europe in distributing migrants across the continent. Analysts note that Salvini’s predecessor as head of the Interior Ministry, Marco Minniti, worked with less fanfare to reduce irregular migration from Libya. Minniti is a member of the Democratic Party.

“If you reopen ports, NGOs in the Mediterranean multiply again, traffickers start throwing the desperate on our coasts and the flow starts anew; this government is dead in the water, because this is not something that the country is going to accept,” Orsina said. “I think rhetoric will change, but not the substance.”