The week began with a bang. On Monday John Bercow, the Speaker of the House of Commons, dropped a bombshell that will resonate through the twilight of Theresa May’s premiership.  

Mrs May was about to make a third attempt at getting the Withdrawal Agreement through the House of Commons, fresh from securing legally binding assurances on the backstop.

But for the second time in as many weeks she was laid low by the patrician tones of legal and political authority, first the Attorney General and now the Speaker of the House.

Bercow sensationally told MPs that a principle stretching back to 1604 prevented her from bringing the Withdrawal Agreement back for a third time unless there were "substantial" changes to it.

Ratification on Tuesday would have paved the way for the prime minister to seek a short "technical" extension of Article 50 in order to pass the necessary legislation.

Even if - as anticipated - Parliament failed to ratify she would have sought a longer extension, the threat of which would terrify Tory MPs into finally backing the Withdrawal Agreement.

Bercow’s declaration derailed that choreography.

Brexiteers were jubilant: A no-deal exit in just six days time looked more likely; remainers believed it strengthened the case for a softer Brexit, or even a second referendum.

On Tuesday, Andrea Leadsom led eight Brexiteer cabinet ministers in a full-scale revolt against Mrs May’s plans to seek a long extension.  

If one was necessary, 30 June was the absolute latest they were prepared to contemplate. If there was no-deal at that point, so be it.

"This used to be the cabinet that would deliver Brexit," Leadsom is reported to have declared, "and now from what I’m hearing it’s not".

Stung by the backlash, Mrs May retreated. "There is a case for giving parliament a bit more time to agree a way forward, but the people of this country have been waiting nearly three years now," was the message from Number 10.

"They are fed up with parliament’s failure to take a decision and the PM shares their frustration."

But her deputy David Lidington was bringing a different message to European Affairs ministers in Brussels. He told his counterparts that Mrs May could seek a long extension, but one that was "fungible".  

In other words, if the treaty was passed she would go short, but would go long if it failed.

But a longer one could be reduced in duration if the treaty, and the attendant legislation, were dealt with quickly.

"We had conflicting points of view," said one EU diplomat on Wednesday. "Mr Lidington was here yesterday. He went around many ministers on behalf of the PM. But this was overtaken by events in the UK."

Another EU source said: "While Lidington was here in Brussels briefing on a long extension, it was being shot down in Cabinet."

On Tuesday night Theresa May was due to write to the European Council President Donald Tusk with her extension request. Officials waited until 10pm and then gave up. No letter arrived.

None of this was going down well in EU capitals, but the anger that was building was not uniform. "It’s very messy," said one EU official.

"There’s a hardening on the EU side, but it’s not translating in the same way among all the leaders. There are those who say we need to go for a short [extension], just give them a month or a few weeks to make their minds up."

On Wednesday morning Mrs May’s letter landed. It was just 24 hours before leaders were due to arrive in Brussels, and nine days before the exit date.

The letter was a blend of explanation, recrimination and a plea for help. But it did conclude with a firm date: 30 June.

One diplomat, with commendable understatement, described the atmosphere. First there was supposed to be a letter on Tuesday. "Later we got the impression that there might not be a letter at all after the cabinet meeting. Then there was a letter, even including a specific date. It was a bit of a rollercoaster [in terms of] what the UK would do. It changed on a daily basis."

Mrs May also acknowledged that the EU would not  be changing the Withdrawal Agreement in the hope that changes might upgrade the motion to something that was "substantially" different.

However, if EU leaders were to formally endorse the legally-binding assurances on the backstop, agreed with Jean-Claude Juncker in Strasbourg, that would  help convince Bercow this was a new motion.

This was not necessarily a given: the Strasbourg deal was conditional  on Mrs May getting the treaty through the House of Commons.

She was clear that the UK would not tolerate having to contest the European Elections. She also spoke about bringing forward domestic legislation "to protect our internal market given the concerns expressed about the backstop," a signal to Tusk that efforts to bring the DUP on board were continuing.

On Wednesday Mrs May phoned Tusk. At 17:15pm Tusk issued a statement. A "short extension" would be possible, but only on the condition  that MPs ratified the Withdrawal Agreement the following week.

The demand for a 30 June extension had its "merits", but it created a "series of questions of a legal and political nature".

The plot thickened. Mrs May would get some kind of extension, but only if the treaty was approved by Westminster - a massive if.

Despite the rollercoaster effect, however, Donald Tusk and Theresa May had been engaged in an elaborate tango.

Ever since EU leaders were burned over the Withdrawal Agreement late last year, EU capitals have been wary of committing to anything that would simply be rejected by Westminster.

In turn Mrs May was wary of asking for anything that would limit her chances of getting the deal through the Commons, or of asking for something she wouldn’t get.

So, each move was carefully calibrated in terms of what both sides believed the other was prepared to give.

As such, the dynamics in Westminster - especially Tuesday’s furious cabinet meeting - instantly played in to the calculation.

"In the cabinet meeting," says one EU official close to the unfolding developments, "she was told, you shouldn’t even be entertaining a long extension.

"Then the moment she starts going for a short extension, we thought, in that case, we’re only going to look at what happens if  she wins. So there was a lot of interplay."

In other words, the constraints on Mrs May shaped what the EU was going to offer. But the EU offer in turn also shaped what the UK could ask for.

The tango would only work so long as both partners followed the same melody and rhythm. Everything was so political that only the 27 EU leaders themselves could decide on the extension, with the officials who would normally pre-cook a summit’s conclusions largely deferring to their political masters.

