function usePublicShop(business){
 const [state,setState]=React.useState({business,data:null,user:null,pending:null,loading:!!business,error:''});
 const version=React.useRef(0),alive=React.useRef(false),actor=React.useRef(null);
 const [revision,setRevision]=React.useState(0);
 React.useEffect(()=>{
  alive.current=true;const token=++version.current;setState({business,data:null,user:null,pending:null,loading:!!business,error:''});
  const api=window.BOOKUNO_API;
  async function load(){try{if(!business||window.BOOKUNO_CONFIG?.customerJourney!==true)return;const [data,session]=await Promise.all([api.getPublicShop(business),api.getSession()]);window.BOOKUNO_SHOP.catalog(data,business);const user=session?.user||null;const pending=user?window.BOOKUNO_GROUP_REQUESTS.read(`shop:${business}:${user.id}`):null;if(alive.current&&version.current===token){actor.current=user?.id||null;setState({business,data,user,pending,loading:false,error:''});}}catch(e){if(alive.current&&version.current===token)setState({business,data:null,user:null,pending:null,loading:false,error:e.message||'Shop unavailable. Please try again.'});}}
  const changed=(event,session)=>{if(event==='SIGNED_IN'&&session?.user?.id===actor.current)return;if(['SIGNED_IN','SIGNED_OUT','USER_UPDATED','PASSWORD_RECOVERY'].includes(event))setRevision(n=>n+1);};
  const sub=api?.onAuthStateChange?.(changed);load();return()=>{alive.current=false;version.current++;sub?.data?.subscription?.unsubscribe?.();};
 },[business,revision]);
 return {...state,refresh:()=>setRevision(n=>n+1),isCurrent:()=>alive.current&&state.business===business};
}
function PublicShop({source,business}){
 const [quantities,setQuantities]=React.useState({}),[email,setEmail]=React.useState(''),[password,setPassword]=React.useState(''),[busy,setBusy]=React.useState(false),[error,setError]=React.useState('');
 const running=React.useRef(false),live=React.useRef(true);React.useEffect(()=>{live.current=true;return()=>{live.current=false;};},[]);
 const data=source.data,api=window.BOOKUNO_API,H=window.BOOKUNO_SHOP,scope=source.user?`shop:${business.id}:${source.user.id}`:null;
 if(window.BOOKUNO_CONFIG?.customerJourney!==true)return null;
 if(source.loading)return <section className="public-shop" role="status">{uiText("Loading products…")}</section>;
 if(source.error)return <section className="public-shop" role="alert"><p>{uiText(source.error)}</p><button className="btn outline" onClick={source.refresh}>{uiText("Reload products")}</button></section>;
 if(!data||!data.enabled&&!source.pending)return null;
 const pending=source.pending,items=pending?.payload?.items||data.products.filter(p=>Number(quantities[p.id])>0).map(p=>({product_id:p.id,quantity:Number(quantities[p.id]),expected_unit_price_cents:p.unit_price_cents}));
 const total=items.reduce((sum,p)=>sum+p.quantity*p.expected_unit_price_cents,0),valid=items.length>0&&items.length<=30&&Number.isSafeInteger(total)&&total>=0&&items.every(p=>Number.isSafeInteger(p.quantity)&&p.quantity>=1&&p.quantity<=999&&(pending||p.quantity<=data.products.find(x=>x.id===p.product_id)?.max_quantity));
 async function signIn(e){e.preventDefault();if(running.current)return;running.current=true;setBusy(true);setError('');try{await api.signIn(email,password);setPassword('');source.refresh();}catch(e){if(live.current)setError(e.message||'Sign in could not be completed.');}finally{running.current=false;if(live.current)setBusy(false);}}
 async function place(e){e.preventDefault();if(running.current||!valid||!source.user)return;running.current=true;setBusy(true);setError('');let request;
  try{const before=await api.getSession();if(before?.user?.id!==source.user.id)throw Error('Your account changed. Sign in again.');request=window.BOOKUNO_GROUP_REQUESTS.claim(scope,pending?.payload||{business_id:business.id,expected_actor_id:source.user.id,items});const saved=await api.placeShopOrder(request);if(saved?.id!==request.request_id||saved?.business_id!==business.id)throw Error('Your order could not be verified. Retry the same request.');const target=H.url(saved),after=await api.getSession();if(!live.current||after?.user?.id!==source.user.id)return;window.BOOKUNO_GROUP_REQUESTS.finish(scope,request.request_id);location.assign(target);
  }catch(e){if(e.shopRejected&&request)window.BOOKUNO_GROUP_REQUESTS.finish(scope,request.request_id);if(live.current){setError(e.message||'Your order could not be confirmed. Retry the same request.');source.refresh();}}
  finally{running.current=false;if(live.current)setBusy(false);}
 }
 return <section id="sf-products" className="public-shop" tabIndex={-1} aria-labelledby="public-shop-title" data-frame-control="true">
  <p className="public-booking-eyebrow">{uiText("Shop with ")}{business.name}</p><h2 id="public-shop-title">{uiText("Choose something good.")}</h2><p>{uiText("Order online for collection from ")}{business.name}{uiText(". Shipping is not included.")}</p>
  {pending&&<p className="orders-notice">{uiText("Your previous order needs checking. Its details are preserved; retry to recover the saved result.")}</p>}
  <form onSubmit={place} className="public-shop-layout">
   <div className="public-shop-grid">{data.products.map(p=><article className="public-shop-product" key={p.id}><h3>{p.name}</h3>{p.variant&&<p>{p.variant}</p>}<strong>{H.money(p.unit_price_cents,p.currency)}</strong><label>{uiText("Quantity for ")}{p.name}<input type="number" min={0} max={p.max_quantity} step={1} inputMode="numeric" value={pending?pending.payload.items.find(x=>x.product_id===p.id)?.quantity||0:quantities[p.id]||0} disabled={busy||!!pending||p.max_quantity===0} onChange={e=>setQuantities(v=>({...v,[p.id]:e.target.value}))}/></label>{p.max_quantity===0&&<p>{uiText("Currently out of stock.")}</p>}</article>)}{!data.products.length&&!pending&&<p>{uiText("No products are available to order right now.")}</p>}{data.has_more&&<p>{uiText("Showing the first 100 products. Contact the business for its full range.")}</p>}</div>
   <aside className="public-shop-review" aria-label={uiText("Review your order")}><h3>{uiText("Your order")}</h3>{items.map(item=>{const p=data.products.find(p=>p.id===item.product_id);return <div className="public-shop-line" key={item.product_id}><span>{item.quantity} × {p?.name||uiText("Saved product")}</span><strong>{H.money(item.quantity*item.expected_unit_price_cents,data.currency)}</strong></div>;})}<div className="public-shop-line public-shop-total"><span>{uiText("Total")}</span><strong>{H.money(total,data.currency)}</strong></div><p>{uiText("For collection. Prices and stock are checked when the order is saved.")}</p>{source.user?<><p>{uiText("Ordering with your signed-in Bookuno account.")}</p><button type="submit" className="btn primary" disabled={busy||!valid}>{busy?uiText("Saving your order…"):pending?uiText("Check previous order"):uiText("Save order & continue")}</button><p>{uiText("Your order is saved before secure checkout opens. A payment failure leaves it unpaid and available to retry.")}</p></>:<p>{uiText("Choose your products, then sign in below to keep your order connected to you.")}</p>}{error&&<p role="alert" className="orders-error">{uiText(error)}</p>}</aside>
  </form>
  {!source.user&&<form className="public-shop-signin" onSubmit={signIn}><h3>{uiText("Sign in to checkout")}</h3><label>{uiText("Email")}<input type="email" autoComplete="email" required value={email} disabled={busy} onChange={e=>setEmail(e.target.value)}/></label><label>{uiText("Password")}<input type="password" autoComplete="current-password" required value={password} disabled={busy} onChange={e=>setPassword(e.target.value)}/></label><button type="submit" className="btn primary" disabled={busy}>{busy?uiText("Signing in…"):uiText("Sign in")}</button><a href="app.html#/auth?type=customer&mode=register">{uiText("Create a Bookuno account")}</a>{error&&<p role="alert">{uiText(error)}</p>}</form>}
 </section>;
}
function OnlineShopSettings({business,actor}){
 const [data,setData]=React.useState(null),[error,setError]=React.useState(''),[busy,setBusy]=React.useState(false);const live=React.useRef(false);
 React.useEffect(()=>{live.current=true;if(window.BOOKUNO_CONFIG?.customerJourney===true)window.BOOKUNO_API.getShopSettings(business).then(d=>{window.BOOKUNO_SHOP.settings(d,business);if(live.current)setData(d);}).catch(()=>{if(live.current)setError("Online shop settings could not be loaded.");});return()=>{live.current=false;};},[business,actor]);
 if(window.BOOKUNO_CONFIG?.customerJourney!==true)return null;
 async function toggle(){if(busy||!data)return;setBusy(true);setError('');try{const session=await window.BOOKUNO_API.getSession();if(session?.user?.id!==actor)throw Error('Your account changed.');const next=await window.BOOKUNO_API.publishShop({business_id:business,expected_actor_id:actor,enabled:!data.enabled});window.BOOKUNO_SHOP.settings(next,business);if(live.current)setData(next);}catch(e){if(live.current)setError(e.message||'The setting could not be saved.');}finally{if(live.current)setBusy(false);}}
 return <section className="online-shop-settings" aria-label={uiText("Online shop")}><div><strong>{uiText("Online shop")}</strong><p>{data?(data.enabled?uiText("Customers can order your active products for collection."):uiText("Your catalogue stays private until you enable online ordering.")):uiText("Loading online shop settings…")}</p></div>{data&&<><button className="btn outline" type="button" disabled={busy||!data.public_page_enabled&&!data.enabled} onClick={toggle}>{busy?uiText("Saving…"):data.enabled?uiText("Pause online ordering"):uiText("Enable online ordering")}</button>{data.enabled&&<a className="btn outline" href={'../app.html#/b/'+encodeURIComponent(data.slug)} target="_blank" rel="noopener noreferrer">{uiText("View online shop ↗")}</a>}{!data.public_page_enabled&&<p>{uiText("Enable your public business page first.")}</p>}</>}{error&&<p role="alert">{uiText(error)}</p>}</section>;
}
