When Theresa May arrived for lunch with Jean-Claude Juncker just after 1pm Brussels time on Monday, it seemed that a deal on the Irish border was done.

In reality, language on the key text was being changed back and forth right up until noon, while the British Prime Minister was still in transit.

Despite weeks of effort, however, the lunch that was to deliver the deal was blown up by a fateful phone call.  It was to be the most bizarre day yet in the Brexit process.

The day began with a text message at 7am (6am Irish time) from an EU source to say there had been no breakthrough in discussions during the night. 

On RTÉ's Morning Ireland, Minister for Foreign Affairs Simon Coveney, struck a gloomy note. "We have had a discussion on text," he said, "but we don't have agreement yet. We're not quite yet where we need to be."

That draft was the result of weeks of painstaking negotiation between the EU, UK, and in the latter stages, Irish officials.

The previous Thursday, 30 November, had seen an intense and difficult round of talks. But a joint EU-UK draft emerged.

The text was entitled: 'Joint Report from the Negotiators of the European Commission and the United Kingdom Government on Progess During Phase I of Negotiations under Article 50.'

At around 11.45am on Monday, just before Mrs May’s lunch date, RTÉ News got sight of a key paragraph in the draft text.

The paragraph read: "In the absence of agreed solutions the UK will ensure that there continues to be no divergence from those rules of the internal market and the customs union which, now or in the future, support North South cooperation and the protection of the Good Friday Agreement."

This was a startling development.

There had been no indication at all that Britain was prepared to concede any notion of "no regulatory divergence", a concept interpreted by many as akin to Northern Ireland staying inside the EU’s single market and customs union while the rest of the UK left.

At 11.48am a second source confirmed that the paragraph was accurate, but that it had been revised to replace "no regulatory divergence" with "continued regulatory alignment."

With confirmation from a second source, RTÉ News felt confident to run a story online that the UK had essentially conceded the principle of Northern Ireland cleaving closely to EU rules, so as to satisfy Irish demands that Brexit would not produce a hard border.

I tweeted the draft paragraph on "no regulatory divergence" at 12:16pm Brussels time, sending out a second Tweet at 12.20pm to say the draft text had been updated to reflect "continued regulatory alignment."

Around the same time the RTÉ website published the story, stressing it was a draft and that it was not yet clear if it had been accepted by both the British and Irish governments.

In fact, at the moment the story was published, EU and UK officials had only just agreed the final text.

While "no regulatory divergence" and "continued regulatory alignment" may appear pretty much the same thing to most people, there were vital differences of meaning and interpretation that bedevilled the process right up until the last minute.

In fact, the two phrases were being inserted back and forth into the draft text that Monday morning until noon.

What is now clear is that the phrase "no regulatory divergence" was in the draft since Thursday, and remained in the text right through the weekend until Monday morning.

But London was clearly unhappy with it.

To UK eyes "no regulatory divergence" bound Northern Ireland to not deviate from the rules of the single market and customs union.

It was too tight a formulation.

"Continued regulatory alignment" had more flexibility and more ownership. The UK would voluntarily opt back up close.

On Friday night, 1 December, the Irish Government provisionally scheduled a special cabinet meeting at 9am on Monday morning.

Despite the misgivings of Irish officials in Brussels that things were still in flux, it was expected that all sides would be in a position to agree the text, with some tweaking here and there over the weekend.

But the weekend dragged on. On Sunday evening a phone call was lined up between Theresa May and Leo Varadkar, but it did not materialise.

That meant that when the Cabinet met at 9am on Monday morning, there was still no agreed text.

The Cabinet meeting was a tense affair. Ministers were asked to leave their phones and iPads outside to avoid any leaks.

There was a conscious acknowledgement that this was a historic moment, and concern that nothing should be done to scupper it.

The meeting lasted around two hours. Leo Varadkar was essentially asking the Cabinet to give their approval in advance for a text that had still to be finally nailed down.

He made a long and detailed presentation on the talks so far, how the "parameters" of how to avoid a hard border had to be agreed before Ireland would accept a green light for Phase 1 of the negotiations to move into Phase 2.

He also spelled out the progress which had been achieved on protecting the Common Travel Area (CTA), an issue which had obvious repercussions for a number of ministers and departments.

Mr Coveney also made a presentation on where things stood. The issue went around the table. 

Minister for Transport Shane Ross said the Taoiseach was effectively asking the Cabinet to make him plenipotentiary to agree a deal that was not yet secure.

