Investors & landlords

A twist, if I may ...
If the property was refinanced, what amount is used to determine the adjusted basis for the loss/gain calculation at the sale?
I refinanced a number of years back to lower the rate, at the time I took at loss rather than foreclosing -I actually had to pay out of pocket to pay off the difference between the balance on the original mortgage and the new refinanced mortgage?
say bought for $105,000, then when $88,000 was still owed, I refinanced for $46,000 and paid $42,000 to cover the loss in value on the property.

I now sold for $66,000.  Also ended up taking $94,500 in depreciation - using original price depreciation calculation for lifetime of ownership.

So is it a $39,000 loss ($105,000 - $66,000) plus my depreciated total?
Or a $20,000 gain ($66,000 - $46,000) plus my depreciated total?
And what about the $42,000 pay down for the refinance?

Thank you?