Even a short extension threatened a complicated collision with the European Parliament elections. A longer extension could  give the House of Commons more time to coalesce around a softer Brexit, arrange a general election or even prepare for a second referendum, but it was politically toxic for leaders.

Michel Barnier, the EU’s chief negotiator, had hinted at a longer extension changing the "political reality" in the UK.

But it was one thing for the European Commission to entertain this course of action and quite another for Mrs May’s fellow heads of government.

"I don’t think EU leaders will want to be part of the British democratic system," said one diplomat. "If we call for assurances it will not go further than [asking] the House of Commons [to state] what position they have on this issue. I don’t think we should mingle in each other’s domestic affairs."

But the European Elections were quickly becoming the fulchrum of the extension dilemma.

The 1976 European Electoral Act sets out the precise rules for member states to hold elections (1979 was the first year MEPs were directly elected). A reform of the act in 2002 requires member states to hold elections within a four day period, sets out electoral thresholds and prohibits MEPs from holding national parliament mandates as well.

From the get go, an extension of Article 50 was always going to get tangled up in the European Parliament elections, which were due to take place across 28 member states between May 23-26.

The initial thinking was that an extension could run right up to 2 July because it is only at that point that the newly elected MEPs will gather in Strasbourg to constitute the new parliament.

The Parliament’s own legal service took a sanguine view. There should be ways around the UK having to contest the elections.

It’s understood that experts in both the European Commission and the European Council were of the view that while the treaties - and the 2002 reform - were crystal clear on the need for member states to hold elections, the EU could take a pragmatic approach.

In other words, so long as Britain was out by the end of June, there shouldn’t necessarily be a big need for European Elections to be held in the UK.

However, as the summit approached the Commission hardened its stance. The secretary general Martin Selmayr took a firm line that if an extension drifted beyond election day then the UK needed to hold elections.

Therefore the extension would have to be 22 May, and not 30 June.

"The legal advice in the Council and the Commission shows a very similar understanding of the situation," says a senior EU official. "The legal situation changes when we come to the 23rd of May, because there is a legal obligation for the UK to hold elections, and it is the right of citizens in the UK, both UK citizens and others, to participate in such elections."

Those elections were brightly visible on national radars. A number of countries have already legislated to receive extra seats, distributed from the UK’s quota of 73.

More importantly, from the EU’s sense of its own institutions, any risk that waiving the UK’s obligation to hold elections might undermine the legality of the new parliament - and the new Commission it would approve - would mean importing the mess of Brexit into the EU for years to come.

Ahead of the summit Theresa May met Tusk at 14.30pm on Thursday afternoon. At 16.00pm she addressed leaders for an hour and forty-five minutes. She was asked why she was focussed on 30 June and not 22 May, and what she felt the chances were of the treaty being approved. What if she didn’t succeed?

"The question and answer session was conducted in a civilised and friendly atmosphere," says the official. "But it left leaders with the impression that chances of an early vote succeeding were not very high. Some put it at 10%, some at 5%."

Mrs May left the room at 17.30pm and remained in the UK delegation quarters for the rest of the evening, meeting Tusk half way through proceedings and again later when things concluded.

Leaders began with a six paragraph draft text, with a reference to 22 May as an extension date. Anything later would run the risk of undermining the European Elections, anything earlier might not give Mrs May enough time to get the legislation through parliament.

The French president suggested 7 May, two days before EU leaders are due to gather in the Romanian city of Sibiu for a long-scheduled post-Brexit rendezvous.

Leaders also struggled over whether the offer should be conditional on the vote getting through the Commons, or whether it should be unconditional.

Some leaders argued that an unconditional offer would have taken the immediate pressure off Mrs May (and avoided the need for them to return to Brussels one week hence).

With dates and conditionality ebbing and flowing, Tusk broke the meeting for dinner. It was in the interval before leaders sat down to eat that they began redrafting the text.

It would be five hours before they finally reached consensus (one diplomat groaned that leaving leaders to draft a text without officials present was never a good idea), but they had at least reached a compromise.

The offer was conditional on Mrs May holding another vote next week. If the Commons approved the Withdrawal Agreement the extension would run till 22 May.  

If not, the UK would have until 12 April - the point at which the electoral authorities would have to announce plans to hold European Elections - to come up with a way forward.

The senior EU official says: "Beyond that date there can’t be elections, until then all options are open. The default option remains no-deal. If nothing happens it means no-deal at the end of 12 April."

The overall objective is that the EU be seen to offer Mrs May (and the House of Commons) as much leeway as is legally and politically possible, while leaving the finger of blame pointing at London if no-deal comes to pass.

It is a risky strategy, as it presupposes the House of Commons is prone to the leverage of elections or no-deal.

However, the hope is that MPs would now start to seriously engage in a cross party consensus. The non-binding Political Declaration would still accommodate fresh language that might reflect that consensus - a permanent customs union/partnership, say, or a Norway Plus model (also referred to as Common Market 2.0), which would mean membership of the single market and customs union.

Failing that the Commons might yet agree on a second referendum, as a condition for approving the Withdrawal Agreement, or events might natural tumble into a general election.

But each of these options comes with a high political price tag, and each will face furious resistance from one camp or other.

"We all find it difficult to imagine what might happen," says the senior EU official. "The general atmosphere is such that it is absolutely impossible to predict. The situation is so dynamic."

On Friday night Theresa May wrote to MPs, summarising the EU’s offer and hinting that she might not bring the Withdrawal Agreement back to the Commons next week after all.

A no-deal Brexit may have been given a two week reprieve, but the House of Commons has proved itself unable to find a consensus - even with two years to spare.