The Iranian turn has come at an opportune time for Turkish foreign policy in other ways, too. Iran has influence with Iraq’s Shiite-led government and Syria’s Alawite elite. In Iraq, where a crucial oil deal hangs in the balance, Turkey needs Iranian cooperation. It also needs Iran’s help on Syria.

Turkey initially tied its policy to America’s demand that President Bashar al-Assad quit. It was disappointed when the Obama administration signed on to a Russian-brokered deal with Mr. Assad on chemical weapons. With violence menacing across the border, Turkey wants to see an end to Syria’s civil war. The new moderate government in Tehran is Turkey’s best hope for leveraging a settlement.

Economic ties between Turkey and Iran have been strengthening, with trade now estimated to be worth $20 billion. The real number may be still higher, since the recent corruption charges allege that Turkish officials and the state-owned Halkbank have been helping Iranian businesses dodge international sanctions. In any case, Iranian exports still reach Turkey, and the proceeds fund the purchase of gold and silver that flow back to Iran. In turn, Turkey’s economy depends on Iran’s oil and gas, its investments dollars and large export market.

If Iran does conclude a long-term nuclear deal with the West, it still cannot expect a warm welcome from the Sunni Arab world. With the region divided by a widening sectarian rift, the Persian Gulf monarchies will become only more fretful about Iran’s regional ambitions. That makes Turkey potentially a key strategic partner for Iran, especially if its economy starts to grow as sanctions are relaxed.

With American influence in the region in decline, and with Israel and the Persian Gulf monarchies finding themselves united in their opposition to Iran, Turkey could find itself playing a central role thanks to its links with Iran. A new Turkish-Iranian partnership could be a welcome development for the West: Turkey’s economic ties could boost Iran’s commercial development, which would help consolidate the political position of the moderates in Tehran. The real gains would come if a closer relationship with Turkey began to erode the alliance of militias and radical religious forces on which Iran has relied to project its influence.

To play this enlarged regional role, though, Turkey must first reassure the West that it will remain a trusted NATO ally and not demonize Western allies as a way of managing political dissent at home. However Mr. Erdogan’s domestic difficulties fall out, Turkey has an opportunity to restore its international standing. It will have to show that it is not simply an advocate for Iran, but has used its influence to shift Iran’s foreign policy and facilitate a permanent nuclear deal.

Vali R. Nasr is dean of the Johns Hopkins School of Advanced International Studies.