import{getContext as t,store as e}from"@wordpress/interactivity";const a=async(t,e,a={})=>{const n=new URLSearchParams({action:e,...a});try{const e=await fetch(t,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:n});return await e.json()}catch(t){return console.error(`Error in AJAX call ${e}:`,t),{success:!1,data:`Error: ${t}`}}},{state:n}=e("cfa-restaurant-list-store",{state:{get ariaLabel(){return`${t().restaurant.name} ${n.detailsLabel}`},get isContainerVisible(){const e=t();return n.isSignedIn&&e.isLoading||!e.isLoading&&!!e.restaurants.length},get isSubtitleVisible(){const e=t();return!e.isLoading&&!!e.restaurants.length},get isSignedIn(){return!!document.cookie.match("cfa_oauth2_signed_in")}},actions:{*loadFavoriteRestaurants(){const e=t();if(n.isSignedIn&&!e.isLoading){e.isLoading=!0;try{const[{success:t,data:n},{success:s,data:r}]=yield Promise.all([a(e.ajaxURL,"cfa_connector_get_restaurants_by_customer",{nonce:e.nonce,query_type:"favorite"}),a(e.ajaxURL,"cfa_connector_get_restaurants_by_customer",{nonce:e.nonce,query_type:"recent"})]);if(!t&&!s)return;let o=[];n.length?(o=n,e.subtitle=e.subtitleLabels.favorite):(o=r,e.subtitle=e.subtitleLabels.recent),yield Promise.all(o.map(async(t,n)=>{const s=await a(e.ajaxURL,"cfa_core_get_open_status",{id:t.id,nonce:e.nonce});o[n]={...o[n],openStatus:s?.data||""}})),e.restaurants=o}catch(t){console.error("Error loading favorite restaurants:",t)}finally{e.isLoading=!1}}}}});