const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./toast-3TZtEphK.js","./toast-C6WyPqyA.js","./preact.module-CNakwbc9.js","./rolldown-runtime-DAXXjFlN.js","./hooks.module-BrXTvcfS.js","./i18n-BaLR3UdQ.js","./src-UeATgcQ9.js","./preload-helper-HclGiUj8.js","./task_states-40T2foIO.js","./dayjs.min-yZk4pRyU.js","./isoWeek-DSe7EiHz.js"])))=>i.map(i=>d[i]);
import{t as e}from"./preload-helper-HclGiUj8.js";import{M as t,U as n,a as r}from"./i18n-BaLR3UdQ.js";var i=class{constructor(e){for(let t in e)this[t]=e[t]}};async function a(e){if(t)return{};let r=glob.appContext?.tabManager?glob.appContext.tabManager.getActiveContext():null,i={"trilium-component-id":glob.componentId,"trilium-local-now-datetime":n.localNowDateTime(),"trilium-hoisted-note-id":r?r.hoistedNoteId:null,"x-csrf-token":glob.csrfToken};for(let t in e)e[t]&&(i[t]=e[t]);return i}async function o(e,t){return await v(`GET`,e,t,{silentNotFound:!0})}async function s(e,t,n){return await v(`GET`,e,t,{raw:n})}async function c(e,t,n){return await v(`POST`,e,n,{data:t})}async function l(e,t,n){return await v(`POST`,e,n,{data:t,silentInternalServerError:!0})}async function u(e,t,n){return await v(`PUT`,e,n,{data:t})}async function d(e,t,n){return await v(`PATCH`,e,n,{data:t})}async function f(e,t){return await v(`DELETE`,e,t)}async function p(e,t,n,r=`PUT`){let i=new FormData;i.append(`upload`,t);let o=async()=>$.ajax({url:window.glob.baseApiUrl+e,headers:await a(n?{"trilium-component-id":n}:void 0),data:i,type:r,timeout:3600*1e3,contentType:!1,processData:!1});try{return await o()}catch(e){let t=e;if(t?.status&&_(t.status,t.responseText))return await g(),await o();throw e}}var m=0,h=null;async function g(){return h||(h=(async()=>{try{let e=await fetch(`./bootstrap${window.location.search}`,{cache:`no-store`});if(e.ok){let t=await e.json();glob.csrfToken=t.csrfToken}}finally{h=null}})(),h)}function _(e,t){if(e!==403)return!1;try{return JSON.parse(t).message===`Invalid CSRF token`}catch{return!1}}async function v(e,t,n,r={}){let i=await a({"trilium-component-id":n}),{data:o}=r,s=await y(t,e,o,i,r),c=s.headers[`trilium-max-entity-change-id`];return c&&c.trim()&&(m=Math.max(m,parseInt(c))),s.body}function y(e,t,n,r,i){return new Promise((o,s)=>{let c={url:window.glob.baseApiUrl+e,type:t,headers:r,timeout:6e4,success:(e,t,n)=>{let r={};n.getAllResponseHeaders().trim().split(/[\r\n]+/).forEach(e=>{let t=e.split(`: `),n=t.shift();n&&(r[n]=t.join(`: `))}),o({body:e,headers:r})},error:async c=>{if(c.status===0){s(`rejected by browser`);return}if(!i.csrfRetried&&_(c.status,c.responseText))try{await g(),o(await y(e,t,n,await a({"trilium-component-id":r[`trilium-component-id`]}),{...i,csrfRetried:!0}));return}catch(e){s(e);return}if(!(i.silentNotFound&&c.status===404)&&!(i.silentInternalServerError&&c.status===500))try{await b(t,e,c.status,c.responseText)}catch{}s(c.responseText)}};if(i.raw&&(c.dataType=`text`),n){try{c.data=JSON.stringify(n)}catch(e){console.log(`Can't stringify data: `,n,` because of error: `,e)}c.contentType=`application/json`}$.ajax(c)})}async function b(t,n,a,o){let s=o;if(typeof o==`string`)try{o=JSON.parse(o),s=o.message}catch{}let c=(await e(async()=>{let{default:e}=await import(`./toast-3TZtEphK.js`);return{default:e}},__vite__mapDeps([0,1,2,3,4,5,6,7,8,9,10]),import.meta.url)).default,l=(typeof s==`string`?s:JSON.stringify(s))||`-`;if([400,404].includes(a)&&o&&typeof o==`object`)throw c.showError(l),new i({requestUrl:n,method:t,statusCode:a,...o});a===400&&(n.includes(`%23`)||n.includes(`%2F`))?c.showPersistent({id:`trafik-blocked`,icon:`bx bx-unlink`,title:r(`server.unknown_http_error_title`),message:r(`server.traefik_blocks_requests`)}):c.showErrorTitleAndMessage(r(`server.unknown_http_error_title`),r(`server.unknown_http_error_content`,{statusCode:a,method:t,url:n,message:l}),15e3),window.logError(`${a} ${t} ${n} - ${s}`)}var x={get:s,getWithSilentNotFound:o,post:c,postWithSilentInternalServerError:l,put:u,patch:d,remove:f,upload:p,getHeaders:a,getMaxKnownEntityChangeId:()=>m};export{x as t};