RIO DE JANEIRO (Reuters) - Brazil’s state-run oil company is nearing a deal in which China National Petroleum Corp Ltd (CNPC) would invest in an oil refinery in exchange for crude oil, two people with knowledge of the talks told Reuters, potentially giving China its first refining capacity in the Americas.

FILE PHOTO: A Petrobras Oil platform is seen at Guabanara bay in Rio de Janeiro, Brazil September 24, 2010. REUTERS/Bruno Domingos/File photo

Petróleo Brasileiro SA PETR4.SA, or Petrobras, may give the state-owned Chinese firm stakes in oil fields it operates in the Campos basin, off the Rio de Janeiro coast, along with the right to use the new Comperj refinery, the sources added.

“We are going to have a deal, but it is complex. We should have an integrated solution,” said one of the sources, who requested anonymity because the negotiations are private.

The second source said talks with the Chinese intensified recently and a deal could be only a few weeks away.

Two other people familiar with the matter said the refinery needed about $3 billion of investment to reach an initial capacity of 165,000 barrels per day, adding that it was not clear if Petrobras would foot part of the bill.

Petrobras declined to comment. CNPC did not respond to a request for comment.

The talks highlight rising Chinese interest in the Brazilian energy sector, which has attracted billions of dollars from oil majors over the past year for rights to new exploration blocs as the government lowers barriers to foreigners.

Chinese state companies have also taken a central role in Brazil’s electric grid, acquiring rights to hydropower dams, major transmission lines and the country’s largest distributor.

The Comperj deal would not be the first in which Petrobras offered oil to attract Chinese funding. In 2016, it renewed a $10 billion credit line with China Development Bank, originally opened in 2009, that was guaranteed by Brazilian oil exports.

Slideshow ( 2 images )

China has also provided more than $50 billion to Venezuela over the past decade under oil-for-loan agreements that helped Beijing secure energy supplies for its fast-growing economy.

CNPC and Petrobras signed a memorandum of understanding last year to reach a strategic partnership in oil exploration and production. CNPC has already partnered with Petrobras in the Libra field of the Santos basin, one of the largest discoveries in Brazil’s prolific pre-salt oil area.

The investment at Comperj would help to offset Brazil’s fuel imports and resolve a long-running headache at the complex, where Petrobras has little to show for some $13.5 billion in investments over the past decade.

The project was caught up in a corruption probe in recent years and Petrobras has booked some 6.5 billion reais ($1.9 billion) in writedowns there linked to overpriced works and services.

The company said it wants to finish Comperj, but preferably without pouring in any more money, so it is talking to potential partners. Petrobras Chief Executive Pedro Parente said last year that CNPC was in talks to invest in the refinery.

On Thursday, Petrobras announced plans to sell 60 percent stakes in four of its existing refineries, underscoring its willingness to give up a near monopoly on Brazilian refining.

Parente told journalists the Comperj project was not included in the plans because it is not yet a productive asset.

He said negotiations for a partnership at the Comeperj refinery were still underway.

Separately, Petrobras awarded a contract last month worth 1.95 billion reais to China’s Shandong Kerui Petroleum Equipment Co to build a natural gas processing unit at the Comperj complex.

The privately run Kerui Group partnered with mid-sized Brazilian engineering company Método Potencial Engenharia SA to build the plant in Itaboraí, about 50 kilometers (30 miles) east of Rio.