New York (CNN Business) Beds-in-a-box have brought the country's largest mattress retailer to its knees.

Mattress Firm filed for bankruptcy on Friday to escape hundreds of leases it could no longer afford in suburban shopping malls and on city streets.

Mattress Firm has more than 3,300 stores across the country, and it plans to quickly shutter 200 of them. By the end of the year, the company may close up to 500 more.

Mattress Firm's struggles have been, in part, self-inflicted. The company failed to recognize that its marketplace was moving online until it was too late. It misjudged what shoppers demanded — convenience and low prices — and ignored the threat digital upstarts like Casper posed to its grip on the industry.

The company was constrained by its decision to buy retail chain Sleepy's in 2015 for $780 million. Instead of investing in digital tools and shipping infrastructure, Mattress Firm expanded its store base at exactly the wrong moment.

"This transformational acquisition unites the nation's two largest mattress specialty retailers, providing customers with convenience, value and choice," Mattress Firm's chief executive Steve Stagner said at the time.

But taking on more than 1,000 Sleepy's stores left Mattress Firm over-retailed. As store traffic slowed, costly leases turned into an albatross around Mattress Firm's neck.

In a Chapter 11 filing, Stanger said that his company had "too many locations in close proximity to each other."

Mattress Firm built out its stores as an effective online direct-to-consumer model began to emerge.

Casper, as well as Tuft & Needle, Leesa, Purple, and Yogabed, took advantage of retailers clinging to the status quo by offering shoppers a new solution for their bedding needs: shipping mattresses in a box to their homes for no added costs.

"Traditional mattress retailers have been alienating customers for decades and are now buckling under pressure," Philip Krim, Casper's co-founder and CEO, said in an email. "Casper has turned a tired industry on its head with innovative products and a superior shopping experience."

Casper believed that shoppers had grown weary of the traditional mattress-buying experience: going into a store, testing out a handful of mattresses for a few minutes, and rushing to a decision about an expensive item designed to last for years. Customers were also turned off by complicated and expensive delivery options.

The company gained attention online with customers posting viral videos of themselves unboxing mattresses, racking up $100 million in sales in 2015.

Casper distinguished its business by pricing the only mattress it sold at the time below traditional competitors, as well as offering free delivery and a 100-day trial period at home.

Casper offered just three varieties of mattresses, which gave it an advantage over stores that sold a confusing array of soft mattresses, firm, foam, springy, and everything in between.

"Consumers have long gone into traditional mattress stores feeling uninformed and have been subject to ridiculous price points," Bob Phibbs, CEO of the consultancy Retail Doctor, told CNN Business in August

Casper continued selling direct-to-consumer online, moving into bed frames, sheets, pillows, and dog mattresses, and it partnered with retailers like Nordstrom, Target, and West Elm to increase distribution.

By the time Mattress Firm launched its own bed-in-a-box brand Tulo in 2017, it was too deep in debt to escape its troubles.

The rise of online companies wasn't the only source of pressure on Mattress Firm.

In addition, a top Mattress Firm supplier, Tempur Sealy International, stopped selling to the company over a pricing dispute. And Mattress Firm's parent company, Steinhoff International, has been bogged down with accounting woes that forced its CEO to resign last year.

Still, physical retail remains an important component of the mattress market.

In August, Casper announced it would open 200 stores in the next three years in an attempt to build loyalty with current shoppers and reach new ones.

Mattress Firm hopes a slimmed down store footing will allow it to remain relevant with shoppers who need a new bed.

"The process we have initiated today will allow us to strengthen our balance sheet and accelerate the optimization of our store portfolio," said CEO Stagner.