/* Unsaved preview only. Receives calculated builder lines; never saves or publishes. */
(function(host){
 const {useEffect,useRef}=React;
 function PromotionSheetPreview({lines=[],isPrice,say,startsOn,endsOn,theme,themes,onClose}){
  const dialog=useRef(null),close=useRef(onClose);close.current=onClose;
  useEffect(()=>{
   const previous=document.activeElement,overflow=document.body.style.overflow;document.body.style.overflow='hidden';dialog.current?.querySelector('button')?.focus();
   const keydown=e=>{if(e.key==='Escape'){e.preventDefault();e.stopPropagation();close.current?.();}if(e.key==='Tab'){e.preventDefault();dialog.current?.querySelector('button')?.focus();}};
   document.addEventListener('keydown',keydown,true);
   return()=>{document.removeEventListener('keydown',keydown,true);document.body.style.overflow=overflow;if(previous?.isConnected)previous.focus();};
  },[]);
  const Artwork=host.PromotionTicketArtwork,pages=Math.ceil(lines.length/10);
  return host.ReactDOM.createPortal(<div className="psp-backdrop"><section ref={dialog} className="psp-modal off-promotions-body" role="dialog" aria-modal="true" aria-labelledby="psp-title" aria-describedby="psp-description">
   <header className="psp-header"><div><h2 id="psp-title">Full sheet preview</h2><p id="psp-description"><strong>UNSAVED DRAFT PREVIEW</strong> · {lines.length} tickets · {pages} A4 sheet{pages===1?'':'s'} · 70 × 50 mm</p></div><button className="btn btn--secondary btn--sm" onClick={onClose}>Back to builder</button></header>
   <div className="psp-note">These are the current unsaved calculations. Closing this preview keeps your builder unchanged. Save a draft before opening printable tickets.</div>
   <div className="psp-scroll">{!lines.length?<div className="psp-empty">No eligible lines in the current preview.</div>:!Artwork?<div role="alert" className="psp-empty">Ticket artwork could not be loaded. Return to the builder and reload the page.</div>:Array.from({length:pages},(_,page)=><div className="psp-page-wrap" key={page}><div className="psp-page-label">Sheet {page+1} of {pages} · UNSAVED DRAFT PREVIEW</div><div className="psp-sheet"><div className="psp-grid">{lines.slice(page*10,(page+1)*10).map((line,index)=><div className="psp-ticket" key={line.productId||line.sku||index}><Artwork name={line.name} sku={line.sku} beforeCents={line.beforeCents} priceCents={isPrice?line.promoCents:null} say={say} startsOn={startsOn} endsOn={endsOn} theme={theme} themes={themes} isPrice={isPrice}/><span className="psp-draft-mark">UNSAVED DRAFT PREVIEW</span></div>)}</div></div></div>)}</div>
  </section></div>,document.body);
 }
 host.PromotionSheetPreview=PromotionSheetPreview;
})(window);
