The NYPD’s largest union called out Mayor Bill de Blasio on Sunday over a massive melee in Brooklyn that left three officers with minor injuries — and involved gunfire that a law-enforcement source called a ” ‘F–k you’ to the cops.”

One of the injured cops was head-butted by a teen who was allegedly caught with a canister of pepper spray, the NYPD said.

Bottles and other debris were also hurled at cops from above, but no one was hit by the projectiles that are believed to have been tossed out of windows or rooftops in the sprawling Marcy projects in Bedford-Stuyvesant, the NYPD said.

“How are cops supposed to do our job in this environment? When will @NYPDNews or @NYCMayor @BilldeBlasio stand up & say enough is enough?” the Police Benevolent Association fumed on Twitter.

“Chaos is running the streets. This is outrageous — we’re lucky it wasn’t worse.”

Several hours of unrest began around 11 p.m. Saturday when cops responded to complaints of a large group of drunk people, the NYPD said.

Violence erupted when when cops placed one person under arrest and onlookers turned on them, the NYPD said.

Cops used pepper spray against the crowd, and video posted on Instagram showed people using milk from a corner store to flush the irritant from their eyes.

Around 12:15 a.m., someone fired nine shots into the air from a rooftop in the housing project, a law-enforcement source said.

“It was a ‘F–k you’ to the cops,” the source said.

Four people were arrested in three incidents, the most serious of which involved Guillermo Lopez, 17, who was charged with assault on a police officer for allegedly head-butting a cop near Marcy and Park Avenues around 12:20 a.m. Sunday, the NYPD said.

Eleven people were also issued summonses for disorderly conduct.

The last of the four arrests — of a 22-year-old woman charged with obstructing governmental administration — took place around 2:40 a.m. near Marcy and Vernon Avenues, the NYPD said.

The three cops who were injured were all treated at local hospitals and released.

One cop who fell and cut his arm needed 12 stitches to close the gash, the NYPD said.

De Blasio, who spent Sunday in New Hampshire stumping for support in the Democratic presidential primary race, tweeted an invitation to city beaches and a video clip that shows him railing against “the 1 percent” before finally addressing the unrest in Brooklyn around 4:15 p.m.

“It’s really simple: anyone who attacks our NYPD officers WILL be caught and WILL pay the consequences,” Hizzoner wrote.

“An investigation is underway into last night’s incidents in Brooklyn. If you have ANY information, please contact the NYPD IMMEDIATELY.”

De Blasio also called and spoke to the cop who needed the stitches, a spokesperson said.

Additional reporting by Nolan Hicks