The stock market is a funny place: When great businesses go on sale, most people don’t rejoice. Instead they freak out. This presents an excellent opportunity for long-term investors who keep their wits about them and triangulate between price and value.

As I perform my own triangulations, I find many tech stocks on sale today, though in this market, as in any market, one must discriminate. Some in the group are worth a look, while others deserve to be crushed. The reason is simple: Like companies in any other industry, most tech companies are doomed to failure, or at least mediocrity, by the viciously corrosive nature of free-market capitalism.

Only companies with moats, to use Warren Buffett’s famous and accurate metaphor, can withstand the intense competition it begets.

For this reason, the key to unlocking successful tech investments remains the same as it has in all previous generations. We must identify, buy, and hold companies with competitive advantages that will allow them to thrive while others languish.

Amazon AMZN, -1.99% is one such company. Down almost 34% in 2022 through Monday, I believe it represents an excellent long-term investment at Monday’s closing price of $2,216.21.

Here’s why:

Even the biggest skeptic will concede that Amazon has large and formidable moats around both its major businesses. Its original business, e-commerce, commands nearly 50% of all online retail traffic, and nobody comes close to matching its mix of selection, price and convenience plus its vast distribution network.

The company’s cloud platform, Amazon Web Services, has a similarly dominant market share of outsourced computing. And while AWS’s core business is renting out “dumb servers,” it bundles that commodity with a suite of powerful software and analytics tools that gradually become embedded in customers’ everyday business processes. This gives AWS a Prime-like stickiness.

Amazon’s moats are therefore beyond any reasonable doubt. The more salient issue is: Can they make money behind them? How much profit, exactly, can Amazon produce from behind its castle walls? This is a tricky question, because its profit history has been erratic.

Let’s get the easy part out of the way: Unlike core e-commerce, AWS shows consistently healthy GAAP profits. Last year, AWS made a 30% operating margin, in line with other capital-intensive tech businesses operating at scale.

AWS, however, has much more growth ahead of it than mature tech hardware companies like Cisco CSCO, -1.13%. Experts estimate that companies spend only about 10%-15% of their computing budget on the cloud today. Over the next generation, that figure could quadruple.

AWS made $18.5 billion in operating profit last year. Even if you decelerate its growth rate from the historical 35% to 20% over the next three years, AWS will make more than $25 billion in after-tax income in 2024. Capitalize that at 20 times, a reasonable multiple for a superior business even in today’s environment of rising interest rates, and it yields a value of roughly $500 billion. By coincidence, that’s roughly half of Amazon’s current market capitalization, less its cash on hand.

The question then becomes: What’s e-commerce worth? The answer depends on how much money e-commerce can make.

Having followed the company for 20 years, I’m convinced that e-commerce has the potential to earn roughly 10 times what it reported in 2021. That should strike any reasonable person as an outlandish statement, so let me explain why this constitutes rational rather than magical thinking.

My first point is that Amazon has shown it can produce profit more or less at will in e-commerce. It can harvest the business it’s growing behind its moat and show bountiful profits that would rival old-economy companies. Amazon has instead decided that with so much more business to capture, spending $1 today and penalizing current profits will generate multiples of that down the road.

Amazon IPO’d during the dot.com boom, and from 1997 to 2001 the company spent like mad to build out its e-commerce moat, producing $2 billion in cumulative operating losses. In the bust that followed, Amazon lost 80% of its equity market value. Chastened by the capital markets, Amazon went into harvest mode from 2002 to 2007, producing margins in the 5% to 6% range and nearly $2 billion in operating income.

E-commerce made 5% operating margins in 2009, and again in 2018 and 2019—but when the pandemic came, Amazon again went into investment mode. It doubled in 24 months the distribution infrastructure it had taken 24 years to build. No wonder that earlier this month it reported its first quarterly operating loss in six years.

