BRUSSELS (AP) — Spain was thrown into turmoil on Thursday by court rulings that could undermine Prime Minister Pedro Sánchez’s hopes of forming a new government and force fresh elections in the region of Catalonia.

In a potentially stinging reversal for Spanish justice authorities, the European Union’s top court ruled that a former Catalán official serving a prison sentence for his role in a banned independence referendum two years ago had the right to parliamentary immunity when he was on trial.

A court in Spain, meanwhile, found that Catalonia’s current president, Quim Torra, is unfit to hold office for 18 months for disobeying the country’s electoral board, a decision likely to lead to more elections in a region riven by protests.

Pro-separatist Catalán politicians applauded and shouted “freedom” after the Luxembourg-based European Court of Justice ruled that former Catalán regional Vice-President Oriol Junqueras had earned the right to immunity when he was elected to the European Parliament in May.

The ECJ said that people like Junqueras who are elected as EU lawmakers “enjoy, from the moment the results are declared, the immunity” to travel to and take part in parliamentary sessions.

After the verdict, Junqueras, 50, tweeted: “Justice has come from Europe. Our rights and those of 2,000,000 citizens who voted for us have been violated. Annulment of the sentence and freedom for all! Persist as we have done!”

Junqueras was sentenced in October to 13 years in prison for sedition. Eleven of his associates were found guilty and eight of them also received prison terms.

He was in pretrial detention when he was declared to have won the European Parliament seat. But Spain’s Supreme Court refused to allow him to leave prison to take an oath to respect the Spanish Constitution, a national requirement for politicians to serve as EU lawmakers

The Spanish electoral commission later declared Junqueras' seat vacant and suspended his parliamentary prerogatives.

The ECJ made no criticism of his trial, and it's unclear if the court's ruling might secure his release. However, it could seriously complicate the efforts of Spain’s ruling Socialist party to form a new government following an inconclusive election last month.

The Socialists won the most seats but not a majority. They're now stuck in tortuous talks with Junqueras’ Catalán Republican Left party, hoping the party's representatives in the national parliament will not vote against Sánchez's government and let the Socialists hold onto office, albeit with a minority.

With the court ruling in Junqueras’ favor, the Catalán Republican Left could feel emboldened to press its demand for Catalonia’s right to self-determination to be recognized, something Spain doesn’t even remotely contemplate.

Spain's Supreme Court said it would study the full ruling and gave prosecutors and defense lawyers five days to present their arguments, after which a decision will be made.

Spain’s state prosecutor’s office asked the Supreme Court to inform the European Parliament that Junqueras is no longer in preventive custody, as the ECJ ruling said, but has been convicted and sentenced.

The prosecutor's office wants Junqueras to serve his time and for the EU assembly to drop his parliamentary status.

At a plenary session in Strasbourg, France, lawmakers applauded politely as EU parliament President David Sassoli read out the ECJ decision, describing it as a “very important ruling” for the makeup of the 751-seat assembly.

He tasked the parliament’s legal services with working out the implications of the verdict.

The case is likely to set an important precedent for fugitive former Catalán leader Carles Puigdemont, who was also elected to the European Parliament in May and has been living in exile in Belgium. Spain wants him extradited on charges of sedition and embezzlement.

Puigdemont has also launched an appeal at the ECJ. A court in Brussels ruled earlier this week that it would await the outcome of his European case before deciding whether to extradite him.

At his residence outside Brussels on Thursday, Puigdemont and former Catalán health minister Toni Comín cheered and applauded the verdict along with other regional politicians joining them by teleconference.

“If we can be at the European Parliament, and we will be, it is because we were able to fight for our rights from an independent space, with legal guarantees, which is something that doesn’t happen in the Spanish state,” Puigdemont said.

The verdict in the Spanish case against current Catalán leader Torra came after clashes between riot police and protesters during a soccer match between Barcelona and Real Madrid as authorities struggled to keep separatist activists from disrupting the game.

Torra was banned from holding office for disobeying Spain’s electoral board by not removing secessionist symbols from public buildings in the northeastern region during an election campaign.

Torra, who is fervently in favor of independence and Catalonia's right to self-determination, told the court he did not believe the electoral board had the right to order him to remove the symbols.

Torra said his lawyers would appeal but added in a statement that he has “zero confidence” in Spain's legal system, saying the ruling against him was politically motivated and aimed at the “repression” of Catalonia’s desire for independence.

The 2017 referendum on whether Catalonia should break away received an overwhelming "yes” vote but was marred by a police crackdown. Those in favor of the northern region remaining part of Spain largely didn't cast ballots after Spain’s central government declared the vote illegal.

Subsequent regional elections have indicated that the relatively wealthy northeast region of 7.5 million people is evenly split between those in favor and those against independence, and the divisions are barely hidden.

___

Giles reported from Madrid. AP video reporter Mark Carlson in Waterloo, Belgium and Barry Hatton in Lisbon contributed to this report.

Copyright 2019 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.