(()=>{"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 r=JSON.stringify({endpointWithFirstSlash:t,params:e,method:a});if(this._activeRequests.has(r))return this._activeRequests.get(r);let n=(null!==(s=window.MPHB._data.restApiUrl)&&void 0!==s?s:"")+t;const d={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();n+=(n.includes("?")?"&":"?")+t}}else d.body=JSON.stringify(e);const o=(async()=>{try{const t=await fetch(n,d),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(r)}})();return this._activeRequests.set(r,o),o}_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([t,e,a,s]),r=this._getCachedData(i);return null!=r?r:{}}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([s.room_type_ids,s.is_add_prices,s.is_truncate_prices,s.is_add_prices_currency]),r=this._getCachedData(i);let n=new Date(s.start_date.getTime()),d=new Date(s.end_date.getTime());if(r){var o;let t=this.getFormattedDate(n),e=this.getFormattedDate(d);const a=null!==(o=s.room_type_ids[0])&&void 0!==o?o:0;if(void 0!==r[a][t]&&void 0!==r[a][e])return r;for(;n.getTime()<d.getTime()&&void 0!==r[a][t];)n.setDate(n.getDate()+1),t=this.getFormattedDate(n)}const{start_date:c,end_date:_,...h}=s;e();const g=await this._getJsonResponse("/availability",{start_date:this.getFormattedDate(n),end_date:this.getFormattedDate(d),...h}),l={...r};for(const[t,e]of Object.entries(g)){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}}})();