Congress on Thursday approved a massive budget deal to avert an impending government shutdown, and President Trump promised to sign it, but only after announcing he would also declare a national emergency so he can get more money for a border wall.

Moments after Trump disclosed his intentions in a phone call with Senate Majority Leader Mitch McConnell (R-Ky.), McConnell announced the news on the Senate floor, ending days of uncertainty over whether the president would support the $333 billion spending deal, which includes less than a quarter of the money he’s sought for a steel wall along the U.S.-Mexico border.

The Senate swiftly passed the legislation on an 83-to-16 vote, and the House followed suit hours later, approving the bill 300 to 128 — veto-proof margins in both chambers. If Trump keeps his promise to sign the measure, it would avert a government shutdown that would have started Saturday and keep the government open through at least Sept. 30.

“I hope the president has learned, like I’ve learned over the years, that a shutdown of the government, collapse of the government funding, is not good for him, not good for the Republicans, it’s not good for the Democrats,” Senate Appropriations Chairman Richard C. Shelby (R-Ala.) said in an interview after the vote. “It’s not good for people’s view of us in the world, either. And it’s not good for the economy.”

Lawmakers had been eager to put shutdown politics behind them after a record 35-day funding lapse forced 800,000 federal workers to go without paychecks through Christmas and much of January. But a national emergency declaration, which would allow Trump to circumvent Congress and use the military to build his wall, would create a new set of problems.

Many of Trump’s GOP congressional allies called the move ­ill-advised, and Democrats promised immediate action aimed at blocking it. And the declaration will probably face legal challenges from states, border residents, civil liberties groups and possibly congressional Democrats.

Trump is expected to sign the budget deal and declare the national emergency in an appearance Friday morning, according to a senior White House official who spoke on the condition of anonymity.

House Judiciary Chairman Jerrold Nadler (D-N.Y.) said he plans to introduce a resolution of disapproval that would overturn the declaration, calling it a “a gross abuse of power that cannot be tolerated.”

Under the National Emergencies Act, House passage of a disapproval resolution would trigger automatic consideration by the Senate, where a simple majority vote would be required to agree to it. Given opposition from some Republicans, that raises the prospect that a disapproval resolution would pass the narrowly divided Senate in an embarrassing rebuke to Trump — a scenario McConnell privately warned the president about recently.

That would force Trump to contemplate issuing the first veto of his presidency, which the president’s critics in Congress would probably lack the votes to override. But Nadler said that if their resolution is vetoed, House Democrats would challenge the emergency declaration in court.

McConnell has been warning against an emergency declaration publicly and privately for weeks, but on Thursday he told senators he had informed the president he would support the move. According to two officials with knowledge of the exchanges who spoke on the condition of anonymity because they were not authorized to speak publicly, Trump had been leaning against supporting the congressional spending bill but relented after several conversations with McConnell, who then announced his agreement to go along with an emergency declaration.

Although Trump has been hinting for weeks at plans to declare a national emergency, his decision caught some lawmakers by surprise and divided Republicans.

Sen. Susan Collins (R-Maine) said that an emergency declaration would “be a mistake.”

“I don’t think it’s a good idea to do it this way,” said Sen. Marco Rubio (R-Fla.). “I don’t believe that’s the way we should be doing these sorts of things. I actually think that there’s a real constitutional question about it.”

The White House Counsel’s Office has warned Trump against declaring a national emergency, calling it a “high litigation risk,” according to a person with knowledge of the discussions who spoke on the condition of anonymity to reveal private deliberations.

Lawyers encouraged Trump to reprogram money without declaring a national emergency. But the president has been inclined toward the declaration, in part because he sees it as an avenue to more wall money, according to administration officials who spoke on the condition of anonymity because they were not authorized to speak publicly.

Republican lawmakers also warned that an emergency declaration would set a bad precedent for a future Democratic president — and House Speaker Nancy Pelosi (D-Calif.) suggested that if Trump is going to declare a national emergency to build his wall, a Democratic president might take the same step to address gun violence.

