SAN JUAN, P.R. — An explosion and fire at an electric substation threw much of northern Puerto Rico into darkness late Sunday in a setback for the territory’s efforts to fully restore power more than five months after Hurricane Maria started one of the longest blackouts in United States history.

The island’s Electric Power Authority said several municipalities were without power, including parts of the capital, San Juan, but officials were optimistic it could be restored within a day as they worked to repair a substation that controls voltage.

The blast illustrated the challenges of restoring a power grid that was already crumbling before it was devastated by Maria, a Category 4 hurricane.

In many cases, power workers are repairing equipment that should have long been replaced but remained online because of the power authority’s yearslong financial crisis. The power authority is worth roughly $4 billion, carries $9 billion in debt and has long been criticized for political patronage and inefficiency. It also struggled with frequent blackouts, including an island-wide outage in September 2016.