This might be considered more by Robert Lewandowski when his contract with Barcelona is over even though there are many other things that are still very open to happen to him in the future. Because as you said that at this time Robert Lewandowski is still very much needed by the Barcelona team because he can still help more young players to develop in the Barcelona team. And the choice to go to the Saudi Arabian League is actually very good for him before he really hangs up his boots from the world of football.
Robert Lewandowski can give his best for Barcelona before his contract ends and now his job is to focus on finishing the season with some much better achievements. Barcelona will not let go of Robert Lewandowski before his contract ends because his contribution is still needed enough to help the team achieve something that is expected and his presence can also provide a better front line. Regarding the future after his contract ends, it can be considered later, whether he plays in the Saudi Arabian League or not before entering retirement in football.
Maybe there could be another extension for one season so Lewandowski plays until 2027, but who knows what decision Barcelona will take whether to keep Lewandowski or choose to sell him and from the rumors circulating Barcelona still has interest in Alexander Isak which when they want to bring him in it is also very possible for Barcelona to have to release Lewandowski.
Best choice is to sell him, they have just got out of the red zone of financial problems so being too reckless by keeping players without getting sales money can be very detrimental.