Scores of civilians in Turkey have been killed in attacks attributed to Islamic State in the past year. The worst of them were perpetrated by suicide bombers in the city of Suruc in July 2015, in Ankara last October, and in Istanbul airport almost two months ago.

Due in large part to these and other terrorist incidents, Turkey has been sucked into an undeclared war with Islamic State.

This is a supremely ironic development, given the fact that Turkey allowed foreign jihadists to enter the ranks of Islamic State in Syria, which has been mired in a civil war for five years now.

When the uprising in Syria erupted, Turkey’s prime minister, Recep Tayyip Erdogan, who has since become president, was a strong ally of Syrian President Bashar al-Assad. Eager to maintain Turkey’s relationship with Syria, he urged Assad to defuse the strife by means of a meaningful program of reforms. Erdogan assumed that Assad would pay heed to his recommendation, since Turkey had close political and mutually profitable commercial ties with Syria.

As a sop to his Turkish ally and his rebellious population, Assad introduced cosmetic reforms, but they satisfied neither Erdogan nor rebellious Syrians.

Frustrated with Assad’s half-measures, Erdogan called on him step down, thereby becoming one of the first Middle Eastern leaders to demand his resignation. Assad paid no attention to Erdogan’s call, prompting Erdogan to turn against the Syrian leader and devise a new policy toward his Baathist regime.

In quick succession, the Turkish government invited rebels opposed to Assad’s rule to establish themselves in Turkey — the only Muslim member state of the NATO alliance — and permitted large numbers of Syrian refugees to settle in Turkey. Today, about two million of these refugees live in Turkey, mostly in makeshift camps.

In addition, Erdogan let tens of thousands of Islamic State volunteers, who had arrived in Turkey from a myriad of destinations, to cross the border into Syria. It was his hope that they would strengthen rebel groups, such as Islamic State, intent on toppling Assad.

As Islamic State captured more Syrian territory, Western governments, notably the United States, urged Turkey to close its border to the torrent of volunteers flowing into Syria. Turkey, determined to oust Assad, was slow to comply with this request.

“I think Turkey has other priorities and other interests,” said James Clapper, the director of U.S. national intelligence, last year in a reference to Erdogan’s objective of removing Assad from office.

Today, it’s clear that Turkey overplayed its hand by letting foreign jihadists enter Syria from its territory. Erdogan’s obsession with undermining Assad had the effect of strengthening Islamic State, much to Turkey’s detriment.

Bowing to further international pressure, the Turkish government in 2015 cracked down on the transit of foreigners from Turkey to Syria and arrested and deported suspected Islamic State volunteers.

And in a momentous decision taken in the same year, Turkey gave the United States permission to use the Incirlik air base to fly bombing missions aimed at Islamic State in Syria and Iraq. After last month’s failed coup in Turkey, the Turkish government temporarily suspended these flights.

Turkey, too, bombed Islamic State bases for the first time and shut down smuggling routes into and out of Syria.

As a result of these decisions, Islamic State designated Turkey as one of its enemies, said John Brennan, the director of the U.S. Central Intelligence Agency.

Critics contend that the Turkish Air Force has carried out more air strikes against the Kurdistan Workers’ Party (PKK), which demands autonomy for Kurds in Turkey, than against Islamic State.

Turkey is also at odds with a Syrian Kurdish militia known as the YPG, which, Turkish spokesman claim, is an arm of the PKK. Much to Turkey’s chagrin, the United States backs the YPG, which has battled the Islamic State in northern Syria. YPG fighters participated in the recapture of the town of Kobani, which had been held by Islamic State. And the YPG played a vital role in the formation of a security zone along sections of Turkey’s border with Syria.

Judging by recent events, Turkey will be locked in combat with Islamic State for the foreseeable future. It’s a commitment Turkey can no longer avoid.