The Spanish prime minister's Catalan migraine just went from bad to head-splitting.

Pedro Sánchez's already fraught efforts to form a government were complicated by a ruling from Europe's top court on Thursday that former Catalan Vice President Oriol Junqueras should have been allowed to leave jail to take up his seat in the European Parliament.

Junqueras, elected as an MEP in the May EU election, has since been sentenced to 13 years in prison over his role in an outlawed 2017 independence referendum. But regardless of the vexed legal question of whether Junqueras will walk free, the court's ruling has had an immediate destabilizing impact on Sánchez's attempts to secure power.

Thursday’s ruling prompted Junqueras’ pro-independence party, the Catalan Republican Left (ERC), to freeze negotiations with Sánchez's Socialists (PSOE).

“We will not sit down with the PSOE until we know the assessment of this ruling and what the state's lawyers will do,” said ERC spokeswoman Marta Vilalta. “Now is the time to handle this politically and to enact justice. We won't rest until we get it.”

Spain blocked Junqueras from taking his seat, saying that under national law he needed to swear an oath to the Spanish constitution

Sánchez has been struggling to form a government since November’s general election, the second of the year, left Spain's political landscape deeply fragmented. His Socialists agreed a preliminary coalition pact with the far-left Podemos, but since the pair don’t have enough seats to form a majority, he had been planning to win an investiture vote by persuading ERC to abstain.

The ERC has demanded fully fledged negotiations with Madrid over the Catalan independence question, which Sánchez has been reluctant to do. The court ruling in their jailed leader’s favor now emboldens them to demand further concessions.

"The key issue here is how it affects the [investiture] negotiations. It seemed like both sides were close to an agreement at the beginning of the week but this creates a hurdle," said Antonio Barroso, a Spanish politics expert at political risk consultancy Teneo.

Question of immunity

In October, Spain's Supreme Court jailed Junqueras for 13 years for his role in the failed 2017 independence referendum. Junqueras, the highest-ranking Catalan official to face trial, was found guilty of sedition and misuse of public funds and was also banned from holding public office for 13 years.

He has been behind bars since his arrest in November 2017, meaning he was detained for over 16 months before the trial began in February this year.

In its ruling Thursday, the Court of Justice of the European Union found that Junqueras should have enjoyed the immunity afforded to members of the European Parliament from the moment the EU election results were declared, and also should have been able to travel to the Parliament for its first session in July.

Spain blocked Junqueras from taking his seat, saying that under national law he needed to swear an oath to the Spanish constitution. The Supreme Court disallowed him from leaving jail in order to do so.

The EU court said that the immunity Junqueras should have received "entails lifting any measure of provisional detention imposed prior to the declaration of that Member’s election, in order to allow that person to travel to and take part in the inaugural session of the European Parliament."

Although in its carefully worded statement the court did not call for Spain to release him, that has not stopped ERC, of which Junqueras remains president, stepping up calls for his immediate release.

Both Spanish and European Parliament authorities are still assessing how to handle the ruling.

Spain's government said in a statement that it is now up to the Supreme Court to decide how to comply with what the EU court decided.

European Parliament President David Sassoli said he had asked the legislative body’s legal services to evaluate how the Parliament itself should respond to the ruling

"The state's lawyers are carefully studying the ruling and will present their response in the coming days," it said.

European Parliament President David Sassoli said he had asked the legislative body’s legal services to evaluate how the Parliament itself should respond to the ruling, but also called on "the competent Spanish authorities to align with the ruling."

Politics at home

Thursday's ruling may also have significant ramifications for ex-President of Catalonia Carles Puigdemont and the region's former health minister Toni Comín, both of whom were also elected as MEPs this year and avoided imprisonment in Spain by fleeing to Belgium.

They are now stepping up attempts to take their seats in the European Parliament.

The prospect of a snap election looked more likely on Thursday after a Barcelona court ruled that current Catalan President Quim Torra should be barred from holding public office for 18 months

But Puigdemont and Junqueras, although aligned on the aim of Catalan independence and both wanting to exercise their roles as MEPs, are also competing for votes for their parties back home. Puigdemont is a member of the Together for Catalonia party.

"[ERC] wants to retain its influence in Spanish politics but if [Junqueras] is seen as being too supportive of the Spanish government, then he's afraid that Puigdemont will use it to essentially attack them and say that they are not true to the independence cause," said Barroso.

"It creates a fear that Puigdemont might use this, which is what everyone is fearing in the ERC, to push for an early election next year."

The prospect of a snap election looked more likely on Thursday after a Barcelona court ruled that current Catalan President Quim Torra should be barred from holding public office for 18 months over his refusal to order the removal of separatist symbols from public buildings. Torra has said he would appeal, but if the ban holds, it could lead to a new ballot.