ISTANBUL — Turkey’s move to abandon parliamentary democracy and adopt one-man rule fulfills a long-held dream of President Recep Tayyip Erdogan, who not only expands his powers but also gets a chance to stay in office for another 15 years if, as expected, the current referendum ballot count holds up.

The official Anadolu news agency said the country voted on Sunday 24.3 million to 23.2 million, a margin of 51.1 to 48.8 percent, in favor of a package of constitutional reforms. But the opposition Republican People’s Party (CHP) said it will challenge well over 1 million ballots which lacked the seal of the election oversight board.

If Erdogan prevails in the end, as many here expect, the result will be a system under which there’s no prime minister, where the parliament will be weakened to the point of being a rubber stamp, and the judiciary will become still more subservient than it is already.

The path to one-man rule—opponents talk of a “dictatorship”—is the story of a politician with a gut instinct for gaining power who’s seized on every political setback that’s come his way in the past two years and turned it into an opportunity to advance his ambitions.

Using adversity as a stepping stone, he’s accreted so much power—far more than is constitutionally accorded to his ceremonial post as president—that the referendum in a sense only formalizes what he’s already accomplished.

Dealing with Turkey after the referendum will present big challenges to the U.S. and Turkey’s other NATO allies. Ankara and Washington are already in a major dispute over how to defeat the Islamic State terror group in Syria, which President Donald Trump has set as a top priority. Soon the two countries are likely to be bickering over how to retain the semblance of democracy in Turkey and thus prevent the alliance from splitting up.

In some respects, the U.S. has played an unwitting role in Turkey’s move toward authoritarian rule. Like Brexit in Britain and the rise of rightwing populism in Europe, which are partly reactions to the flood of refugee from Syria, the internal political shift here is also a byproduct of that war.

Former President Barack Obama decided largely to ignore the Assad regime’s war against its own people as well as the consequences—more than half the population displaced internally or abroad, of them more than 3 million Syrians in Turkey alone.

It wasn’t until ISIS extremists seized Mosul, Iraq, and named Raqqa, Syria, as their capital that he decided to intervene militarily. That led to a tactical U.S. alliance with People’s Protection Units (YPG), the Syrian affiliate of the Kurdistan Workers’ Party (PKK), which the U.S., the EU, and Turkey all have designated a terrorist organization. Turkey has been at war with the PKK for some 40 years, with a brief break from 2013-2015.

Less than a year after Obama began providing military support to the YPG in the fight against ISIS, the PKK, no doubt buoyed by its burgeoning relationship with Washington, announced an end to the cease-fire with Turkey and began attacking Turkish security forces in the southeast.

Erdogan, citing the revived war with the PKK, pleaded with the U.S. repeatedly to break off the alliance with the YPG, but to no avail.

Today the U.S. appears as determined to use the PKK’s affiliate in Syria to help capture Raqqa as Erdogan is to block it.

A security challenge in its own right, the PKK’s return to war with Turkey in July 2015 provided an occasion for Erdogan to rally political support at home.

He was in need of a new political strategy.

Just a month before the PKK had announced the return to violence, Erdogan had endured a major electoral setback, a loss of his majority in national parliamentary elections. The big winner in those elections was Selahattin Demirtas, the charismatic leader of the pro-Kurdish HDP, whose election campaign had gained support from non-Kurds partly because he was campaigning to block Erdogan from gaining additional powers.

The HDP won 80 seats in the 550-seat parliament, a gain of 51, while Erdogan’s party fell to 258, down 69, and lost its parliamentary majority.

Other politicians might have been discouraged by such setbacks, but Erdogan saw opportunity. Rather than form a unity government, he delayed and delayed until he could call new elections. To weaken the HDP, and destroy the chances of its giant-killer Demirtas, he labeled it the political arm of the PKK.

Calling a second round of elections in November, the AKP regained its majority with 317 seats. With that, Erdogan formed a one-party government and began efforts to sideline the HDP, which still had 59 seats in parliament.

In March 2016, parliament voted to lift the immunity of 115 members, including nearly all of the HDP parliamentarians. Demirtas and his co-leader Figen Yuksekdag were arrested last November, and both are now in jail. Yuksekdag was found guilty of supporting terrorism in February and stripped of her parliamentary seat and Demirtas was found guilty of “insulting the Turkish nation and state institutions.” The party of the “giant-killer” was crippled.

