(()=>{"use strict";window.MPHB=window.MPHB||{},window.MPHB.restApiHelper=new class{_activeRequests=new Map;_cachedData=new Map;async _getJsonResponse(t,e={},a="GET"){var s,i;const n=JSON.stringify({endpointWithFirstSlash:t,params:e,method:a});if(this._activeRequests.has(n))return this._activeRequests.get(n);let r=(null!==(s=window.MPHB._data.restApiUrl)&&void 0!==s?s:"")+t;const o={method:a,headers:{"X-WP-Nonce":null!==(i=window.MPHB._data.restApiWpNonce)&&void 0!==i?i:"","Content-Type":"application/json"}};if("GET"===a){if(e&&Object.keys(e).length>0){const t=new URLSearchParams(e).toString();r+=(r.includes("?")?"&":"?")+t}}else o.body=JSON.stringify(e);const d=(async()=>{try{const t=await fetch(r,o),e=await t.json();if(!t.ok){const a=new Error(e?.message||`REST API error: ${t.status} ${t.statusText}`);throw a.status=t.status,a.code=e?.code,a.data=e?.data,a}return e}finally{this._activeRequests.delete(n)}})();return this._activeRequests.set(n,d),d}_getCachedData(t){return this._cachedData.get(t)}_setCachedData(t,e){this._cachedData.set(t,e)}getFormattedDate(t){return`${t.getFullYear()}-${String(t.getMonth()+1).padStart(2,"0")}-${String(t.getDate()).padStart(2,"0")}`}getCachedAvailabilityData(t=[],e=!1,a=!1,s=!1){const i=JSON.stringify(["getAvailabilityData",t,e,a,s]),n=this._getCachedData(i);return null!=n?n:{}}async getAvailabilityData(t,e=()=>{},a=()=>{}){const s={start_date:new Date,end_date:new Date,room_type_ids:[],is_add_prices:!1,is_truncate_prices:!1,is_add_prices_currency:!1,is_request_for_admin_area:window.MPHB._data.isAdmin,...t},i=JSON.stringify(["getAvailabilityData",s.room_type_ids,s.is_add_prices,s.is_truncate_prices,s.is_add_prices_currency]),n=this._getCachedData(i);let r=new Date(s.start_date.getTime()),o=new Date(s.end_date.getTime());if(n){var d;let t=this.getFormattedDate(r),e=this.getFormattedDate(o);const a=null!==(d=s.room_type_ids[0])&&void 0!==d?d:0;if(void 0!==n[a][t]&&void 0!==n[a][e])return n;for(;r.getTime()<o.getTime()&&void 0!==n[a][t];)r.setDate(r.getDate()+1),t=this.getFormattedDate(r)}const{start_date:c,end_date:_,...g}=s;e();const h=await this._getJsonResponse("/availability",{start_date:this.getFormattedDate(r),end_date:this.getFormattedDate(o),...g}),l={...n};for(const[t,e]of Object.entries(h)){l[t]||(l[t]={});for(const[a,s]of Object.entries(e))l[t][a]={...l[t][a]||{},...s}}return this._setCachedData(i,l),a(),l}async getCountries(t="en_US"){const e=JSON.stringify(["getCountries",t]),a=this._getCachedData(e);if(a)return a;const s=await this._getJsonResponse("/countries",{locale:t});return this._setCachedData(e,s),s}async getGoogleHotelsData(){return await this._getJsonResponse("/google-hotels-data",{})}async updateGoogleHotelsData(t){return await this._getJsonResponse("/google-hotels-data",t,"POST")}}})();