Retirement tax questions

The mega-backdoor thing I'm referring to is described here, for example (https://www.nerdwallet.com/article/investing/mega-backdoor-roths-work). My company used to use Fidelity and now uses Alight, and in both instances when I contributed Roth/traditional/after-tax amounts, they went to a single account. So if I contributed $50,000 in a single year, with $10,000 pre-tax, $10,000 Roth, and $30,000 after-tax to an empty account, then my Fidelity/Alight 401k would show me as having 1 401k account with $50,000 in it. Now, I understand that earnings on after-tax contributions are considered pre-tax amounts, so if my $50,000 doubled to $100,000 without any further contributions, the new amounts would be $50,000 pre-tax, $20,000 Roth, and $30,000 after-tax -- the nominal amount of after-tax amount in the 401k never increases without further contributions, as far as I'm aware, because any earnings on after-tax amounts are considered pre-tax.

 

Now, what I gather from what you're saying is that a loan repayment, made with after-tax dollars, will be allocated as pre-tax or Roth depending on whether the loan came from the pre-tax funds or the Roth funds. If that's the case, then that is at least consistent, but is there anywhere where it spells out exactly how the loan repayment should be treated for the purpose of rollovers? I ask because of the following specific hypothetical:

 

Let's say my 401k account can take pre-tax, Roth, and after-tax contributions to a single account, with accounting on the back-end to allow contribution bases to be tracked. I contribute $20,000 pre-tax and then take out a $10,000 loan, repaying it over time with interest such that my new balance is $22,000 (= $10,000 not loaned + $10,000 loaned + $2,000 interest). Since the repayments are after-tax, and would need to be reported as such to prevent me from deducting them on my tax return, they must add to the "after-tax" pool in my 401k. However, maybe I decide to roll everything (all $22,000) into one or more IRAs. It is true that after-tax amounts can be rolled to Roth IRAs without paying any additional taxes (else the mega-backdoor Roth IRA would not be possible) and it is true that $12,000 of my account in this scenario is after-tax, so would it be true that I can take the $12,000 in after-tax amounts from my account and roll it into a Roth IRA, while taking the remaining $10,000 traditional amount and rolling it into a traditional IRA? If not, where along this sequence of events have I made an illegal move or assumed something incorrect?