import{B as f,G as v,K as d,N as c,S as o,i as y,j as m,oc as p,pc as S,qc as T,tc as b,uc as h,v as a}from"./chunk-NCBQEKN3.js";var l=class s{constructor(e){this.enc=e;sessionStorage.getItem("token")||this.clear()}NS="gc_";memoria=new Map;copiar(e){try{return typeof structuredClone=="function"?structuredClone(e):JSON.parse(JSON.stringify(e))}catch{return JSON.parse(JSON.stringify(e))}}get(e){let t=this.NS+this.userKey()+e,i=this.memoria.get(t);if(i){if(Date.now()-i.ts>i.ttl){this.memoria.delete(t);try{localStorage.removeItem(t)}catch{}return null}return this.copiar(i.data)}try{let r=localStorage.getItem(t);if(!r)return null;let n=this.enc.decrypt(r);return!n||Date.now()-n.ts>n.ttl?(localStorage.removeItem(t),null):(this.memoria.set(t,n),this.copiar(n.data))}catch{return null}}set(e,t,i){let r=this.NS+this.userKey()+e,n={data:this.copiar(t),ts:Date.now(),ttl:i};this.memoria.set(r,n);try{localStorage.setItem(r,this.enc.encrypt(n))}catch{}}invalidatePattern(e){let t=this.NS+this.userKey()+e;for(let r of[...this.memoria.keys()])r.startsWith(t)&&this.memoria.delete(r);let i=[];for(let r=0;r<localStorage.length;r++){let n=localStorage.key(r);n?.startsWith(t)&&i.push(n)}i.forEach(r=>localStorage.removeItem(r))}clear(){this.memoria.clear();let e=[];for(let t=0;t<localStorage.length;t++){let i=localStorage.key(t);i?.startsWith(this.NS)&&e.push(i)}e.forEach(t=>localStorage.removeItem(t))}userKeyCache=null;userKey(){let e=(()=>{try{return sessionStorage.getItem("user")}catch{return null}})();if(this.userKeyCache&&this.userKeyCache.raw===e)return this.userKeyCache.valor;let t=this.calcularUserKey(e);return this.userKeyCache={raw:e,valor:t},t}calcularUserKey(e){try{let t=e!==void 0?e:sessionStorage.getItem("user");if(!t)return"anon_";let i=this.enc.decrypt(t)??"anon",r=0;for(let n of i)r=(r<<5)-r+n.charCodeAt(0),r|=0;return(r>>>0).toString(16).slice(0,8)+"_"}catch{return"anon_"}}static \u0275fac=function(t){return new(t||s)(o(h))};static \u0275prov=c({token:s,factory:s.\u0275fac,providedIn:"root"})};var C=class s{constructor(e,t,i){this.http=e;this.encryptionService=t;this.cacheService=i}apiUrl=b.apiUrl;getBearerToken(){let e=sessionStorage.getItem("token");if(!e)return null;try{return this.encryptionService.decrypt(e)}catch{return e}}getHeaders(){let e=new p({"Content-Type":"application/json"}),t=sessionStorage.getItem("token");if(!t)return e;let i="";try{i=this.encryptionService.decrypt(t)}catch{i=t}return i&&(e=e.set("Authorization",`Bearer ${i}`)),e}handleError(e){return m(()=>e)}get(e,t){let i={headers:this.getHeaders(),params:new S({fromObject:t||{}})};return this.http.get(`${this.apiUrl}/${e}`,i).pipe(a(this.handleError))}post(e,t){return e==="login"?this.http.post(`${this.apiUrl}/${e}`,t).pipe(a(this.handleError)):this.http.post(`${this.apiUrl}/${e}`,t,{headers:this.getHeaders()}).pipe(a(this.handleError))}put(e,t){return this.http.put(`${this.apiUrl}/${e}`,t,{headers:this.getHeaders()}).pipe(a(this.handleError))}delete(e){return this.http.delete(`${this.apiUrl}/${e}`,{headers:this.getHeaders()}).pipe(a(this.handleError))}patch(e,t){return this.http.patch(`${this.apiUrl}/${e}`,t,{headers:this.getHeaders()}).pipe(a(this.handleError))}postFile(e,t){let i=new p,r=sessionStorage.getItem("token");if(r)try{let n=this.encryptionService.decrypt(r);n&&(i=i.set("Authorization",`Bearer ${n}`))}catch{}return this.http.post(`${this.apiUrl}/${e}`,t,{headers:i}).pipe(a(this.handleError))}enVuelo=new Map;getCached(e,t,i=5*6e4){let r=this.cacheKey(e,t),n=this.cacheService.get(r);if(n!==null)return y(n);let u=this.enVuelo.get(r);if(u)return u;let g=this.get(e,t).pipe(d(E=>this.cacheService.set(r,E,i)),f(()=>this.enVuelo.delete(r)),v({bufferSize:1,refCount:!1}));return this.enVuelo.set(r,g),g}invalidate(e){this.cacheService.invalidatePattern(e)}clearCache(){this.cacheService.clear()}cacheKey(e,t){let i=e.replace(/[^a-zA-Z0-9]/g,"_"),r=t&&Object.keys(t).length?"_"+JSON.stringify(t).replace(/[^a-zA-Z0-9]/g,""):"";return i+r}static \u0275fac=function(t){return new(t||s)(o(T),o(h),o(l))};static \u0275prov=c({token:s,factory:s.\u0275fac,providedIn:"root"})};export{C as a};
