WASHINGTON — As the sun rose over the nation's capital Thursday, legislative leaders had one main priority: pass a House bill reauthorizing crucial surveillance powers. With a law expiring next week, a lot of American intelligence eavesdropping was about to go dark.

Months of work had gone into the political strategy behind the reauthorization of what is known as Section 702 of the Foreign Intelligence Surveillance Act. It was a complicated dance, because some lawmakers were threatening to defeat the bill unless changes were made. But the Republican House speaker and his Democratic counterpart were on the same page. The White House days earlier had issued a statement in support of the measure.

And then, at 7:33 am, President Donald Trump threw the whole plan into chaos.

Trump posted a tweet that seemed to denounce the bill, suggesting that the FISA law had been used to "so badly surveil and abuse the Trump Campaign." The tweet mirrored language used 47 minutes before, by a Fox News analyst, Andrew Napolitano. Napolitano mischaracterized the legislation on "Fox and Friends," a show the president is known to watch in the White House residence as he begins his day.

“House votes on controversial FISA ACT today.” This is the act that may have been used, with the help of the discredited and phony Dossier, to so badly surveil and abuse the Trump Campaign by the previous administration and others? — Donald J. Trump (@realDonaldTrump) January 11, 2018

A second tweet emanated from Trump's account some 101 minutes later, reaffirming his support for the bill, which passed handily, later that day. But Trump's mistake — which immediately became the lead story on news websites and cable — sent the White House and Hill Republicans into crisis mode, with Chief of Staff John Kelly rushing in to orchestrate a clean-up.

Director of National Intelligence Dan Coats, CIA Director Mike Pompeo and White House counter terrorism adviser Tom Bossert were involved, and House Speaker Paul Ryan got on the phone with Trump himself, according to multiple government officials familiar with what happened.

Despite an angry denial from White House spokeswoman Sarah Sanders that Trump was in any way confused, his actions left White House and intelligence officials shaking their heads, given that they put at risk a program that the spy agencies rely on to track terrorists, weapons proliferation and foreign adversaries.

White House press secretary Sarah Huckabee Sanders denied the president was confused. Manuel Balce Ceneta / AP

Some analysts have theorized that Trump's controversial tweets are strategic, designed for some purpose only he can divine. But there was nothing smart or savvy about this one, U.S. officials said. As one senior official put it, "It completely disrupted everything," with no political gain for the Trump administration.

"When the history of President Donald Trump's use of Twitter is written, there will be a stiff competition for his most destructive, most irresponsible tweet, wrote Susan Hennessey, a former lawyer at the National Security Agency. This one, she said, will be a strong contender.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

One thing that mystified senior intelligence officials, they said, was that Trump had been briefed on the FISA issue, and they believed he understood it. It was the top legislative priority of the intelligence agencies.

Up for reauthorization is a part of the law that allows the intelligence agencies to vacuum up data on foreigners from internet companies like Google, Facebook and Microsoft — without an individualized warrant. The eavesdropping is overseen by a secret court.

Because some data from Americans is incidentally collected there are privacy concerns, and some wanted a new requirement that the FBI get a warrant to search the American data. But the intelligence community opposed that step as too cumbersome.

This likely was not the part of the FISA law that would have been used to eavesdrop on Trump aides suspected of colluding with the Russian interference campaign, U.S. officials said. Nonetheless, in what was not a coincidence, the director of national intelligence issued new rules Thursday on "unmasking," or circulating the names of Americans who are featured in intelligence reports, when there is a need to know. Trump has said the names of some of his aides were improperly "unmasked" in the Russia investigation, although no evidence has emerged to support that.

Fox's Napolitano, a former judge, is the same pundit whose unsupported assertions about surveillance appeared to have led Trump to proclaim last March that he had been wiretapped in Trump Tower. He reportedly was suspended by Fox News over his unsupported claim that a British intelligence agency helped the Obama administration spy on candidate Trump. But he was back on the air Thursday, urging Trump to reverse course on the surveillance bill.

"I don't understand why Donald Trump is in favor of this," Napolitano said. "His woes began with unlawful foreign surveillance and unconstitutional domestic surveillance of him, before he was the president of the United States, and now he wants to institutionalize this."

Trump's tweet began with a quote from a Fox headline.

"'House votes on controversial FISA ACT today,'" he wrote on Twitter. "This is the act that may have been used, with the help of the discredited and phony Dossier, to so badly surveil and abuse the Trump Campaign by the previous administration and others?"

On the Hill, lawmakers were stunned. Rep. Peter King, Republican chairman of the Homeland Security Committee's counterterrorism subcommittee, told NBC News that Trump was obviously mistaken. House Democrats urged Republicans to pull the bill.

The ranking Democrat on the Senate intelligence committee, Mark Warner, said on Twitter: "This is irresponsible, untrue, and frankly it endangers our national security. FISA is something the President should have known about long before he turned on Fox this morning."

This is irresponsible, untrue, and frankly it endangers our national security. FISA is something the President should have known about long before he turned on Fox this morning. https://t.co/LQ9shut53k — Mark Warner (@MarkWarner) January 11, 2018

Behind the scenes at the White House, Trump's advisers were swinging into action.

Kelly hustled over to explain the situation to Trump, three officials said. In consultation with others, he came up with language for a second tweet.

"With that being said," it began, as if continuing a thought, "I have personally directed the fix to the unmasking process since taking office and today's vote is about foreign surveillance of foreign bad guys on foreign land. We need it! Get smart!"

Kelly then rushed to the Hill, where he buttonholed wavering lawmakers.

The bill passed the House overwhelmingly, and it is expected to pass the Senate next week. Some government officials mused privately that Trump's error may even have helped its chances, because it led to a lot of news coverage featuring former national security officials discussing the importance of re-authorizing FISA.

For the record, White House officials refused to acknowledge that anything odd had happened.

On Thursday afternoon, White House press secretary Sanders insisted there was no "confusion or contradiction" between Trump's two tweets.

Pressed by NBC's chief white house correspondent Hallie Jackson, Sanders retorted:

"I think that the premise of your question is completely ridiculous and shows the lack of knowledge that you have on this process."