BEIJING (BLOOMBERG) - When the Taleban took over Afghanistan the first time in 1996, China refused to recognise their rule and left its embassy shut for years.
This time around, Beijing has been among the first to embrace the Islamist militants next door.
China's remarkable shift was on display little more than two weeks ago, when Foreign Minister Wang Yi welcomed a Taleban delegation to the northern port of Tianjin as the group made gains against the administration of President Ashraf Ghani, who fled the country on Sunday (Aug 15).
Mr Wang's endorsement of the Taleban's "important role" in governing Afghanistan provided a crucial boost of legitimacy for an organisation that has long been a global pariah due to its support of terrorism and the repression of women.
China's reasons have as much to do with its own rise as a global power as the Taleban's surprisingly swift march on the Afghan capital.
China today commands an economy worth US$14.7 trillion (S$19.9 trillion) - more than 17 times its size in 1996 - and a massive trade-and-infrastructure initiative that stretches across the Eurasian landmass.
Beijing's fears about Islamist extremism among its own Uighur minority have also deepened in recent years, leading it to build a vast police state adjacent to Afghanistan.
Moreover, an increasingly intense rivalry with the United States has prompted Chinese President Xi Jinping to seize any opportunity to push back against Washington's dominance and push American forces away from his borders.
Those interests make China look like the next great power with a stake in bringing order to Afghanistan as the Taleban prepare to declare an Islamic Emirate in Kabul.
After the failures of the Soviet Union and now the US, China will be hard pressed to avoid repeating the same mistakes in a rugged and landlocked nation notorious for exhausting empires.
"Twenty years ago, China wasn't a global power and what was happening in Afghanistan didn't bother China," said Ms Yun Sun, director of the China Programme at the Washington-based Stimson Centre.
"But today, there are so many new factors - there's the Uighur issue, there's economic interests and China's self-perception as a global power."
China has sought to portray itself as more pragmatic and less interventionist than the West while urging a negotiated peace.
"China hopes the Afghan Taleban can unite with other political parties and with all ethnic groups and build a political framework in keeping with national conditions that is broadly inclusive and will lay the foundation for enduring peace," Foreign Ministry spokesman Hua Chunying told reporters on Monday in Beijing.
Still, Ms Hua stopped short of endorsing the Taleban's rule, saying only that the situation in Afghanistan had "experienced major changes".
It was all part of a long evolution by China, which once denied any connection with the Taleban before hosting the first delegations from the group in 2013. Now, as Taleban fighters made their entrance into Kabul, posts circulated on China's heavily censored social media comparing the event to Mao Zedong's taking of Beijing in 1949.
Meanwhile, state media gloated over the American withdrawal, with a commentary in the official Xinhua News Agency declaring it the "death knell for declining US hegemony".
It said: "The sound of roaring planes and the hastily retreating crowds mirrored the last twilight of the empire."
Even so, China is one of the few countries that saw some benefit from the US' US$840 billion nation-building debacle, which bogged down Beijing's rivals while creating a relatively stable environment for its companies.
That has left China with economic interests to secure, including a copper mine and several oil blocks. The country evacuated some 200 business people last month.
Afghanistan's stability is key to protecting more than US$50 billion worth of Belt and Road projects in neighbouring Pakistan that provide a crucial overland route to and from the Indian Ocean.
But perhaps no issue is as pressing for Beijing as ensuring that Afghanistan does not become a source of extremism that bleeds over the Chinese border.
Mr Wang, the Chinese foreign minister, pressed chief Taleban negotiator Mullah Abdul Ghani Baradar during their July 28 meeting to make a clean break with the East Turkestan Islamic Movement. China blames the group for terrorist attacks it has cited to justify crackdowns in the Xinjiang region.
Mr Baradar pledged that the Taleban would never allow any force to use Afghan territory to engage in acts detrimental to China, according to the Chinese statement.
"China's attitude towards a Taleban-led regime will depend on its policies, for instance, whether the Taleban will honour its promises and not to become a hotbed for extreme forces that have links to China," said Professor Fan Hongda from the Middle East Studies Institute of Shanghai International Studies University.
Afghanistan could become the biggest test yet of a Chinese diplomatic model that is driven by loans, commodities and infrastructure deals rather than demands for liberal policies.
If the Taleban pursue moderate policies towards women that do not alienate other nations, and achieves political stability, Beijing might consider an array of investments similar to what it has done in Pakistan, according to Stimson Centre's Ms Sun.
"The Chinese approach is, 'through economic infusion we create roads, we create infrastructure, and we make sure everyone has jobs'," she said.
"And if everyone goes to work at nine in the morning and comes home at 6pm, they don't have time to think about terrorism."