The woman who shot three people and killed herself at YouTube’s San Bruno headquarters bought the handgun she used in January — apparently the first firearm she’d owned, authorities said Thursday.

Nasim Aghdam, 39, legally bought the 9mm Smith & Wesson semiautomatic pistol on Jan. 16 from a gun dealer in San Diego, where she lived, said San Bruno police Cmdr. Geoff Caldwell. He declined to identify the dealer.

Caldwell said Aghdam, an animal rights advocate and YouTube video maker who according to investigators and family members was angry at the video-sharing service long before Tuesday’s shooting, also bought at least two ammunition magazines.

Under a California law designed to limit the possible carnage from shootings, Aghdam was limited to 10-round magazines. Caldwell said a second magazine was found at the scene, indicating she had squeezed the trigger numerous times.

“The suspect began firing the pistol until it was empty, dropped the magazine, reloaded a new magazine into it, continued firing and then turned the gun on herself,” Caldwell said.

Aghdam’s family said they told police she had been furious at YouTube for a year because she believed it was wrongly censoring some of the many videos she posted.

Her father, Ismail Aghdam, had reported her missing Monday to San Diego County sheriff’s deputies after she left her grandmother’s house in that city “due to family issues.” His family said he told officers she was mad at YouTube and should be watched after she was found at 1:40 a.m. on the day of the attack, sleeping in her car in Mountain View.

After police there let her go, finding no evidence she posed a threat, she went to a local gun range to practice shooting, then drove to San Bruno, parked near YouTube and entered its grounds through a parking garage, authorities said.

Investigators believe the shooter chose her victims at random when she opened fire in a courtyard just before 1 p.m. Tuesday, sending employees running for their lives in every direction.

Two female victims, ages 32 and 27, were released from San Francisco General Hospital Tuesday night after receiving treatment for gunshot wounds. The third victim, a 36-year-old man, continued to recover on Thursday and was listed in fair condition at the hospital. The victims’ names have not been released.

On Wednesday, investigators from the federal Bureau of Alcohol, Tobacco, Firearms and Explosives interviewed the shooter’s family and served a search warrant at their home in a quiet neighborhood in the suburban Riverside County town of Menifee.

A search warrant was also served on the San Diego county home of Aghdam’s grandmother, where Aghdam lived.

The shooting was particularly troubling to experts because most mass shooters are male. The FBI studied 160 active-shooter incidents between 2000 and 2013 and found that just six involved a female shooter — 3.8 percent of the total.

Frank McAndrew, a psychology professor at Knox College in Illinois, said Aghdam’s case appeared to differ in significant ways from recent rampages.

“Nobody died except her, she was using a handgun instead of a high-powered weapon, and she had a specific grievance against this business rather than trying to make a statement to the world,” said McAndrew, an expert on the psychology of mass killers.

“The fact that she was willing to kill herself without having killed a lot of other people first,” he said, “indicates the killing of a lot of people wasn’t her primary goal.”

McAndrew believes Aghdam planned the attack.

“It is a little suspicious,” he said. “You go 39 years without a gun, then you get a gun and go out and do something like this. One has to think she wasn’t buying it for protection.”

He noted that, short of banning handguns, none of the proposed gun control measures — such as beefing up background checks and banning assault weapons — apparently would have stopped Aghdam. As far as anyone knows, she had not been diagnosed with mental illness.

Ismail Aghdam apologized for the harm his daughter did, issuing a statement saying that his family was in “absolute shock” and feeling the “utmost regret, sorrow for what has happened to innocent victims.”