On the surface, Turkish intervention in the Libyan civil war appears to be a savvy move on the Eastern Mediterranean energy chessboard, a check on plans by a consortium of the European Union (EU), Greece, Egypt, Israel, and Cyprus to exploit offshore gas and oil deposits there.

In exchange for military support, Libya’s beleaguered, UN-supported Government of National Accord (GNA) in Tripoli signed an agreement last November that re-draws maritime boundaries in the region, giving Ankara a seat at the table.

Or at least that is what Turkish President Recep Tayyir Erdogan hopes.

But “hope” and “Libya” are not two words that easily mesh, and Ankara is finding that the Turkish intervention is less like a move in a game of skillful maneuver than an old fashioned quagmire. Why the Turkish autocrat thought choosing sides in a civil war was a good idea is hard to fathom, especially after his debacle in Syria.

When the Syrian civil war broke out in 2011, Erdogan jumped in with both feet, arming and feeding the opposition to the Bashar al Assad government, aiding Al Qaeda extremists crossing the Turkish border and predicting that the Damascus regime’s days were numbered. Nine years later, Turkey is swamped with 3.8 million refugees, and Ankara’s allies are barely clinging to Syria’s Idlib Province in the northwest.

While Turkey’s invasion of Syria last year did drive most of the Kurds from Syria’s eastern border with the Turks, Syrian and Russian troops blocked Ankara’s plans for a 20-mile deep cordon sanitaire to which it could relocate millions of refugees.

After almost a decade of intervention, Erdogan finds his army bogged down on the losing side of a civil war, growing discontent at home over the refugees and the economy, and looking outmaneuvered by Moscow and Damascus.

And yet once again Turkey is picking sides in a civil war, and this one more than 1,000 kilometers from the Turkish border.

Turkey’s Newest Quagmire

There is a certain logic to Ankara’s move in Libya.

Turkey’s claim to energy resources is based on its occupation of northern Cyprus, and Turkey objects to being left out of the regional energy agreement drawn up by the consortium. But since no country in the world recognizes the Turkish Republic of Northern Cyprus, Ankara’s claims for a slice of the energy pie have been ignored.

When Egypt, Israel, Cyprus, Italy, Jordan and Palestine formed the Eastern Mediterranean Forum last year, Ankara was left out. Some Forum members want to built a pipeline to ship natural gas through Crete to Italy and mainland Greece.

The confrontation over energy has, at times, gotten ugly. Turkish warships drove off Italian drillers last year, but backed down from an American energy company accompanied by a U.S. destroyer. Tensions are high between Athens and Ankara, and some sort of military clash is not out of the question, in spite of the fact that Turkey and Greece are both members of NATO.

The Turkish president’s usual sure footedness seems to have deserted him. By openly declaring for one side in Libya, Turkey has damaged its ability to influence events. The Russians and French are also deeply involved in Libya, backing the eastern-based Libyan National Army (LNA) in Tobruk. Italy backs the UN-recognized Government of National Accord (GNA) based in Tripoli.

The French have been sneaking weapons to the LNA, and a Russian private company, the Wagner Group, is supplying mercenaries and trainers. But the European involvement is undeclared and unofficial, allowing those countries to play a mediating role in the future.

However, by guaranteeing it would protect the Tripoli-based GNA government, Turkey has painted itself into a corner. Its only real ally is Qatar and (clandestinely) Italy.

Openly arrayed against the GNA are the United Arab Emirates and Egypt, which along with French supplied rockets and Russian mercenaries and drones, have driven the Tripoli government out of Sirte and are knocking on the door of the capital. Erdogan’s plan to use Turkish soldiers was scotched by the unanimous opposition of the 22-member Arab League and the Jan. 20 Berlin Conference on the war. And Turkey’s plan to use Syrian mercenaries seems to have died aborning.

That Erdogan really thought Syrians would want to fight in Libya suggests a certain disengagement from reality.

Erdogan initially assumed that his intervention would be supported by Morocco, where Erdogan’s Justice and Development Party (AKP) is closely aligned with Rabat’s ruling Party. But instead of opening its airfields to Turkish warplanes, Morocco is remaining adamantly neutral, as is Algeria.

At home, Erdogan’s intervention has been popular. Many Turks are nostalgic for the old days when the Ottoman Empire ruled the Middle East and North Africa, and the GNA is allied with the ethnically Turkish militias in Misrata. Libya was the last Ottoman holding to break free from Istanbul’s rule.

Division in the Ranks

But how long that popularity holds is an open question. The Turkish economy is in recession and unemployment is at 14 percent. Turkey will soon have to cope with hundreds of thousands more Syrian refugees fleeing from the Syrian Army and Russian air power in the northwest.

A number of other foreign adventures have gone south as well. Last month, several Turkish contractors and policemen were targeted by a roadside bomb in Somalia. Turkey has poured more than $1 billion into that war-torn country, taking over its major airport and sea port. But if you want the definition of “quagmire,” you does not have look much further than Somalia.

In the last round of local Turkish elections, Erdogan’s AKP took a thrashing, losing the mayoralty races in Turkey’s six largest cities. His hugely expensive scheme to dig a massive canal to link the Black Sea to the Sea of Marmara has run into a buzz saw of opposition in Istanbul, and was one of the reasons the AKP lost the election.

The loss was a double blow because Istanbul was where Erdogan got his start in politics. It was also a piggy bank for the AKP, which cashed in on kickbacks by construction firms. The city represents more than 30 percent of Turkey’s GDP.

Has the most powerful and successful politician since Mustafa Kemal Ataturk, the founder of modern Turkey, started to stumble?

For almost 20 years, Erdogan has dominated the country through a combination of clever politics and an iron fist. He has built a formidable election machine through his construction schemes — the canal is the latest — stuffed ballot boxes, virtually eliminated any opposition media, and tossed thousands of his opponents into prison.

But Syria is a disaster, Libya looks like a bridge too far, and the African Union is considering withdrawing troops from Somalia, leaving Turkey to inherit the two-decade old war. Erdogan is at odds with the EU and every country in the Middle East save Qatar. And even Qatar seems to be positioning itself to settle its differences with two of Turkey’s regional foes, Saudi Arabia and the United Arab Emirates.

At home, the Turkish lira is plummeting, unemployment remains stubbornly high, and massive construction projects no longer keep the economy humming. In the past Erdogan could rely on religiously conservative Kurds to back the AKP, but his repressive policies toward the Kurdish community has alienated that minority.

Lastly, the AKP has splintered, spinning off a center-right party attracting those who are weary of Erdogan’s one-man rule.

Never Count Erdogan Out

Counting Erdogan out, however, would be premature.

He can keep the EU at bay by threatening to unleash millions of refugees now residing in Turkey. He can count on the loyalty of the military and the police to keep much of the opposition cowed, and he can still rely on most religious Turks.

While there are no national elections scheduled until 2023, Erdogan is likely to push that up to 2021, if not before, figuring he can pull out another victory. But the AKP has never gone into an election with the opposition controlling the major cities and divisions within its own ranks.

Erdogan may get his early election. It may not turn out the way he wants.