OCEANPORT, N.J. — Monmouth Park Racetrack bet its future against long odds, and it’s about to cash in on what it hopes will be a jackpot.

Five years ago, when sports betting was illegal essentially everywhere but Nevada, the 147-year-old horse racing track near the Jersey Shore invested $1.5 million in turning a drab cafeteria into a gleaming, 300-seat bar that could begin taking bets at the turn of a switch. The move was based on a speculative bet that the state would prevail in a fight against the major sports leagues to legalize sports betting.

The bet appears to have paid off: The U.S. Supreme Court ruled against the sports leagues in May, and the New Jersey Legislature is set for a vote as soon as Thursday on legislation to clear the final roadblocks that keep Monmouth Park’s bar from being the first licensed, full-service sports betting parlor in the state.

But the size of Monmouth Park’s payday is still unclear. After fighting an all-out war for years against sports betting, the leagues are now trying to jockey for a piece of the action. Major League Baseball and the National Basketball Association, in particular, have quietly been working behind the scenes for months as they braced for a pivotal decision from the Supreme Court, teaming up on a multistate lobbying blitz to have a say in how sports betting should be legalized.

MLB and the NBA have been pressing for “integrity fees” of up to 1 percent of all bets placed on games — purportedly for policing against game-fixing — and drafted what’s known as model legislation to help get the fees codified into law in each state. To make their case, they have been bankrolling more than 80 lobbyists in more than a dozen statehouses and dispatching top officials across the country — from Kansas to New York — to testify in legislative hearings.

All this is part of an influence game that pits the leagues against a host of other special interest groups — including the gambling industry, college athletic programs, players’ unions and even service providers for gambling addicts — while Congress ponders whether to wade into the legal thicket and come up with a new federal framework to regulate sports betting.

The attraction? Billions of dollars in projected profits could be reaped from what’s long been the domain of illegal barroom bookies and offshore operators. Legalized sports betting is estimated to be worth $41.2 billion a year in the U.S., according to a 2017 study commissioned by the American Gaming Association, a trade group that represents the casino industry. Once regulated, it could generate nearly $9 billion in federal, state and local taxes.

For Monmouth Park and other struggling gambling venues, the stakes are high: The outcome of the leagues’ lobbying campaign could make or break their future success, as integrity fees threaten to take the lion’s share of profits from sports betting.

Dennis Drazin, chairman and CEO of the management company that operates Monmouth Park, said the leagues should be the ones paying New Jersey, which racked up millions of dollars in legal fees for its fight to legalize sports betting.

“The leagues could have sat down with us years ago, but they didn’t,” Drazin said. “I just think it’s wrong for them to be coming here at this point trying to seek any money from us.”