The recent Iran-US escalation in the Middle East caused much anxiety among Iran's neighbours. Turkey too followed the developments with unease.

After the January 8 missile attacks on bases hosting United States troops in Iraq, there seems to be a temporary de-escalation and, at least for now, a wider conflict has been avoided. This has brought temporary relief to Iran's neighbours, including Turkey, as none wants to see another war in the region.

Although Ankara and Tehran have had a fraught relationship since the 2003 US invasion of Iraq marked by a geopolitical rivalry, they have also maintained strong diplomatic and economic relations.

The fallout of the Iran-US confrontation could both threaten Turkish interests in the region and offer an opportunity to strengthen some of its positions and potentially lead a regional dialogue.

Between cooperation and rivalry

To better understand where dangers and opportunities lie for Turkey within the continuing Iran-US confrontation, it is important to look back at the historical dynamics in Turkish-Iranian relations.

Although the political map of the Middle East has been reshaped repeatedly over the centuries, the border between these two countries has remained unchanged for almost 400 years. Indeed, despite historic Turkish-Iranian rivalry, both have stayed committed to the Treaty of Qasr-e Shirin of 1639 which brought an end to 150 years of intermittent confrontation between the Ottoman and Persian empires.

Many analysts, mostly in the West, do not understand this long history and seek to frame the dynamics between Turkey and Iran within the friend or foe binary. But the truth is, Iranian-Turkish ties have always been quite complex, multi-faceted and motivated by different considerations.

Over the past 20 years, Iranian-Turkish relations have been swayed by different regional events, but have continued to defy the friend-foe binary.

Throughout the 1990s, Turkish secular establishment treated the rise of political Islam as an Iranian regime export to Turkey. Tehran was suspected of being behind the assassinations of high-profile secular intellectuals and journalists and of supporting the Kurdistan Workers' Party (PKK), which led an armed uprising and committed terrorist attacks against the Turkish army.

The US invasion of Iraq in 2003, which Turkey vehemently opposed, refusing to grant access to its territory for US operations against Iraq and Iran wholeheartedly welcomed, created new points of tension between the two neighbours.

In the toppling of Saddam Hussein, Ankara lost a friendly regime which had allowed it to carry out cross border operations against the PKK, while Tehran won a victory against an old foe without firing a bullet.

In the aftermath of the invasion, Turkey became concerned about the possibility of an independent Kurdish state in northern Iraq and the fate of Iraq's Sunni community, while Tehran took to filling the power vacuum in Baghdad, creating a network of supporters among various Shia groups and laying the foundations for its entrenched political and security presence in the country.

This renewed rivalry, however, did not mean that diplomacy took a back seat. In fact, just seven years after the invasion of Iraq, Turkish-Iranian relations reached their zenith. In 2010, Ankara convinced Tehran to sign a nuclear swap deal which saw the Iranian authorities commit to sending low-enriched uranium to Turkey in exchange for getting nuclear fuel rods for a research reactor.

The agreement was supposed to make unnecessary the imposition of more UN sanctions on Iran, but the Obama administration sabotaged it and two months later slapped Tehran with more sanctions.

Apart from the nuclear deal setback, two other events in 2010 shook Turkish-Iranian relations: The nomination of Nouri al-Maliki for a second term as prime minister of Iraq with Tehran and Washington's blessings and the outbreak of the Arab Spring.

In his first term, al-Maliki, who enjoyed wide support from Iran, adopted increasingly sectarian policies, which created conflicts with the Sunni community and eventually led to an insurgency. He also came after Ankara's allies in Baghdad, particularly Iraqi Vice President Tariq al-Hashimi, who was sentenced to death in absentia in what was considered a political trial.

The Arab Spring also pushed to the fore the regional rivalry between Turkey and Iran; the former supported the pro-democracy protests in the region, while the latter saw them as a threat.

Ankara saw in the Arab revolutions an opportunity to expand its influence in the Middle East, which Tehran sought to curb, especially in Iraq, Syria and Palestine. The opposing stances they took on the Arab Spring fed into the tensions that had already appeared with their clashing interests in Iraq and transformed them into a proxy war in Syria. It was this proxy confrontation that the US chose to ignore and focus on the battle against ISIL (ISIS) - a mistake that only in recent years it has admitted to, as the Iranian presence in the region has started worrying its allies in the Gulf and Israel.

This head-on confrontation, however, was paradoxically accompanied by diplomatic rapprochement. While Turkish and Iranian-backed armed groups continued to fight each other on the ground over the past eight years, Turkish and Iranian leaders repeatedly met in Nursultan, Tehran and Istanbul to seek a settlement.

Opportunities for Turkey

In this context, the assassination of General Qassem Soleimani on January 3 could become yet another pivotal point in Turkish-Iranian relations.

Given Soleimani's key role in designing and maintaining Iran's network of proxies in the region, his death could change the fate of the prolonged proxy wars in Iraq, Syria and Yemen. It is likely to weaken Iranian proxies in the region and therefore, Iran's capacity to project power outside its borders.

From Ankara's perspective, stability in Syria and Iraq is key for its own security and geopolitical interests. In both countries it sees Iran as a destabilising factor. Therefore, a weakened Iranian proxy network is a welcome development for Turkey, which, recognising this weakness, is also likely to grow less tolerant of Iranian activity in Syria.

In Iraq, the potential for Turkish gains at the expense of Iran is less significant. While there are political parties and movements which seek to strengthen relations with Ankara, the Turkish government does not have the capacity to do so due to domestic considerations.

The current ultra-nationalist sentiments and discourse in Turkey are constraining the forward movement of Turkish foreign policy and engagement with certain foreign actors, including the Kurds in Iraq, which in the long term could cost the political capital in the region.

Apart from potential gains for Turkey, Soleimani's death could also lead to renewed momentum in Turkish-Iranian diplomatic engagement. Turkey is the only powerful and stable country bordering Iran; it is also a NATO member.

If the Iranian leadership decided to make a strategic move towards de-escalation and potentially normalisation of relations with the West, Ankara could play a vital role. In 2010, Turkey proved that it can be a reliable partner by seeking a solution on the Iranian nuclear issue and resisting US pressure.

For such engagement to take place, however, Iran will have to reconsider its strategy in Syria and Iraq and revise its counterproductive proxy tactics.

For now, unfortunately, it seems that more conservative elements are dominating decision-making in Tehran and pushing the country towards confrontation.

If there is more escalation between Iran and the US, Turkey's position will be clear. Just as it did not back the 2003 invasion of Iraq, it would not join a US military campaign against Iran.

If the US proceeds anyway, this would hurt Turkish interests. Turkey and the entire region have already suffered severely from the chaos that the toppling of Saddam Hussein created. If Iraq continues to be a battleground between Tehran and Washington, the region will continue to suffer economically and politically.

In the end, Turkey wants to see the US and Iran sit at the negotiating table and resolve not only the nuclear issue but also conflicts in Iraq and Syria. Whether cooler heads will prevail in Washington and Tehran remains to be seen. Meanwhile, Ankara should hope for the best and prepare for the worst.

The views expressed in this article are the author's own and do not necessarily reflect Al Jazeera's editorial stance.