Donald Trump's visit to Mexico was not hastily put together, but the result of a couple of weeks of closely guarded work, sources close to the campaign told NBC News.
Trump insiders quietly reached out to officials in Mexico with the goal being to prompt an invitation from President Enrique Peña Nieto to meet Trump in person, the sources said.
Peña Nieto said he invited both Trump and Democratic nominee Hillary Clinton to visit. Trump met with Peña Nieto Wednesday, in a visit hours before Trump's speech on immigration in Arizona.
Trump's son-in-law Jared Kushner led the effort to secure a meeting, the sources said. With receptive signals coming back from Mexico, Team Trump waited for the formal invitation they had set in motion. Following decorum, Mexican officials extended an invitation to Hillary Clinton as well.
The sources acknowledged that Trump expressed concern that he could face criticism for appearing politically weakened by meeting the Mexican president. Trump began his campaign by accusing Mexico of sending criminals and rapists across the border, and has made building a wall on the U.S.-Mexico border a key policy proposal.
According to sources, Trump agreed to make the Mexico trip only if he could deliver his immigration address in his way, the very same day.
Insiders say Trump understood and ultimately agreed he could benefit from appearing on the same stage with a world leader and conveying a more controlled manner.
But Trump did not want to lose the fire that is his brand on immigration. He agreed to the more measured five points he outlined in Mexico during his team's planning over the past few days with help from top advisers.
Those connected to the Trump campaign also recognized the Mexico trip carried a variety of risks politically but with strong family support, Trump agreed it was a risk worth taking, insiders said.
One of those risks was provoking new criticism from the Mexican president or other officials.
Some of that has already begun with tweets from Peña Nieto that appear to contradict Trump's statement that the wall was not discussed. Peña Nieto said on Twitter after the meeting that "At the start of my conversation with Donald Trump I made it clear that Mexico will not pay for the wall."
Clinton campaign chairman John Podesta said in a statement: "It turns out Trump didn't just choke, he got beat in the room and lied about it." More criticism may come after Trump delivers an expected red meat speech.
Trump's insistence that the visit and immigration speech occur on the same day is what led to the on again-off again optics about the campaign's schedule and speech timing over the last week.