A new biography of Apple chief executive Tim Cook out this month describes the moment — and the deliberations — after the FBI issued an unprecedented legal order demanding Apple undermine the security of its flagship product.

The new book, “Tim Cook: The Genius Who Took Apple to the Next Level” by Leander Kahney, offers a first-hand view from former staff about how Apple battled the order, which Cook said would be “too dangerous” to comply with.

Three years ago, following the San Bernardino terrorist attack, which killed 12 people and injured dozens, the FBI demanded Apple create a special version of its mobile software capable of bypassing the encryption and other security features on an iPhone used by one of the shooters. But fearing the backdoored software could one day end up in the wrong hands, Cook wrote in a public letter that the company would reject the order and fight the FBI in court. “This software would have the potential to unlock any iPhone in someone’s physical possession,” said Cook. What would ensue was a public battle between the tech giant and the government in a lawsuit lasting several months, until the government paid out for hackers to break into the device.

Apple long contended that the Justice Department wanted to fight Apple in the open to win over the public in the aftermath of the attack — painting Apple as helping terrorists — and sought a court order before the company could respond.

Had Apple lost the case, its long-running privacy and security mantra would be shattered. Cook is said to have “bet the company” on the decision to fight the order, according to former Apple general counsel Brian Sewell, who was quoted in the book.

Sewell described the FBI’s order as a tipping point following “a lot of activity” that preceded the decision by former FBI director James Comey to ask a judge to sign the order.

The order was issued on an obscure law known as the All Writs Act, which the FBI interpreted as a way to ask a court to order a company to do something not otherwise covered by the law. An order cannot be “unduly burdensome,” a subjective term often determined by the court issuing the order.

Sewell said the FBI had as early as 2014 asked Apple for “getting access to phones on a mass basis” after Apple rolled out iOS 8, which encrypted iPhones and iPads with a passcode. Law enforcement struggled to get into devices they said was necessary to investigate crimes. There was no other feasible way to break into an iPhone — even with a court order. Not even Apple could unlock the devices. The company declined the FBI’s request.

But the book said law enforcement “saw it as an opportunity to force Apple’s hand,” wrote Kahney.

“There was a sense at the FBI that this was the perfect storm,” said Sewell, as quoted. “We now have a tragic situation. We have a phone. We have a dead assailant. This is the time that we’re going to push it. And that’s when the FBI decided to file [the order],” he said.

Apple knew public opinion was divided. But the company didn’t let up.

For the following two months, Apple’s executive floor at its former headquarters at One Infinite Loop in Cupertino “turned into a 24/7 situation room,” with an intensified effort to respond to press queries — which Apple had seldom done before, known historically as a secretive company.

The case eventually resolved without a trial. The day before Apple was meant to go head-to-head with the government in a California court, the government pulled the plug on its legal action. It had paid almost a million dollars to hackers to successfully break into the phone. Cook was said to be “disappointed” the case didn’t come to trial, according to Sewell, because he sought a resolution to the case that he believed would have ruled in Apple’s favor. The legality of the order remains unsettled today, despite efforts by the government to force other companies — like Facebook — to rework their software to allow access to police.

Justice Department spokesperson Nicole Navas declined to comment. Apple did not comment.

“Tim Cook: The Genius Who Took Apple to the Next Level” is on sale April 16.