By James Mackenzie

KABUL (Reuters) - With so much riding on American support, Afghanistan is waiting anxiously to see if President-elect Donald Trump matches his maverick image and reverses policy or keeps to a path that has cost billions and committed thousands of troops to propping up a fragile ally.

Much to the private annoyance of officials in Kabul, America's longest war barely featured in the election campaign, but few were expecting the billionaire property developer to beat former Secretary of State Hillary Clinton.

"It's been a complete surprise," said one senior Afghan official closely involved in national security issues. "I think everyone was expecting the opposite result.

"It (Afghanistan) wasn't a priority in the campaign, that's obvious. No one talked about Afghanistan at all. At least with Mrs. Clinton, though, you knew more or less what you were getting."

Afghanistan will nonetheless present the incoming Trump administration with one of its most intractable foreign policy challenges.

The United States has spent some $115 billion in aid for Afghanistan, but 15 years after the hardline Islamist Taliban were toppled after the Sept. 11 attacks, a third of the country is out of government control and security forces are struggling.

As recently as last week two U.S. service members were killed fighting the Taliban near the northern city of Kunduz, and expectations that Afghan security forces could survive without extensive foreign assistance have proved illusory.

Faced with a mounting Taliban insurgency, Obama dropped his original aim of pulling out of Afghanistan entirely. In July, he decided to keep 8,400 U.S. troops in Afghanistan, shelving earlier plans to bring the numbers down to 5,500 and leaving it to his successor to decide how to move forward.

Before the election, U.S. officials told partners that Afghanistan, whose war-ruined economy depends heavily on billions of dollars in foreign aid, will not be neglected by the new administration and that ties will remain strong and close.

"You can be assured that Afghanistan will remain at the highest levels of our foreign policy agenda," Ambassador Michael McKinley told an election day party at the U.S. embassy in Kabul, before the final results of the ballot were in.

LITTLE INTEREST

However, Trump's "America First" doctrine has left many wondering if he will be willing to continue spending billions of dollars funding Afghanistan, particularly given his declaration that: "We're getting out of the nation-building business".

Among Afghans who use social media, a sense that the incoming president is opposed to their old enemy Pakistan made some hope his election would be good for Afghanistan, but there is little to go on for those trying to parse his public remarks.

So far, he has shown little interest in Afghanistan, although his most recent comments suggested he favored keeping troop numbers at around 5,500, the level they were intended to reach by the end of the year before Obama shelved the plan and set the number at 8,400.

But in other comments, he has described America's involvement in Afghanistan as "a terrible mistake" and appeared to set conditions on the U.S. commitment to NATO, which leads the Resolute Support advise-and-assist mission in Afghanistan.

Security officials in Kabul say the threat that Islamic State militants could build their presence in Afghanistan should act as an incentive to keep a U.S. force in place, although they admit they remain in the dark about Trump's intentions.

"As Trump promised during his campaign that he would eliminate Daesh (Islamic State), we don't think he would withdraw American forces from Afghanistan as Daesh is a new threat for Afghanistan and the region," said one official, who spoke on condition of anonymity because of the sensitivity of the issue.

"But we are still in a state of uncertainty and we will wait and see what Trump has for Afghanistan," he added.

The decision will be vital for a country that is likely to depend on American support for years to come.

"The United States will almost certainly continue to be the leading source of both military and civilian reconstruction aid to Afghanistan for years to come," the latest report from the Special Inspector General for Afghanistan Reconstruction, a Congressional oversight body, said only last month.

In an interview with CNN television last year, Trump himself appeared to accept that it was too late to pull U.S. forces out.

"And at this point, you probably have to stay because that thing will collapse about two seconds after they leave," he said, in the interview in October, 2015.

(Additional reporting by Hamid Shalizi; Editing by Mike Collett-White)