import{getContext as t,store as e,withSyncEvent as a}from"@wordpress/interactivity";const n=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:r}=e("cfa-location-store",{state:{get buttonAriaLabel(){const e=t();return e.ariaLabels[e.activeState]},get buttonLabel(){const e=t();return e.labels[e.activeState]},get favoriteIconClass(){return"favorite"===t().activeState?"favorite":""},get isSignedIn(){return!!document.cookie.match("cfa_oauth2_signed_in")},get signInDialog(){return r.isSignedIn?"":"sign-in-dialog"},get unfavoriteIconClass(){return"unfavorite"===t().activeState?"unfavorite":""}},actions:{*getFavorites(){const e=t();if(r.isSignedIn)try{const{success:t,data:a}=yield n(e.ajaxURL,"cfa_connector_get_restaurants_by_customer",{nonce:e.restaurantsNonce,query_type:"favorite"});if(!t)return;e.favoriteRestaurantIds=a?.map(t=>t.number)||[],e.favoriteRestaurantIds.includes(e.locationNumber)&&(e.activeState="unfavorite")}catch(t){console.error("Error loading favorite restaurants:",t)}finally{e.isLoading=!1}else e.isLoading=!1},updateFavorites:a(function*(){if(!r.isSignedIn)return;const e=t();if(e.isUpdating)return;e.isUpdating=!0;const a={nonce:e.nonce,active_state:e.activeState,location_number:e.locationNumber};yield n(e.ajaxURL,"cfa_connector_update_favorite_restaurants",a),"favorite"===e.activeState?e.activeState="unfavorite":e.activeState="favorite",e.isUpdating=!1})}});