// The public-offer page renderer: splices the live catalog price list into BOTH language sources of
// the owner-edited offer markdown at the pricing marker, and renders the bilingual /offer/ page with
// the SAME renderOfferHtml the browser build uses (bundled from ui/src/lib/offer). The English view
// transliterates the Russian product names client-side (renderLegalHtml). Kept out of server.mjs so
// the substitution is unit-testable without HTTP.
import { renderOfferHtml } from '../dist/gameimage.mjs';

// pricingMarker is the token ui/legal/offer_{ru,en}.md carry at §4.4 where the price list belongs. It
// mirrors the backend marker (backend/internal/payments/offer.go) and is replaced with the fetched
// tables before rendering.
export const pricingMarker = '<#pricing_template#>';

// renderOffer returns the standalone /offer/ HTML: the RU + EN offer markdown with the pricing marker
// replaced by pricingTables (the two markdown tables the backend projects from the active catalog),
// rendered to a self-contained bilingual document. The replacement is literal (a function replacer)
// so a "$" in a product title is never read as a replacement pattern; a missing marker leaves the
// text as is.
export function renderOffer(ruMarkdown, enMarkdown, pricingTables) {
  const splice = (md) => md.replace(pricingMarker, () => pricingTables);
  return renderOfferHtml(splice(ruMarkdown), splice(enMarkdown));
}