Martin Fraser, the Secretary General, also made a strong intervention, his experience and knowledge of Northern Ireland adding a gravitas appreciated by those listening.

Just before 11am Irish time, Mr Fraser left the Cabinet meeting and returned to say that Jean-Claude Juncker was on the phone to speak to the Taoiseach. 

Mr Varadkar left the room and Mr Coveney resumed the meeting just before it drew to a close.

Mr Juncker told the Taoiseach that Britain had accepted the joint text.

Mr Fraser then returned to the Cabinet room and suggested that Mr Coveney join the Taoiseach. 

A call then came from Donald Tusk, the President of the European Council. 

The deal was on. The Department of the Taoiseach announced that Mr Varadkar would make a statement in Government Buildings at 2.30pm Irish time.

A short time later, Mr Tusk fired off a light-hearted Tweet: Tell me why I like Mondays! Encouraged after my phone call with Taoiseach @campaignforleo on progress on #Brexit issue of Ireland. Getting closer to sufficient progress at December.

Just before he hosted the British Prime Minister lunch, Jean-Claude Juncker met the Brexit Steering Group in the European Parliament. He told them that the European Commission and British negotiators had agreed the text. 

They were just waiting for approval from the Irish Cabinet.

"Juncker was very clear there was no problem with the draft on Ireland," says one source who was present.

"They were waiting on word from Dublin on a text which had been agreed between the British and the Commission. They were confident they would get a positive response.

"There was no question that there was a problem back in London with the text."

Shortly afterwards, Committee member Philippe Lamberts, a Belgian Green MEP, briefed journalists outside the European Commission’s Berlaymont headquarters.

He showed how the text had called for "continued regulatory alignment from those rules of the internal market and the customs union which now or in the future support north south cooperation and protection of the Good Friday Agreement".

He was confirming RTÉ's story.

At around 12:30pm Brussels time, Theresa May arrived in a fleet of limousines, sweeping past reporters and into the Berlaymont building for her working lunch.

By that stage the draft text was "closed", meaning officials had signed off on it. It just needed Mrs May’s and Mr Juncker’s political imprimatur.

The mood in Dublin was warming. In the markets, sterling was on the rise.

On RTÉ's News at One, Mr Coveney said: "Certainly the indications we have is that we are in a much better place than we have been in Brexit negotiations to date.

"We have now a language that gives us the safeguards we need; that there is reassurance for people there is not going to be a re-emergence of a border."

However, as news of the two versions of the draft text spread, the mood was much darker in DUP headquarters.

Jeffrey Donaldson MP dismissed the report. "That is not our understanding of the UK Government's position," he said.

The DUP hastily organised a statement by leader Arlene Foster.

"We will not accept any form of regulatory divergence which separates Northern Ireland economically or politically from the rest of the United Kingdom," she said.

"The economic and constitutional integrity of the United Kingdom will not be compromised in any way."

Twenty minutes later, Mrs May left the lunch and held a 15-20 minute phone call with Ms Foster.

A pale Theresa May came back to the lunch table. The deal, which had seemed so close, was dead.

The Taoiseach was forced to postpone the 2.30pm statement. 

As the news sank in, Sterling began to fall again.

The Government called a new press announcement. The Taoiseach, Tánaiste Simon Coveney and Helen McIntee, the Minister for European Affairs, walked on to the podium grim-faced. 

Mr Varadkar expressed surprise and disappointment at the scuppering of a text which had been agreed.

Suffice to say, there was thinly disguised fury in Brussels and Dublin.

In Brussels, officials expressed bewilderment that the DUP had not been kept in the loop by Mrs May as the evolving text waxed and waned along the semantic faultline of regulatory convergence and regulatory alignment.

The DUP turned its guns on the Taoiseach, suggesting that Dublin’s position could force Theresa May to walk away from the negotiations: "The Irish Republic would suffer far worse economically from no trade deal than the UK.

"Mr Varadkar may try to appear calm on the surface but he is playing a dangerous game — not with us but with his own economy."

Christopher Montgomery, a former DUP Chief of Staff and a director of Friends of the Union, went on the BBC Radio 4 Today and the World at One programmes, claiming that Dublin had deliberately leaked the text and accusing this correspondent of misreporting events and causing the process to collapse.

Andrew Murrison, a Eurosceptic Tory MP and chair of the Northern Ireland Affairs Select Committee told the Financial Times: "It was simply a breakdown in communication and a misunderstanding between the government and the DUP."