Erdogan’s third opportunity to turn adversity into political capital occurred July 15 last year, when a group of senior military officers staged an abortive coup. Erdogan, who was vacationing on the Aegean coast, was cut off from his own government, but took to the airwaves over a FaceTime link with CNN Turk, an independent television channel, in which he appealed to the Turkish public to take to the streets in opposition to the coup. His followers confronted tanks in Ankara, Istanbul, and other cities, and 248 lost their lives.

In a daring return to center stage, Erdogan flew in a small plane through airspace controlled by the coup plotters to Istanbul’s Ataturk airport, where loyalists had cleared the runway and facilitated a safe landing.

At a brief appearance the next day, he declared: “This uprising is a gift from God to us, because this will be a reason to cleanse our army.” Five days after the failed coup, he ordered a state of emergency and began ruling by decree.

With his expanded powers, he purged political opponents associated with one-time ally, Fethullah Gülen, an Islamic preacher who lives in U.S. self-exile, who Erdogan charged was behind the coup. But he also purged, fired or arrested Kurds suspected of sympathies with the PKK and many others. Some 150,000 people, many of them public employees, were dismissed from their jobs, 100,000 were put under investigation, and 44,000 were imprisoned pending trial, according to a recent report by the parliamentary committee of the Council of Europe, which sets human rights norms for Europe. Some 177 media outlets were closed, many them Gülenist, but a great many Kurdish, and 2,500 journalists lost their jobs, among them many Kurds but also many affiliated with the opposition CHP.

This set the stage for Erdogan’s drive to change the Turkish constitution and give him the powers he had sought for years.

First, he obtained the backing of Devlet Bahceli, the leader of the nationalist MHP, although what the quid pro quo might have been remains unclear. That gave Erdogan the votes in parliament needed to call a referendum. And then, without any public discussion, he submitted the amendments to the parliament itself.

Such debate as there was had to take place under the state of emergency. When ISIS organized a terror attack on an Istanbul nightclub in the dark early hours of New Year’s Day, Erdogan launched a major crackdown by the security forces—and extended the state of emergency.

A good part of the debate was not even televised, and individual CHP members posted it on the Internet, using their smartphones. But that was just a preview of the almost surreal campaign for the amendments.

Ever since they won parliamentary approval in mid January and the date for the referendum was set, what’s been missing is vigorous public discussion.

The Turkish news media, with some notable but rare exceptions, toe the government line, and those that don’t support Erdogan self-censor, particularly at a time some 150 journalists are now reported in jail.

So, there was little debate in the news media. Also missing from the national debate was an actual debate, according to Utku Çakırözer, a former editor at the opposition daily Cumhuriyet, who’s now a CHP deputy from Eskisehir, an industrial city in central Anatolia. “I don’t think there’s been a debate between yea-sayers and nay-sayers at the expert level,” he said.

Absent such input, the government was able to make claims for the amendments that were completely spurious, such as that the changes would strengthen the separation of powers and that the boost in deputies to 600 from the current 550 would strengthen the parliament.

Erdogan’s decision to run an all-out campaign when his current post is supposed to be apolitical and ceremonial rankled his opponents, as did the decision to deploy the ministers in the Turkish government and the aircraft, vehicles, state buildings and other facilities in the campaign.

Erdogan also tapped into an ingrained attitude of defiance to foreign powers last month when he sent government ministers to western Europe to try to win votes from Turks living abroad.

When the German and Dutch governments made it clear the ministers were not welcome, Erdogan seized the opportunity to accuse both governments of Nazi-style policies. The gambit, which shocked public opinion in west Europe, appears to have won him support among Turks.

But the most critical factor in putting his message across was the way in which Erdogan, who according to the constitution is supposed to be neutral, and his Prime Minister, Binali Yildirim, commandeered the airwaves. What made this possible was a decree issued on the eve of the referendum campaign that lifted the requirement that television channels to give equal time to each side in the debate.

Erdogan made two or three speeches a day, as did Prime Minister Binali Yildirim, and they invariably got full coverage on national television. A study just cited by the CHP said Erdogan, in his capacity as president, and the AKP as the party advocating the “yes” vote received 10 times as much air time as Kemal Kilicdaroglu, the head of the CHP. The HDP was not even a minor player.

The fact the deck was thus stacked in favor of a “yes” vote may win the losing “no” advocates a vote of sympathy, and that seems to be what Kilicdaroglu now hopes for. “We held a referendum on unequal conditions,” he said Sunday night. “We did our best to obey the rules under these conditions.”

He promised a fight to the end. But there’s almost no place in the Turkish government to appeal to, and international opinion appears to carry less weight with Erdogan with every passing day.

—Duygu Guvenc in Ankara contributed to this story