The 1,169-page legislation passed Thursday was the product of painstaking bipartisan negotiations in the three weeks since the last shutdown ended, a timeline Trump imposed on Congress to force lawmakersto come up with a deal. It would fund nine Cabinet departments and dozens of other agencies through Sept. 30, including Homeland Security, Commerce, Agriculture, and Transportation.

The compromise provides $1.375 billion for 55 miles of new fences along the border in Texas, far short of the $5.7 billion Trump had sought for 234 miles of steel walls. The final number for border barriers is also less than deals that were on the table last year.

The legislation was released just before midnight Wednesday, giving lawmakers and the White House very little time to review it before voting. Lawmakers defended the rushed timeline because of the impending shutdown deadline.

After getting burned by Trump in December on a spending bill the Senate passed and the president disavowed, McConnell wanted to move as fast as possible to a vote following Trump’s assurance of support. The majority leader was in such a hurry to announce Trump’s backing and call the vote Thursday that he interrupted Sen. Charles E. Grassley (R-Iowa) in the middle of a speech about biofuels, drawing wrathful exclamations and glares from the longtime Iowa lawmaker.

Some liberals were unhappy with the bill, arguing no money at all should go to border barriers. They also complained that overall funding for the Homeland Security Department increases under the bill, and that the legislation does not do enough to limit the U.S. Immigration and Customs Enforcement agency’s detention authority.

Democratic negotiators included language they said should reduce ICE detention numbers over time, but Republicans say ICE will still have the flexibility to detain as many immigrants as agency officials deem appropriate. Some liberals and immigrant advocates agreed with the GOP assessment.

In a joint statement Thursday, freshman Democratic Reps. Alexandria Ocasio-Cortez (N.Y.), Ilhan Omar (Minn.), Ayanna Pressley (Mass.) and Rashida Tlaib (Mich.) announced their opposition to the legislation, citing Trump’s “weaponization” of federal immigration agencies.

“The Department of Homeland Security does not deserve an increase in funding and that is why we intend to vote no on this funding package,” the four lawmakers wrote. But in the end, Pelosi limited Democratic “no” votes to 19 and told reporters that many of those defectors would have been with her “if we’d needed their votes.”

Some conservatives also trashed the deal, with conservative television host Laura Ingraham terming it a “monstrosity” that she predicted would lead to increased undocumented immigration.

White House officials have closely held their precise plans on taking executive action, insisting that they had legal ways to secure more than $5.7 billion in funds without congressional approval but refusing to say exactly how they’d do it.

One reason they were circumspect is because they were waiting for final details of the congressional deal to be made public, so they could ascertain the level of resources they would need to redirect from other programs, according to a White House official who spoke on the condition of anonymity to discuss the internal plans. Traditionally, moving money from one program to another requires congressional approval, but declaring a national emergency could give them more flexibility.

Congressional aides said Thursday that they believe there is up to $21 billion in “unobligated” Defense Department funding that the president could target for construction of the wall. That includes $10 billion in military construction money in the fiscal 2019 budget and $11 billion in previous budgets that is not yet spent, numbers that U.S. officials disclosed weeks ago when Trump first floated the idea of declaring a national emergency.

For weeks, defense officials have been informally reviewing military construction projects that they could cancel or delay if Trump seeks military construction money to pay for the wall. Defense officials had declined to provide specifics, saying that no emergency declaration had been made.

A central promise of Trump’s 2016 presidential campaign was that he would somehow make Mexico pay for the construction of the border wall, but since becoming president, all of his efforts have focused on using U.S. taxpayer money to finance the projects. Trump has said that a pending revised trade agreement with Mexico and Canada effectively fulfills his promise, but it is unclear whether Congress will approve that trade agreement. And there is no language in the agreement that would create any new funding mechanism to provide money for a wall.

John Wagner, Dan Lamothe, Josh Dawsey, Mike DeBonis, Sean Sullivan and Paul Kane contributed to this report.