'use strict'; var react = require('react'); function m(e){if(typeof e!="object"||e===null)return e;if(e instanceof Date)return new Date(e.getTime());if(Array.isArray(e))return e.map(t=>m(t));let o={};for(let t in e)Object.prototype.hasOwnProperty.call(e,t)&&(o[t]=m(e[t]));return o}function h(e,o,t="right"){return t==="left"?U(e,o):j(e,o)}function U(e,o){if(Array.isArray(e)&&Array.isArray(o))return [...e,...o];if(typeof e!="object"||typeof o!="object")return e;let t={...e},n=e,r=o;for(let s in o)Object.prototype.hasOwnProperty.call(o,s)&&(Object.prototype.hasOwnProperty.call(e,s)?e!==null&&o!==null&&(t[s]=U(n[s],r[s])):t[s]=r[s]);return t}function j(e,o){if(Array.isArray(e)&&Array.isArray(o))return [...e,...o];if(typeof e!="object"||typeof o!="object")return o;let t={...o},n=e,r=o;for(let s in e)Object.prototype.hasOwnProperty.call(e,s)&&(Object.prototype.hasOwnProperty.call(o,s)?o!==null&&e!==null&&(t[s]=j(n[s],r[s])):t[s]=n[s]);return t}function w(e){return e?.constructor===Object}function k(e,o,t){if(!w(e)||!o)return t;let n=o.split("."),r=e;for(let s=0;si.toString(16).padStart(2,"0")).join("")}function M(e){return new Promise(o=>setTimeout(o,e))}var b=900,C=250,A=10,N=100,Z=0,v={consoleOptions:{logThrottleCacheHits:!1,logThrottleWarnings:!0,logThrottleWarningsThreshold:A},frequencyOptions:{cacheCleanupIntervalSeconds:b,frequencyStrategy:"memory-cache",frequencyStrategyTTL:C},interpolateUrlOptions:{addUnusedStateToQueryString:!1,discardOrphanedQueryStringPlaceholders:!1,preEncodeQueryStringValuesForKeys:[]},requestOptions:{autoCreateBodyFromState:!0,errorOn404:!1,formEncodingType:"url",method:"GET",mode:"cors",pageToken:"",recordLimit:N,recordSkip:Z,requestType:"json",responseType:"json"},serverModelOptions:{jsonDataDotPath:"",jsonErrorDotPath:"",jsonNextPageTokenDotPath:"nextPageToken",jsonPreviousPageTokenDotPath:"previousPageToken",jsonTotalDotPath:"total",pageTokenQueryParamName:"pageToken",recordLimitQueryParamName:"limit",recordSkipQueryParamName:"skip"}},y=m(v);function ee(e){y=h(v,e);}var S=new Map;function _(e){return `INFO: The endpoint call to ${e} was returned from session cache.`}function te(e){return `INFO: The endpoint call to ${e} was returned from memory cache.`}function ne(e,o){return `INFO: The endpoint call to ${e} was throttled ${o} time(s).`}function oe(e,o){return `WARNING: The endpoint call to ${e} has been throttled ${o} times. Please review your call logic for bugs, especially if being made within a React hook. This warning can be disabled by setting logThrottleWarningsThreshold to 0 in the options.`}var P=new Map;async function H(e,o,t){if(t.frequencyOptions.frequencyStrategy==="memory-cache"&&t.requestOptions.method==="GET"&&S.has(e)){let n=S.get(e);if(n.expiresAt>Date.now())return t.consoleOptions.logThrottleCacheHits&&console.log(te(o.url)),{cachedResult:n.result,needToCallFetch:!1}}if(t.frequencyOptions.frequencyStrategy==="session-cache"&&t.requestOptions.method==="GET"){let n=sessionStorage.getItem(e);if(n){let r=JSON.parse(n);if(r.expiresAt>Date.now())return t.consoleOptions.logThrottleCacheHits&&console.log(_(o.url)),{cachedResult:r.result,needToCallFetch:!1}}}if(t.frequencyOptions.frequencyStrategy!=="none"){if(P.has(e)){let n=P.get(e);if(n!==void 0){let r=n+1;return P.set(e,r),t.consoleOptions.logThrottleWarningsThreshold&&t.consoleOptions.logThrottleWarnings&&r>=t.consoleOptions.logThrottleWarningsThreshold?console.error(oe(o.url,r)):t.consoleOptions.logThrottleCacheHits&&console.log(ne(o.url,r)),{cachedResult:null,needToCallFetch:!1}}}else P.set(e,0);return setTimeout(()=>{P.delete(e);},t.frequencyOptions.frequencyStrategyTTL),await M(t.frequencyOptions.frequencyStrategyTTL),{cachedResult:null,needToCallFetch:!0}}return {cachedResult:null,needToCallFetch:!0}}var F=null;function Q(){clearInterval(F),S.forEach((e,o)=>{e.expiresAt=0;e--){let o=sessionStorage.key(e);JSON.parse(sessionStorage.getItem(o)).expiresAt= 15. Using default value of ${b} instead.`),t.frequencyOptions.cacheCleanupIntervalSeconds=b),t.frequencyOptions?.frequencyStrategyTTL&&t.frequencyOptions.frequencyStrategyTTL<1&&(console.error(`WARNING: frequencyStrategyTTL must be greater than 0. Using default value of ${C} instead.`),t.frequencyOptions.frequencyStrategyTTL=C),t.consoleOptions?.logThrottleWarningsThreshold&&t.consoleOptions.logThrottleWarningsThreshold<1&&(console.error(`WARNING: logThrottleWarningsThreshold must be greater than 0. Using default value of ${A} instead.`),t.consoleOptions.logThrottleWarningsThreshold=A),t.requestOptions?.recordLimit&&t.requestOptions.recordLimit<1&&(console.error(`WARNING: recordLimit must be greater than 0. Using default value of ${N} instead.`),t.requestOptions.recordLimit=N),t}var q=":";function V(e,o={},t={}){if(!w(o))throw new Error("state must be a plain object (e.g. Record)");let r={...{addUnusedStateToQueryString:!1,discardOrphanedQueryStringPlaceholders:!0,preEncodeQueryStringValuesForKeys:[]},...t},s=new URL(e),a=m(o),i=m(o),f=ae(s.pathname,a,i),c=decodeURIComponent(s.searchParams.toString()),T=re(c,a,i,r),d=se(i,r),p=[...T,...d].join("&"),l=`${s.origin}${f}${p?`?${p}`:""}${s.hash}`;return {unmatchedParamState:r.addUnusedStateToQueryString?{}:i,url:l}}function re(e,o,t,n){return e.split("&").reduce((s,a)=>{if(a.includes(q)){let i=a.split("="),f=i[0],c=String(i[1]);c=c.split(",").reduce((l,g)=>{if(g.startsWith(q)){let u=g.split(q)[1];if(!u)return l;let D=o[u];delete t[u];let E=typeof D<"u"?String(D):"";(!n.discardOrphanedQueryStringPlaceholders||E)&&l.push(n.preEncodeQueryStringValuesForKeys?.some(X=>X===u)?encodeURIComponent(E):E);}else l.push(g);return l},[]).join(",");let p=!n.discardOrphanedQueryStringPlaceholders||c?`${f}=${c}`:void 0;p&&s.push(p);}else a&&s.push(a);return s},[])}function se(e,o){return o.addUnusedStateToQueryString?Object.keys(e).filter(n=>typeof e[n]<"u").map(n=>String(e[n]).split(",").reduce((s,a,i)=>(s+=`${i>0?"&":""}${n}=${String(a)}`,s),"")):[]}function ae(e,o,t){return e.split("/").map(r=>{let s=decodeURIComponent(r);if(s.startsWith(q)){let a=s.split(q)[1];if(a){let i=o[a];if(delete t[a],typeof i<"u")return i}}return s}).join("/")}function z(e,o){let t=e.requestOptions?.url||"";if(!t)throw new Error("requestOptions.url is required");if(e.requestOptions?.method==="GET"){let r=new URL(t);e.requestOptions.recordLimit&&e.serverModelOptions?.recordLimitQueryParamName&&r.searchParams.set(e.serverModelOptions.recordLimitQueryParamName,e.requestOptions.recordLimit.toString()),e.requestOptions.recordSkip&&e.serverModelOptions?.recordSkipQueryParamName&&r.searchParams.set(e.serverModelOptions.recordSkipQueryParamName,e.requestOptions.recordSkip.toString()),e.requestOptions.requestType==="json"&&e.requestOptions?.pageToken&&e.serverModelOptions?.pageTokenQueryParamName&&r.searchParams.set(e.serverModelOptions.pageTokenQueryParamName,e.requestOptions.pageToken),t=r.toString();}let n={unmatchedParamState:{},url:t};return o&&(n=V(t,o,e.interpolateUrlOptions)),n}async function J(e,o,t){let n=x(e,o),r=O(e,t),s;r?s=o:O(o,t)?s=t||{}:s=o;let a=z(n,s);if(n.requestOptions.method!=="GET"&&n.requestOptions.method!=="DELETE"&&n.requestOptions.autoCreateBodyFromState&&!n.requestOptions.body&&Object.keys(a.unmatchedParamState).length){let f=R(a.unmatchedParamState);n.requestOptions.body=f;}let i=n.frequencyOptions.frequencyStrategyKey||"";try{i||(i=await I({body:n.requestOptions.body,normUrl:a.url}));let f=await W(n),c=await H(i,a,n);if(!c.needToCallFetch)return c.cachedResult;let T=L(n.requestOptions,"body","method","mode"),d=await fetch(a.url,{...T,cache:"no-store",headers:f});return await G(d,i,n)}catch(f){return {error:`The endpoint call to ${a.url} failed: ${f}`}}}function B(e){return e instanceof Error?e:new Error(R(e))}var ie="appendData is only supported for GET requests",pe="appendData is only supported for array data and result was not an array";function st(e,o,t){let[n,r]=react.useState(),[s,a]=react.useState(!1),i=react.useCallback(async(c,T)=>{a(!0);let d=typeof o=="boolean"?o:t;try{let p=x(e,o),l=c,g=T;if(l?.body||l?.pageToken||l?.recordSkip||l?.signal?p.requestOptions=h(p.requestOptions,l):g=c,d&&p?.requestOptions?.method&&p.requestOptions.method!=="GET")throw new Error(ie);if(n?.data&&d&&n&&n?.data&&(n.nextPageToken&&(p.requestOptions.pageToken=n?.nextPageToken),n.total&&Array.isArray(n.data)&&(p.requestOptions.recordSkip=n.data.length),n.total&&p.requestOptions.recordSkip&&Array.isArray(n.data)&&n.data.length>=n.total||!p.requestOptions.recordSkip&&!n?.nextPageToken))return;let u=await J(p,g);if(u?.error){r(d?D=>h(D,u):u);return}if(!d||d&&!Array.isArray(n?.data)){if(d&&u?.data&&!Array.isArray(u?.data))throw new Error(pe);r(u);return}u?.data&&r(D=>{let E=m(D);return delete E?.nextPageToken,delete E?.total,E={...E,data:h(E?.data,u.data),error:u.error},u.nextPageToken&&(E.nextPageToken=u.nextPageToken),u.total&&(E.total=u.total),E});}catch(p){let l=B(p);r(d?g=>({...g,error:l.message}):{error:l.message});}finally{a(!1);}},[t,o,n,e]),f=react.useCallback(()=>new Promise(c=>{r(void 0),c();}),[]);return [i,n,s,f]} exports.INVALID_METHOD_FOR_APPEND_DATA = ie; exports.NON_ARRAY_APPEND_DATA = pe; exports.callEndpoint = J; exports.setOptionsDefaults = ee; exports.useCallEndpoint = st;