By Thursday the temperature in the rhetoric had yet to cool. The Eurosceptic MP Bernard Jenkin said on the Today Programme that "the European Commission is playing Ireland like a harp."

But contacts were revived. The view in Brussels on Wednesday was that Theresa May was the author of her own misfortune and she needed to stew for a short period.

On Thursday morning the European Commission said the definitive deadline for Mrs May to get agreement was midnight on Sunday.

In Dublin, the Fine Gael view since Monday was that Ireland could not be seen to assume ownership of the crisis: this was a problem between Theresa May and the DUP. 

Ireland would not be offering solutions. 

Ministers were reminded to say nothing that might give the impression Dublin would step in to help.

On Thursday morning, senior EU officials in Brussels were speculating that Mrs May would try to get a new text past the DUP before the end of the week, as the clock really was ticking. 

It would only take an hour or so for the Prime Minister to fly back to Brussels; it might even happen on Thursday afternoon.

In Dublin, the Taoiseach is thought to have received signals on Thursday afternoon that British Prime Minister would make a second attempt that night, or early on Friday morning.

A long pre-arranged Cabinet dinner had been scheduled for Thursday evening.  As the dinner drew to a close, more and more calls were coming in. By the end the Cabinet felt something was in the offing.

In Brussels the office of Donald Tusk announced he would make a statement at 07:50 on Friday morning. 

A spokesman clarified that Mr Tusk was due to fly to Hungary at 8.15am and he would be making a statement whether Theresa May was ready or not.

This is seen as an effort to keep the pressure on London.

At 7:45pm on Thursday evening, I received a text from a senior European Commission source that calls had taken place between Theresa May and Jean-Claude Juncker, with calls between Downing Street and the Taoiseach "ongoing".

"There’s a lot of activity," the source said. "There’s a desire to reach agreement tonight or tomorrow morning. Juncker is putting all his efforts into getting a deal."

Another source confirmed the activity, and that Mrs May was still working to get the DUP on board.

By 10pm Dublin time, the Taoiseach was aware that a deal had been done. He scheduled a press conference at 8.30am.

It was vital that the DUP were not able to fill a vacuum by announcing what the deal meant before the Irish Government could make its response, so the Irish news conference was timed to go immediately.

Shortly after the news conference was scheduled, word came that Mrs May would make a pre-dawn flight to Brussels, meeting Jean-Claude Juncker at 7.30am.

At 3.30am on Friday, a car took the British Prime Minister and her team, including Brexit Secretary David Davis, to RAF Northolt. They were whisked over the Channel to a freezing Brussels in the darkness.

At the Berlaymont headquarters, the British delegation was met by Jean-Claude Juncker, his chief of staff Martin Selmayr and other officials.

Upstairs on the 13th floor over croissants, juice and coffee, Theresa May, David Davis and her "sherpa", Olly Robbins, faced across from Juncker, Michel Barnier and Selmayr.

Shortly before 8am, Selmayr tweeted a picture of white smoke pouring from a chimney in the Vatican.

A deal was done, and this time the DUP were – albeit reluctantly, and with grave reservations – on board.

To the Irish Government, the text that was finally agreed on Friday morning was substantially similar to what had been in Monday’s text.

"It was quite incredible that nothing had changed during the week," says one senior Irish source. 

In fact, despite the drama of Monday, from the way the text had first been reported by RTÉ News, to the DUP’s reaction and Theresa May’s abortive lunch, the entire episode may have even significantly improved Ireland’s post Brexit position.

There was new language in the text about the East-West dimension, and about no barriers to trade between Northern Ireland and Great Britain.

Because the text also guaranteed that the UK would opt for "full alignment" with the single market and customs union if other solutions did not work to avoid a hard border, it meant that the UK as a whole would have to opt for high alignment with the EU regulatory system.

That would not only remove a hard border, it would also improve the prospects of a favourable trade deal that would safeguard the huge volumes of trade between Ireland and the UK.

"If anything that additional text was helpful from a Republic of Ireland point of view," says the source. 

"While it might park the issue for the moment, it significantly revives the chance that the single market and customs union is going to come back into play for the Brits, because in relation to the East West relationship, they’re going beyond just regulatory alignment on the island of Ireland. It’s going to be East-West as well.

"If anything, the DUP have inadvertently made Theresa May’s life more difficult down the road.  But Ireland’s chance of getting a more favourable outcome has been positively improved."

Part two of Tony Connelly's article, Brexit and the Irish border: How the deal was salvaged, will be available tomorrow morning.