LONDON, Feb 9 (Reuters) - Before Moscow's invasion of Ukraine two years ago, Russia's Nasdaq-listed search engine giant Yandex was briefly worth $30 billion. This week, a consortium of domestic investors agreed a deal to buy it for $5.2 billion.

In normal times, that would represent a disaster for Dutch parent company Yandex NV and its Western shareholders.

But in a world where Western firms have left Russia in droves, sometimes for a nominal fee, and Moscow has unilaterally seized foreign-owned assets, there is a sense of relief, if not triumph after 18 months of tense negotiations.

Reuters spoke to nine people familiar with parts of the negotiation process, including employees of Yandex and advisers, investors and negotiators on both sides to establish how the deal came about and what may come next.

The sources declined to be named because of the sensitivity around the deal before it closes.

"Great work has been done," former Russian finance minister turned Yandex advisor Alexei Kudrin, whose meetings with President Vladimir Putin in 2022 were crucial in securing a green light to proceed with the deal, said on Monday. "The process is underway."

Six months earlier, the purchase of Yandex's Russia-based businesses, which generate more than 95% of revenue, by Russian investors looked in peril when the company's co-founder Arkady Volozh called the invasion of Ukraine "barbaric".

With the Kremlin highly sensitive to criticism of what it calls a "special military operation", some on the Russian side of talks wanted to leave Volozh and Yandex NV with nothing, according to four sources familiar with the matter.

But Moscow's fear of losing more skilled technology workers to its war-induced brain drain prevented it from forcibly seizing assets and got negotiations back on track, the sources added.

The Kremlin welcomed the deal on Monday but did not respond to a request for further comment. Kudrin and Yandex NV declined to comment further while Volozh did not respond.

SHRINKING POOL OF BUYERS

At other times, new Western sanctions on potential buyers, and Kudrin himself, caused delays and adjustments. The negotiations, driven by Yandex, were highly complex, with the deal requiring Russian, U.S. and European Union approval.

Once closed, it will be the most significant corporate takeover of the last two years in Russia, leaving one of the few local companies with true global potential before the war under domestic control.

The deal will eliminate Western oversight of Yandex, often dubbed "Russia's Google" and tighten the Kremlin's control over Russia's internet space.

Negotiators needed to find a suitable transaction currency, ultimately settling on China's yuan, adjust Yandex's corporate structure and develop a plan for transferring the Moscow Exchange listing from one company to another.

Success seemed so far off that a person involved at one point quipped: "It feels like we're rearranging the deckchairs on the Titanic."

Potential buyers, including well-known Russian billionaires with ties to the Kremlin, initially bid for half of Yandex for $7 billion but as the pool of potential investors shrank, Volozh's anti-war declaration in August came closest to derailing the process and pushed the price lower, four of the sources said.

Yandex declined to comment on parts of this story but stressed that complying with sanctions regulations was crucial for both sides.

Meanwhile, the expropriation risk left Yandex NV viewing the extraction of any money, even at a huge discount, as a relief, two of the people said.

The Kremlin's success in establishing Russian ownership at a knock-down price is more apparent. Yandex's online services, from taxi and food delivery to search, are ubiquitous in the country.

A group of Yandex managers will become leading shareholders with a 35% stake - they stressed that Yandex would retain its independence, crucial for a technology company where staff are the primary asset.

The remaining 65% will be split between oil major Lukoil and structures owned by businessmen Alexander Chachava, Pavel Prass and Alexander Ryazanov.

A NIGHTMARE

As it encouraged shareholders to approve the deal, Yandex NV pointed to Western firms such as Finnish energy group Fortum, French dairy group Danone and Danish brewer Carlsberg which have had their local assets seized by Moscow.

Its sales pitch goes that AI-focused businesses in cloud, data solutions, self-driving technology and education technology will flourish when headquartered in Amsterdam.

The risk of the Dutch company being immediately deprived of intellectual property rights did not come to pass with licences granted until end-2024.

But Volozh - who holds an 8.5% economic interest through a family trust although no voting rights - is still under EU sanctions.

A person close to Yandex NV said that could prevent him from playing a role in the future company, although two sources said legal arguments against Volozh were easing given the proposed full divestment.

Either way, Yandex NV is counting on developing the four businesses quickly, with global partners ready to collaborate as soon as the deal is done, four people said.

Yandex NV said it planned to return a "substantial portion" of net cash proceeds to remaining shareholders via a buyback but how much they stand to receive is still unclear.

One investor with a shareholding once worth around $200,000 curtly summed up their feelings:

"This deal is a nightmare."

(Reporting by Alexander Marrow, Darya Korsunskaya, Polina Devitt; additional reporting by Gleb Stolyarov; Editing by Mike Collett-White, Kirsten Donovan)