Tehran - The arson attack on Saudi Arabia's embassy in Tehran that prompted Riyadh to cut diplomatic ties with Iran was initially seen as a victory for hardline opponents of President Hassan Rouhani.
But analysts say the incident could ultimately backfire on his foes ahead of Iranian parliamentary elections next month, giving the moderate Rouhani ammunition as he confronts conservatives and works to restore his country's international reputation.
Although similar episodes have occurred in the past - Britain's embassy was stormed in 2011 for example - Rouhani's response signals a shift against the "rogue elements" he condemned for Saturday's attack.
The violence has undermined the president's efforts in the Middle East and beyond to bring Iran out of its relative isolation - exemplified by last year's deal with world powers on its nuclear programme.
His government has maintained its criticism of Saudi Arabia's execution of Shiite cleric Nimr al-Nimr, which sparked the angry protests at the Tehran embassy and Riyadh's consulate in Mashhad, Iran's second-largest city.
The row has reduced opportunities for Rouhani to engage with Arab states - several Saudi allies also cut or reduced ties - and benefited those who want to stop his efforts at rapprochement.
"The reaction at the embassy changed the game against us and helped Saudi Arabia and the radicals," Amir Mohebbian, a political strategist close to Iranian Foreign Minister Mohammad Javad Zarif, told AFP.
"Public opinion, not only in Iran but the world, had been against Saudi Arabia but the action of the radicals made the situation bad for Iran in handling our relations with Western countries and in the region." Although 50 arrests were quickly made, the ransacking of the embassy was condemned across the world, including by the UN Security Council.
Such a setback for Rouhani - who Wednesday wrote to Iran's judiciary chief to demand that those accused of orchestrating the violence be dealt with urgently - came after years of trying to improve Iran's international image.
In contrast, the scenes at the embassy included petrol bombs being thrown and people climbing onto the building's roof to tear down the Saudi flag, while some took selfies with items they had stolen from inside.
Hardline organisations including the Revolutionary Guards and its junior Basij militia publicly criticised the embassy attack and denied any involvement.
The rift between competing political factions, however, has been visible in Iranian media, with reformist newspapers accusing hardline groups in the regime of fuelling the crisis in a bid to harm Rouhani at the polls.
But given that the violence "will likely only have costs for Iran" Rouhani may seek to confront the issue, said Mohebbian, a moderate conservative who has advised top politicians.
"Maybe this should be the start of something to manage the radical people, whose actions are not rational," he said, noting that supreme leader Ayatollah Ali Khamenei's comments about the violence would be vital.
Though yet to address the attack, Khamenei has previously spoken out against similar incidents.
When the British embassy was attacked in 2011, after a new round of sanctions was imposed on the Islamic republic as punishment for its nuclear programme, Khamenei later said "the emotions of youths involved were right, but their actions were not." Unlike Rouhani this week, Iran's then president, the hardline Mahmoud Ahmadinejad, remained silent four years ago.
Ellie Geranmayeh, an Iran specialist at the European Council on Foreign Relations, said the recent violence had limited the space for diplomacy on regional crises.
"This seemed to be the result that hardliners in Saudi and Iran wanted," she said, noting that Riyadh knew Nimr's execution would "bear costs" with Shiite governments but it was a price it seemed willing to pay.
The reaction from Rouhani and other officials in condemning the attack, however, may be a harbinger of a dramatically different result.
"Ironically, Iran's hardliners may have given Rouhani more ammunition to convince the supreme leader to marginalise the radical elements of the system and to continue participating in multilateral diplomacy," such as in resolving the conflict in Syria, Geranmayeh said.
Only by doing so will Iran "recoup its standing among the international community," she added.