function usePublicBookingCatalog(business) {
  const [state,setState]=React.useState({business,loading:!!business,data:null,error:''});
  const [revision,setRevision]=React.useState(0);
  const generation=React.useRef(0),pending=React.useRef(false),live=React.useRef(false);
  React.useEffect(()=>{
    live.current=true;const token=++generation.current;pending.current=false;
    setState({business,loading:!!business,data:null,error:''});
    if(business)Promise.resolve().then(()=>window.BOOKUNO_API.getPublicBookingCatalog(business)).then(data=>{
      window.BOOKUNO_PUBLIC_CATALOG.validate(data,business);
      if(live.current&&token===generation.current)setState({business,loading:false,data,error:''});
    }).catch(()=>{if(live.current&&token===generation.current)setState({business,loading:false,data:null,error:'Booking choices could not be loaded. Please try again.'});});
    return()=>{live.current=false;generation.current++;};
  },[business,revision]);
  const current=state.business===business?state:{business,loading:!!business,data:null,error:''};
  async function more(kind){
    if(pending.current||current.loading||!current.data||!['session','pitch'].includes(kind))return;
    const cursor=current.data['next_'+kind+'_cursor'];if(!cursor)return;
    pending.current=true;const token=generation.current;
    setState({...current,more:kind,moreError:''});
    try{const next=await window.BOOKUNO_API.getPublicBookingCatalog(business,{[kind]:cursor});
      const merged=window.BOOKUNO_PUBLIC_CATALOG.merge(current.data,next,kind);if(live.current&&generation.current===token)setState({...current,data:merged,more:null,moreError:''});
    }catch(_){if(live.current&&generation.current===token)setState(previous=>({...previous,more:null,moreError:'More choices could not be loaded. Your current choices are still here. Try again.'}));}
    finally{if(token===generation.current)pending.current=false;}
  }
  return {...current,morePending:current.more,refresh:()=>setRevision(n=>n+1),loadMore:more};
}
function PublicBookingChoices({source,business,hideEmpty=false}) {
  const first=React.useRef(null);
  const money=(cents,currency)=>new Intl.NumberFormat(window.BOOKUNO_REGION.locale(),{style:'currency',currency}).format(cents/100);
  const date=row=>window.BOOKUNO_REGION.dateFormatter({timeZone:row.timezone,dateStyle:'medium',timeStyle:'short'}).format(new Date(row.starts_at));
  const data=source.data;
  const hasChoices=!!(data&&(data.sessions.length||data.pitches.length||window.BOOKUNO_PUBLIC_CATALOG.appointments(data,business.id,business.services).length));
  if(hideEmpty&&data&&!hasChoices&&!source.loading&&!source.error)return null;
  const jump=id=>{const target=document.getElementById(id);target?.scrollIntoView({block:'start',behavior:'auto'});target?.focus({preventScroll:true});};
  return <section id="sf-booking-choices" className="public-booking-choices" tabIndex={-1} ref={first} aria-labelledby="public-booking-title" data-frame-control="true">
    <p className="public-booking-eyebrow">{uiText("Book with ")}{business.name}</p><h2 id="public-booking-title">{data&&!hasChoices?uiText("Booking information."):uiText("Choose what works for you.")}</h2>
    {hasChoices&&!source.loading&&!source.error&&<nav className="public-booking-jumps" aria-label={uiText("Booking choices")}>{data.sessions.length>0&&<button type="button" className="btn outline" aria-controls="sf-departures" onClick={()=>jump('sf-departures')}>{uiText("Sessions & departures")}</button>}{data.pitches.length>0&&<button type="button" className="btn outline" aria-controls="sf-overnight" onClick={()=>jump('sf-overnight')}>{uiText("Overnight stays")}</button>}{window.BOOKUNO_PUBLIC_CATALOG.appointments(data,business.id,business.services).length>0&&<button type="button" className="btn outline" aria-controls="sf-appointments" onClick={()=>jump('sf-appointments')}>{uiText("Appointments")}</button>}</nav>}
    {source.loading?<p role="status">{uiText("Loading booking choices…")}</p>:source.error?<div role="alert"><p>{uiText(source.error)}</p><button type="button" className="btn outline" onClick={source.refresh}>{uiText("Reload booking choices")}</button></div>:data&&<>
      {data.sessions.length>0&&<div className="public-booking-section" id="sf-departures" tabIndex={-1}><h3>{uiText("Sessions & departures")}</h3><p>{uiText("Choose a dated session. Places are checked again when you reserve.")}</p><div className="public-booking-grid">{data.sessions.map(row=><article key={row.id}><h4>{row.title}</h4><p>{date(row)}<br/>{row.timezone}</p><strong>{money(row.unit_price_cents,row.currency)} <span>{uiText("per place")}</span></strong><p>{row.booking_supported?(row.available===0?uiText("Currently full"):row.available+uiText(" places available")):uiText("Contact the business about booking.")}</p><a className="btn outline" href={window.BOOKUNO_PUBLIC_CATALOG.link('session',row,business.id)}>{row.booking_supported&&row.available>0?uiText("Choose places"):uiText("View session")}</a></article>)}</div>{data.next_session_cursor&&<button type="button" className="btn outline public-booking-more" aria-disabled={!!source.morePending} onClick={()=>source.loadMore('session')}>{uiText("More departures")}</button>}</div>}
      {data.pitches.length>0&&<div className="public-booking-section" id="sf-overnight" tabIndex={-1}><h3>{uiText("Overnight stays")}</h3><p>{uiText("Choose a space, then check your dates and guest count.")}</p><div className="public-booking-grid">{data.pitches.map(row=><article key={row.id}><h4>{row.name}</h4><p>{uiText("Up to ")}{row.max_guests}{uiText(" guests · ")}{row.min_nights} {row.min_nights===1?uiText("night"):uiText("nights")}{uiText(" minimum")}</p><strong>{money(row.nightly_price_cents,row.currency)} <span>{uiText("per night")}</span></strong><p>{uiText("Final availability and stay total depend on your dates.")}</p><a className="btn outline" href={window.BOOKUNO_PUBLIC_CATALOG.link('pitch',row,business.id)}>{row.booking_supported?uiText("Choose stay dates"):uiText("View stay details")}</a></article>)}</div>{data.next_pitch_cursor&&<button type="button" className="btn outline public-booking-more" aria-disabled={!!source.morePending} onClick={()=>source.loadMore('pitch')}>{uiText("More spaces")}</button>}</div>}
      {source.moreError&&<p role="alert">{source.moreError}</p>}
      {!data.sessions.length&&!data.pitches.length&&!window.BOOKUNO_PUBLIC_CATALOG.appointments(data,business.id,business.services).length&&<p>{uiText("No online booking choices are listed right now.")}{(business.phone||business.email)&&uiText(" Use the business’s contact details for orders or enquiries.")}</p>}
    </>}
  </section>;
}
