One could do equity value + discounted payouts - discounted invested capital. This includes payments on debt and possibly taxes.
This measure would not be a "value" in an economic sense as valuation is in present terms, incorporating future potential and using the past only to inform assumptions. This measure catalogs a company's time-integrated capital productivity. This could inform a valuation, but taking an extreme case, it would value dearly a shell company that just paid its assets out in a dividend.
With the subjectivity complexities like cross-border activity or M&A introduce the exercise has little value beyond the pedagogical.
To answer "what is the most valuable company in history" I would look for the company which paid out the most in dividends before it expired. Stock price is largely a measure of which company has managed to attract the most suckers.
These definitions would also allow comparing non-standard corporations, like churches and states. It seems like this is a natural comparison - it's inevitable that big companies are ranked among comparable countries.