Pfc. William Berczik of Houston, with the 4th Squadron, 2nd Cavalry Regiment, stands on top of a Stryker vehicle after a live-fire exercise with 120mm mortars in the desert on March 6, 2014, near Kandahar, Afghanistan. (Scott Olson/Getty Images)

For President Obama, the war in Afghanistan has been a matter of profound ambivalence — a strategic necessity and an unmistakable burden.

He has talked about the United States’ interest in preventing the country from ever becoming a sanctuary for global terrorists. Just as often, he has spoken of ending the war and about the limits of American military power, money, patience and time.

Nearly eight years after Obama began his presidency with a long and contentious Afghanistan strategy review, he is leaving behind for President-elect Donald Trump a war that reflects his divided outlook.

By most measures, the conflict is a stalemate. Afghan troops are fighting hard, but “the casualty rate is much higher than we would have hoped,” a senior defense official recently told reporters. Taliban forces are taking territory from the U.S.-backed government, but they have not been able to seize and hold any major cities or towns.

“Clearly the situation is getting worse, but not to the extent that you see the Taliban winning or the Afghan government is clearly failing,” said Andrew Wilder, a vice president and Afghanistan expert at the United States Institute of Peace.

9 foreign policy issues the Trump administration will have to face

The United States’ longest and most expensive war — with the largest U.S. troop presence in a combat zone — was mostly absent from the presidential campaign. On the rare occasions Trump has spoken about Afghanistan, he has sounded as conflicted as his predecessor.

Trump has said the fight against extremist groups is his foreign policy priority. But he also has said that he wants to get the United States out of the nation-building business.

“I would stay in Afghanistan,” Trump said in an interview with Fox News Channel last year. “I hate doing it. I hate doing it so much. But again, you have nuclear weapons in Pakistan, so I would do it.”

Not long after his election, Trump and Afghan President Ashraf Ghani discussed the U.S. military’s counterterrorism operations in Afghanistan, the Taliban’s cross-border havens in Pakistan, and the opportunities to jump-start Afghanistan’s potentially lucrative and long-dormant mining sector.

“So far, all of our interactions have given us the indication that those are going to be the two key areas of interest,” said Hamdullah Mohib, Afghanistan’s ambassador to the United States, referring to counterterrorism and business opportunities in his country.

The first big question for Trump is whether he can live with a stalemate that in 2016 cost the United States nine service members’ lives and $3.8 billion in military aid. For much of Obama’s second term, his war strategy was defined by the nation’s drawdown and eventual departure.

President Obama sits for a military briefing with the U.S. ambassador to Afghanistan, James Cunningham, left, and Marine Gen. Joseph Dunford at Bagram Air Base in Kabul on May 25, 2014. (Jonathan Ernst/Reuters)

Obama had been determined to cut the size of the U.S. force to about 1,000 troops before he left office — essentially ending the U.S. role in the war.

But Taliban advances and the uneven performance of Afghan troops forced him to drop the plan. Today, the 8,400 U.S. troops remaining in Afghanistan are focused on fighting international terrorist groups, such as al-Qaeda and the Islamic State, and helping to train Afghan army and police forces.

“Given the president-elect’s focus on terrorism, there may be opportunities to do more, but I find it difficult to believe that he’ll do a lot more,” said Zalmay Khalilzad, the U.S. ambassador to Afghanistan during the George W. Bush administration.

Breaking the current stalemate will be tough, but Trump could bolster the local military effort by boosting the number of U.S. combat and surveillance planes supporting Afghan ground forces in their fight against the Taliban.

As U.S. forces withdrew from the battlefield after Obama’s 2010 troop surge, air and intelligence support to Afghan forces was significantly scaled back. Over the past two years, Obama has authorized steps to restore some of that military support, but Trump could do much more with relatively small increases in the size of the American force.

Eventually, Trump will have to confront bigger questions about the United States’ long-term plans for Afghanistan and its long-troubled relationship with Pakistan, an unstable nuclear power where key elements of the Taliban find shelter.

The Obama administration’s overriding focus on a drawdown, critics allege, meant that the White House never took a long view of the war and the goal of an American presence there. Some senior U.S. military officials have touted Afghanistan as a potential launchpad for counterterrorism operations throughout Central Asia. Others have pointed to the American presence as a means of influencing a troubled region.

“If you are going to be projecting force regionally from Afghanistan, then it makes sense to come up with a five-year plan,” said Matt Sherman, who served as a senior adviser to the military in Kabul. “How could we use our role to pressure Pakistan, Russia, China or Iran? Are there areas of mutual interest where we could cooperate? You could look at Afghanistan in a fundamentally different way.”

Trump also will have to decide whether he wants to take a different approach in Pakistan. In 2012, he blasted the country’s leaders, citing allegations that elements of Pakistan’s military had provided sanctuary to Osama bin Laden.

“Get it straight: Pakistan is not our friend,” he tweeted at the time. “We’ve given them billions and billions of dollars, and what did we get? Betrayal and disrespect, and much worse.”

More recently, Trump lavished praise on Pakistani Prime Minister Nawaz Sharif, calling him a “terrific guy,” according to a summary of the conversation compiled by Pakistani officials.

Trump must decide whether it makes sense to put more pressure on Islamabad in an attempt to force the Taliban into peace negotiations. Last year, Obama ordered a drone strike that killed Taliban-leader Mullah Mansour in the country’s southwestern province of Baluchistan, setting a potential precedent for U.S. military strikes in that longtime Taliban sanctuary.

Trump could decide to authorize a campaign of similar strikes as part of a broader push to pressure Pakistan to force the Taliban to the negotiating table, a move that Obama rejected as he sought to keep his promise to end the war.

But Trump, who has touted himself as the ultimate dealmaker, could take a different approach to the Pakistan problem by boosting strikes or withholding aid.

“There hasn’t been accountability,” Khalilzad said. “Maybe recalibrating the relationship could produce a change?”

Trump may have little experience in international affairs, but his foreign policy team has a long history in Afghanistan. Retired Gen. James Mattis, Trump’s defense secretary nominee, led the first Marine units into the country in late 2001, and retired Lt. Gen. Michael T. Flynn, his national security adviser, led U.S. military intelligence in the country from 2009 to 2011.

Both are also painfully aware of the country’s problems, which extend far beyond the Taliban to include corruption, poor governance and a thriving narcotics trade.

Recently, the U.S. special inspector general for Afghanistan reported that “as much as half” of the fuel purchased by U.S. taxpayers for Afghan police and military forces is siphoned off by mismanagement and corruption. Such statistics are certain to infuriate Trump, who has railed against Pentagon waste.

Ultimately, Trump will have to decide what is possible in the country after nearly 15 years of American support and sacrifice.

“The Obama administration tried much too hard to end the war much too fast,” weakening the chances for success, said Ronald E. Neumann, who also served as U.S. ambassador to Afghanistan under Bush. But, in Neumann’s view, the current level of American support appears to be enough to prevent a collapse.

“There’s not going to be any evacuation off the embassy roof,” he said. “Afghanistan is sustainable with what we have now.”

A major terrorist attack emanating from Afghan soil or the Taliban takeover of a major Afghan city could change Trump’s calculus. For now, though, he will have to decide whether he can live with a hard-fought standoff.