Before the war in Syria broke out in 2011, a budding personal friendship between Bashar al-Assad, the leader of Syria, and Recep Tayyip Erdoğan, the president of Turkey, augured close economic ties and an open border. That would be only the beginning: Turkey saw Syria as the launching pad for its plans to become the dominant economic force in the Arab world, a region it had largely retreated from after losing its vast Arab provinces with the collapse of the Ottoman Empire.

But the Syrian uprising forced Turkey to reconsider. Erdoğan severed his ties with Assad and cast his lot with the popular, predominantly Sunni opposition, making the bet that they would overthrow the minority Alawite-based regime. Meanwhile, Turkey gradually found itself on the outs with Washington, a close strategic partner for decades. Following the recent hostile rhetoric from the Trump administration and the imposition of sanctions on Turkey for detaining an American pastor, tensions have grown.

Seven years into the war in Syria, as Turkey struggles to shield itself from the destabilizing spillover of regional turmoil, Erdoğan’s bet on the Arab world looks increasingly like a losing one. A Syria at war has become the graveyard for any dreams of the neo-Ottoman grandeur he may have nurtured.

The rump state of the once mighty Ottoman Empire, Turkey has experienced numerous traumas over the past century, including repeated coups to suppress internal challengers. It has also fought a tenacious armed insurgency by the Kurdistan Workers’ Party (PKK), which claims to represent the region’s Kurds, many of whom ended up in Turkey when Britain and France divided up the empire’s remains at the end of World War I. (Turkey deems the PKK a terrorist organization, as do the United States and the European Union.) The possibility that Kurdish secession could further erode the country has been intolerable for successive Turkish leaders. They have used methods ranging from harsh repression to bombardments to peace negotiations to curb the PKK’s ambitions, defeat it on the battlefield, or render it politically irrelevant.

The ascendancy of Erdoğan’s Justice and Development Party (AKP), with its conservative base of small-business owners and Muslim Brotherhood–inspired ideology, heralded a decade of rapid economic growth starting in 2002. A confident Turkey embarked on a policy of “zero problems” in its neighborhood, presenting itself as the region’s düzen kurucu, or “order setter.” The government expected significant economic payoffs, as Turkish companies fanned out across the region and made their mark, especially in the construction sector. Turkish television channels entertained Arab audiences with Turkish soaps, warming them up for visa-free visits to Istanbul and the Mediterranean coast.

But the Arab Spring uprisings put an end to all that. Along with the war in Syria, the turning point was the July 2013 coup in Egypt, which ousted the Brotherhood-led government, Ankara’s ally. With the Brotherhood on the retreat throughout the region, Turkey saw its reputation and investments go up in smoke.

In response, Turkey began to focus on its most immediate concern: Syria’s civil war, which the Erdoğan government, along with its Western and Gulf allies, helped kindle in a failing effort to defeat the Assad regime. Instead, the war not only undermined Turkey’s interests as its rebel allies lost their footing—it opened a vacuum that jihadists and the PKK, Turkey’s two most formidable enemies, were keen to exploit. Indeed, for Turkey, the Syrian war has become less about overthrowing Assad—a task that became seemingly impossible once Russia entered the war in 2015—and much more about keeping these two groups at bay. Each threatened Turkey: The Islamic State sought to reestablish the caliphate, while the PKK sought to safeguard Kurdish rights. The latter aim, Turkish leaders feared, could eventually encourage the group to press for statehood, and therefore Turkey’s breakup.

While jihadist attacks generated terror, the war against the PKK in the southeast most worried Ankara. After talks with the group broke down three years ago, the costly and corrosive conflict seemed destined to go on forever. The PKK reinvigorated itself through its Syrian affiliate, the People’s Protection Units (YPG), which had started recruiting and training new fighters to take on ISIS. Even more worrisome for Turkey: The YPG received military assistance from the United States, which saw it as an effective fighting force against the jihadists. The result was a YPG-protected buffer zone inside Syria along the Turkish border—a situation Erdoğan has been keen to reverse.

Today, Turkey finds itself tangled up in a major contradiction. It remains a key part of the Western alliance, yet feels aggrieved by Washington’s support for the Kurds, its imposition of sanctions over Turkey’s detention of an American pastor, and its refusal to extradite Fethullah Gülen, the head of a religiously based political movement whom Ankara accuses of masterminding the attempted coup in July 2016. Angry as it may be, Turkey will not readily forgo its strategic alliance with the West. This assessment emerged from discussions at a forum organized by the Körber Foundation in Berlin in July, in which I participated: Strains in the relationship will doubtless remain, but Washington needs Ankara as a bulwark against Iran, Russia, and jihadists, while Ankara needs Washington as a protector of last resort against Russian and Iranian maneuvering in its neighborhood.

All the same, Turkey is reaching out to Russia to help produce an outcome to the Syrian war that it can live with. This could take the form of a deal between the YPG and Damascus brokered and guaranteed by Moscow and Washington, one that would see the Assad regime reasserting its control over at least part of the border. This arrangement wouldn’t be ideal from Erdoğan’s perspective, but could be the least bad one—if it can be achieved. Turkey will also need Russia’s help in Idlib, the last rebel stronghold in Syria. This is a province whose population of about 1 million has more than doubled thanks to the influx of Syrians displaced from other parts of the country. With Russia’s blessing, Turkey has managed to shield Idlib from a regime offensive, which would likely foment a new refugee crisis. Turkey can barely cope with the more than 3.5 million Syrians it has already taken in.

But Russia’s objectives in Syria are not Turkey’s. Moscow wants Ankara to reconcile with the Assad regime. Turkey’s reliance on Russia to protect itself from the PKK and prevent a new surge of refugees, this time from Idlib, may therefore force it into an accommodation with Damascus that it has successfully resisted until now.

With impulsive and mercurial leaders in both Washington and Ankara, no one can say how Turkey will navigate the gathering storm. It may count itself lucky if it emerges with mere scrapes from its lost gambles in Syria and the wider region, and does not find itself shipwrecked, with enemies surrounding it.

We want to hear what you think about this article. Submit a letter to the editor or write to letters@theatlantic.com.

Joost Hiltermann is the program director for North Africa and the Middle East at the International Crisis Group.