Typically travelling from country A to country B contains TWO separate immigration transactions
- Exit from country A, which is handled by country A exit immigration
- Entry into country B, which is handled by country B entry immigration.
In many cases these two transactions are completely independent and no data or information is exchanged. In other cases they may exchange information but what exact form this takes, is difficult to know.
In either case, there is NO requirement to use the same passport for both transactions. It's perfectly legal and normal to switch passports between transactions and choose the one that works best for a specific country. In some cases you MUST switch passport since some countries (like the US) requires you to use your US passport when entering and/or leaving the passport country.
show passport B at check-in, then passport B at exit immigration, then passport A at arrival immigration.
No. The passport you show at check-in MUST be the passport that you will use to enter the destination country. This is NOT arbitrary. The airlines needs to verify whether you can legally enter the destination country. Whether you can legally leave the departure country (or not) is none of their concern.
If I understand APIS corrctly, someone along this line will figure out that you are showing them the passport that you didn't check-in with.
Not necessarily. APIS is solely administered by the destination country. The airline collects all relevant data at check in and supplies it to the destination country. In some cases, they may also share some or all of this data with the departure country as well. That has nothing to do with APIS per se, but is governed by local laws and agreements in the departure country.
Especially if two passports have different names on them, this might get complicated.
Not necessarily. It's perfectly fine and legal to use different passports even with different names.
... if the data from departure country is linked all the way to the country of destination with all the passengers data.
It might, it might not. This depends on the agreements between the different countries and the airlines. These are typically not public knowledge, so it's difficult to predict what may be shared and what isn't. APIS per se doesn't require any data to be shared with the departure country.
security, at immigration exit and immigration entry at the country of destination
Security only checks whether you have a valid boarding pass and an ID that matches. That can be either passport or something completely different. For example, in the US I can use by US driver's license at the security check point even if I'm travelling internationally on a non-US passport.
All of this of course assumes that it is legal and acceptable for you to have two passports in the first place. If it isn't, then you should avoid flying directly from Ukraine to the US. If you try anyway, you might get lucky but it's impossible to know up front what will happen.