Those who worry about overbuilding should have their head examined. Amazon’s network doubled in two years, but its sales grew “only” 65%, leading to overcapacity. If Amazon’s e-commerce division grows 10%-15% annually, which is well below its historical rate, it will take the company only three to four years to again reach maximum capacity. If sales grow closer to its five-year historical growth rate of 20%, the issue will be solved in two years, leading Bank of America analyst Justin Post to correctly write, “Growing into existing fulfillment capacity may be one of the easiest problems for Amazon to solve in its history.”

Still, given its recent price decline, Amazon could again be compelled by market forces to show higher profits like it did after the dot-com bust.

Read: Amazon looks to cut costs after first loss in seven years sends stock careening lower

Internal unrest could also trigger such a move. Over the last two years, Amazon has given employees stock worth $22 billion at the time of the grants—but the stock price is down over that time, and the rank and file will abide this for only so long.

My second point is that more than any other tech company, Amazon and its founder, Jeff Bezos, have proven they know how to invest. They can be trusted to make the trade-offs between current profits and future ones. Unlike most tech entrepreneurs, Bezos began his professional life at a hedge fund. He is a huge fan and student of Buffett, and he understands value creation down to his marrow.

Specifically, both Bezos and Buffett understand that it is smart to spend $1 today if there’s a reasonable chance you’ll make more than $1 in the future.

Avid Reader Press / Simon & Schuster

In the late 1990s, Buffett tanked GEICO’s reported profits by spending hundreds of millions of marketing dollars to win new customers. He knew that such spending would make GEICO appear less profitable in the present–but in the long run, he would reap the rewards.

Bezos and Amazon have behaved similarly. It’s true that Amazon’s spending in its early days could rightly be called speculative, but those days are over. With huge moats around its e-commerce business, most of Amazon’s spending now involves widening and deepening its moat—throwing sharks and alligators in it, to use Buffett’s colorful extension of the metaphor.

Finally, Amazon has powerful profitability levers to pull that it only recently acquired. It has leveraged the fact that nearly half of all e-commerce begins on its website into a $30 billion business selling advertising on the site. The ads business is three times the size of what it was only three years ago, and margins here likely approach 100%.

Moreover, most of what Amazon now sells on its website originates from third parties. When Amazon acts as merely a platform for other merchants, it avoids a retailer’s single biggest expense–the goods themselves. Driven by such developments, e-commerce’s gross margin, which is a good proxy for a business’ ability to drive operating profits, has grown from 25% to nearly 40% over the last decade.

Amazon’s e-commerce business reported operating margins last year were 1.5%. Given the above, I believe its latent earnings power—what it could earn should it decide to put itself into harvest mode–is roughly 15%.

AMAZON: VALUATION          
$ in billions  
  Operating After-Tax  
Segment 2024E Revenue Margin Income Multiple Value
Online Retail                    295 6%             18 20x             301
Third-Party Retail                    137 15%             21 20x             350
Advertising                      54 90%             48 20x             820
Physical Stores                      18 2%               0 20x                  6
Subscriptions                      43 0%              –   20x                –  
TOTAL RETAIL                    547 16%             87                             –            1,477
Cloud                    107 30%             32 20x             514
TOTAL Business Value          1,991
  Shares              0.5
  2024 Value/Share  $      3,904
  Price Today  $ 2,215
        Upside 76%

As you can see from this chart, assigning reasonable multiples to each of its segments yields a stock price of nearly $4,000 in two years. That’s 75% to 80% north of where the stock trades today — and why Amazon is both one of the top positions in my portfolio and why I’m adding to it now in accounts that have cash to deploy.

Adam Seessel is the founder and chief investment officer of Gravity Capital Management in New York and the author of “Where the Money Is: Value Investing in the Digital Age.

Now read: With interest rates rising, it’s time to focus on MANG stocks instead of FAANG, according to Jefferies

Plus: Here’s the best way to spot stock-market winners, according to this 25-year tech analyst

And: This surprising investing strategy crushes the stock market without examining a single financial metric