Recep Tayyip Erdoğan turned Turkey’s local elections into a referendum on his personal leadership. The results, showing his Justice and Development party (AKP) in retreat nationally and losing control of seven of Turkey’s 12 main cities, not counting Istanbul, will thus be viewed as a stinging personal repudiation.

The question now is, how will Erdoğan react? The man who has dominated Turkish politics since 2003 is a bad loser, unaccustomed to defeat. He cannot abide criticism in any form – and despite his claims to the contrary, the big swing against the AKP, on a 84.5% countrywide turnout, amounts to an unprecedented rebuke.

Erdoğan certainly deserves blame. As is his wont, he alone ultimately directed the AKP’s campaign. Discarding any pretence of impartiality, he exploited his position as president and held almost daily rallies for two months preceding the vote. He said an AKP victory was a “matter of national survival” and claimed his opponents were in league with terrorists.

Even by his unscrupulous standards, he hit an all-time low by screening video footage of last month’s Christchurch mosque killings at rallies, saying the attacks were part of an organised western campaign against Muslims in general, and Turks in particular.

The poll outcome is Turkey’s biggest political shock since the failed 2016 coup attempt. Erdoğan’s immediate reaction then was to order mass arrests and blame foreign-based plotters, including the US military. He went on to use the coup as an excuse for launching indiscriminate nationwide purges to strengthen his grip on power.

Similar payback can now be anticipated, although probably not on the same scale. As in the past, the AKP will launch legal challenges against winning opponents with the help of a pliant judiciary, seeking to disqualify them before they can take office or contesting the propriety of local electoral procedures.

People walk past AKP billboards with pictures of Erdoĝan and mayoral candidate Binali Yıldırım in Istanbul. Photograph: Murad Sezer/Reuters

Erdoğan will be especially determined to quash celebrations in Kurdish areas of south-east Turkey, where the pro-Kurdish People’s Democratic party (HDP) made gains. He maintains the HDP is in cahoots with the outlawed Kurdistan Workers’ party. The HDP adamantly denies this. Several pre-poll arrests of HDP activists on terrorism charges were reported.

The fate of the HDP’s most charismatic leader, Selahattin Demirtaş, is instructive. He gave Erdoğan a run for his money in national elections in 2014 and was consequently marked down as a threat. Demirtaş has been held in jail since 2016, accused of disseminating “terrorist propaganda” among other things. The European court of human rights’ demand for his release has been ignored.

Having won sweeping executive powers in a stage-managed referendum last year, Erdoğan’s domination of Turkish institutions, including the government, military, and courts, is already formidable. The ability of independent journalists to scrutinise and investigate has been all but crushed – which many link to a perceived increase in corruption. If anything, Erdoğan will now further tighten his grip.

All of which makes the performance of the main opposition party, the Republican People’s party (CHP), in winning Ankara and other cities, all the more remarkable. The party’s veteran leader, Kemal Kılıçdaroğlu, has survived endless AKP ridicule and intimidation. He has now gained a measure of revenge. The CHP’s successes will boost all the opposition parties and renew hope that democracy in Turkey, almost given up for dead last year, may yet be disinterred.

Is this the beginning of the end for Erdoğan? It is too soon to say. He does not face a national election until 2023. He has proved his staying power again and again, and his base support, mainly rural, remains strong at about 44%. But there is one wild card beyond his control: the state of the economy – a determining factor in the weekend polls.

Erdoğan’s untutored efforts to block interest rate rises, boost prestige infrastructure spending and ignore rising debt levels have helped propel Turkey into recession, exacerbating currency, inflation and unemployment problems. Rocketing food prices, and shortages of household staples, spell trouble for any politician, anywhere – even a grand Turkish sultan.

The price of onions may yet be Erdoğan’s undoing.