A federal bankruptcy judge approved a major debt restructuring plan for Puerto Rico on Monday in the first deal of its kind for the U.S. territory since the island's government declared nearly four years ago that it was unable to repay its public debt.

The agreement involves more than $17 billion worth of government bonds backed by a sales-and-use tax, with officials saying it will help the government save an average of $456 million a year in debt service. The deal allows Puerto Rico to cut its sales-tax-backed debt by 32 percent but requires the government to pay $32 billion in the next 40 years as part of the restructuring.

Senior bondholders, who hold nearly $8 billion, will be first to collect, receiving 93 percent of the value of the original bonds. Junior bondholders, many of whom are individual Puerto Rican investors and overall hold nearly $10 billion, will collect last and recover only 54 percent.

Get Breaking News Delivered to Your Inbox

"Puerto Rico has taken an important step toward its total financial recovery," Gov. Ricardo Rossello said in a statement. "This represents more than $400 million annually that will be available for services in critical areas such as health, education, pension payments, and public safety, in compliance with other obligations."

The deal was previously approved by bondholders but prompted hundreds of people to write and email Judge Laura Taylor-Swain, who held a hearing on the issue nearly three weeks ago, to express concerns about the government's ability to make those payments and the effect it will have on public services. In her ruling, she wrote she reviewed and carefully considered all those messages before making a decision.

"Many of the formal and informal objections raised serious and considered concerns about the Commonwealth's future ability to provide properly for the citizens of Puerto Rico who depend upon it," she wrote. "They are not, however, concerns upon which the Court can properly act in making its decision ... the Court is not free to impose its own view of what the optimal resolution of the dispute could have been."

The judge said that the deal represents a reasonable compromise and that further litigation would present a "significant gamble" for Puerto Rico. The island is mired in a 12-year-old recession and struggling to recover from Hurricane Maria as the government tries to restructure a portion of its more than $70 billion public debt load.

A U.S. government report issued last year said Puerto Rico's public finance problems are partly a result of government officials who overestimated revenue, overspent, did not fully address public pension funding shortfalls and borrowed money to balance budgets. The Government Accountability Office also reviewed 20 of Puerto Rico's largest bond issuances over nearly two decades and found that 16 were issued solely to repay or refinance debt and fund operations, something many states prohibit.

Taylor-Swain's ruling said the compromise is "admittedly, deeply disappointing to countless citizens of Puerto Rico and investors in Commonwealth bonds."

A federal control board that oversees the island's finances praised the ruling, saying in a statement the bond restructuring will help revive Puerto Rico's economy.

"The deal demonstrates ... our determination to resolve Puerto Rico's debt crisis and establish sustainable foundations for (the) island's economic road to recovery," said Natalie Jaresko, the board's executive director.

Antonio Fernos, a Puerto Rico economist, said in a phone interview the agreement is a good deal.

"It's positive because it brings some clarity to bondholders and what the board and government are willing to accept in negotiations," he said.

More challenges remain, with Puerto Rico's government still negotiating with those who hold general obligation bonds.

Last month, the control board asked the judge to invalidate $6 billion worth of that debt, including all general obligation bonds issued in 2012 and 2014, alleging issuance violated debt limits established by the island's constitution. Taylor-Swain has held hearings on the issue, but has not ruled yet.

In November, Puerto Rico's government reached a debt-restructuring deal with creditors holding more than $4 billion in debt issued by the now-defunct Government Development Bank.