MEXICO CITY – The surprise announcement that Donald Trump would visit Mexico on Wednesday triggered a deluge of negative reactions, with many Mexicans criticizing their president for inviting a U.S. candidate who has vowed to seal the border with a wall and deport millions of immigrants.

The question many have for Mexican President Enrique Peña Nieto, struggling through the latter half of his difficult six-year term, is: why?

As Peña Nieto and his office explained in tweets Tuesday night, he extended invitations to both the Republican presidential candidate and Democratic nominee Hillary Clinton last Friday, and Trump accepted. Peña Nieto wrote that he believed in dialogue to "promote the interests of Mexico in the world, and, principally, to protect Mexicans wherever they are."

But many Mexicans blasted Peña Nieto for the decision. Former President Vicente Fox, an outspoken Trump critic, said on Mexican television that the visit would be an opportunity for Trump to mock Peña Nieto on his home turf.

Fox told Milenio TV that Trump is not welcome and that "he has offended us, he has deceived us, he has discriminated against us."

The outrage came from many sectors: politicians, former officials, Mexicans at home and abroad. Some were upset that Peña Nieto would offer Trump an opportunity to act presidential — meeting a head of state on a foreign visit — at a time when he has been slipping in the polls.

"This legitimizes Trump & his xenophobia & sends the message that there is no cost bashing Mexico & Mexican migrants," Arturo Sarukhan, a former Mexican ambassador to the United States, wrote on Twitter.

Peña Nieto's decision to sit down with Trump appears to have been closely held. Several Mexican officials and diplomats contacted Tuesday had no notion that Trump had even been invited, let alone planned to visit the next day. When the news broke, Mexico's foreign minister, Claudia Ruiz Massieu, was in Milwaukee for the opening of a new Mexican consulate. Members of her staff said they were unaware of a possible Trump visit.

Peña Nieto is a cautious, scripted president, and some suggest he may actually be hoping to encourage Clinton to visit Mexico — and invited Trump as a formality, expecting he wouldn't accept.

Once seen as a dashing reformer who opened up Mexico's oil sector to foreign investment, took on long-standing monopolies, and proposed ambitious changes to the education system, Peña Nieto, has lost much of the momentum he had on taking office in late 2012. A poll this month in Mexico's Reforma newspaper put his approval rating at 23 percent, the lowest in the two decades that the newspaper has been conducting the survey of presidential popularity.

The oil reform has yet to take off amid low global prices for petroleum. The murder rate declined early in his term but has risen again, jumping 16 percent in the first five months of this year over the same period last year.

And the president has been beset by scandal. A favored government contractor bought houses on behalf of his wife and finance minister. Forty-three students from the state of Guerrero disappeared in 2014, and massive protests erupted when information emerged suggesting that they were captured and killed with the help of police.

Earlier this month, Mexico's human rights commission reported that 22 of 42 suspected drug cartel members killed at a ranch in Michoacan last year were allegedly executed by police and not killed in a gunfight as police had claimed. That contributed to the ousting of the head of the federal police this week.

Peña Nieto's Institutional Revolutionary Party struggled in the midterm elections in June, losing governorships it had long held.