Updated

After an ugly presidential election campaign, Indonesia is set to declare the winner on Tuesday — but that may not settle a simmering dispute between the two candidates, both of whom claim victory.

Unofficial counts by eight polling agencies of the July 9 vote have given Joko Widodo, the popular and sneaker-wearing former governor of Jakarta known as "Jokowi," a slim lead.

But Prabowo Subianto, a former general with a checkered human rights record who has drawn voters with his thundering nationalistic rhetoric, insists he has polling data showing he has won, raising speculation that he might may not accept the results if he loses.

The tension could threaten Indonesia's fragile transition to democracy 16 years after it emerged from the long and brutal Suharto dictatorship. The country of 240 million is experiencing a slowing economy — the largest in in Southeast Asia — and needs leadership to tackle a rapidly crumbling infrastructure.

Once the Election Commission announces the winner, it is highly likely the losing candidate will appeal to the Constitutional Court, the country's highest. Judges there will have two weeks to rule on any complaints before deciding who won.

However, some experts worry that Indonesia's endemic corruption could affect that decision. Last month, the court's chief justice was sentenced to life imprisonment for taking a bribe to adjudicate in favor of a plaintiff in a case of disputed provincial election.

"That will be a challenge for the Constitutional Court, whose image has already been ruined," said Mohammad Qodari, a political analyst.

Subianto, who has declared assets of $140 million and is on his third bid for the presidency, denies any intention to attempt to buy the vote.

The results could trigger social unrest such as clashes between supporters of the two men. Indonesia has experienced frequent outbreaks of political, ethnic and separatist violence during its transition to democracy. The security situation across the country's 18,000 islands has improved markedly in recent years, but the unprecedented rancor of the campaign, the first between just two candidates in the country's history, means that tensions are running high.

There were significant smear campaigns in the run-up to the election, and supporters of both men used social media for personal attacks.

"On the Jokowi side there are too many parasites, they are a danger to the country," Subianto supporter and lawmaker Fahri Hamzah tweeted recently. "After the 22nd we will 'deal' with them. Just be patient," he said, using language that could easily be interpreted as menacing.

Widodo, a former furniture maker, is widely seen as untainted by the often-corrupt military and business elite that has run Indonesia for decades. He likes to wear casual plaid shirts, listen to heavy metal music and make impromptu visits to the slums.

Subianto, late dictator's former son-in-law, is seen by some as providing stronger leadership and was endorsed by Islamic-based parties, hard-line Islamic groups and outgoing President Susilo Bambang Yudhoyono's political party. He was trailing behind Widodo for months before the polls but caught up on the back of a well-organized, well-funded campaign that was supported by most of the country's establishment political parties. His supporters also spearheaded a smear campaign against Widodo, spreading unfounded rumors he is not a Muslim — which could undermine his standing in this Muslim majority nation.

Voters for Subianto chose to ignore his links to past human rights abuses. He admits taking part in the abduction of pro-democracy activists during the dying days of the Suharto dictatorship when he was head of the army's strategic command, saying he was following orders. He was fired from the army as a result, and spent several years in self-imposed exile in Jordan.

Unofficial "quick counts," which tally a sampling of votes around the country, carried out by eight agencies have given Widodo a lead of around 4 percent, or roughly 6 million votes. The counts have been highly accurate in forecasting the results of previous elections. Independent analysts say there is no reason to think otherwise around this time around, and that anything but a Widodo victory of around 4 percent would be highly suspicious.

Four polling firms with links to the Subianto campaign produced tallies that showed him in the lead by differing margins. Earlier this week, the Indonesian Association of Public Opinion Surveyors dismissed two of them from the grouping after they refused to reveal their research methods. The two other pollsters were not members of the association.

The Electoral Commission needs about two weeks to collect and tabulate votes from nearly a half-million polling stations across the country's 33 provinces. The commission's national leadership is mostly seen as professional and impartial, but the same can't be said for its official and local or provincial levels.

Once the formal results are announced, it might be difficult for Subianto to keep up his campaign.

The political coalition that supported his bid might abandon him, preferring to try and get positions in a Widodo administration. Public and media support might well wane.

"My suggestion is that the two (Subianto and his running mate, Hatta Rajasa) should have a reality check. I know that deep in their hearts, they know they have lost. They should be just resigned to the results, as gentlemen and statesmen," Abdillah Toha, a leading member of a political party that supported the pair, wrote in an open letter this week.