import{t as e}from"./axios-m7YkukvY.js";import{t}from"./i18n-jmtZ2Bqk.js";import{a as n,c as r,i,n as a,o}from"./syncService-CQzggLWC.js";var s={"/branches":`branches`,"/projects":`projects`,"/safety-incidents":`safety_incidents`,"/purchase-requests":`purchase_requests`,"/site-invoices":`site_invoices`,"/expenses":`expenses`,"/receipts":`receipts`,"/invoices":`invoices`,"/workers":`workers`,"/assets":`assets`,"/suppliers":`suppliers`,"/purchase-orders":`purchase_orders`,"/parties":`parties`,"/contracts":`contracts`};function c(e=``){let t=e.replace(/^\/api/,``).split(`?`)[0];for(let[e,n]of Object.entries(s))if(t.startsWith(e))return n;return null}function l(){try{return JSON.parse(localStorage.getItem(`user`)||`{}`).current_company||null}catch{return null}}async function u(e,t){let n=t.match(/\/(\d+)$/);return n?i[e]?.where(`id`).equals(Number(n[1])).first().catch(()=>null):null}var d={async get(t,n={}){if(a.isOnline)return e.get(t,n);let r=c(t);return r?{data:await o(r,l()),status:200,offline:!0}:e.get(t,n)},async post(t,o={},s={}){if(a.isOnline)return e.post(t,o,s);let l=c(t);if(l){let e=r(),t={...o,uuid:e,id:null,revision:1,_synced:!1,_dirty:!0,created_at:new Date().toISOString(),updated_at:new Date().toISOString()};return await i[l]?.put(t).catch(()=>{}),await n(l,`create`,o,e),{data:t,status:201,offline:!0}}return e.post(t,o,s)},async put(t,r={},o={}){if(a.isOnline)return e.put(t,r,o);let s=c(t),l=s?await u(s,t):null;return s&&l?(await i[s].put({...l,...r,_dirty:!0,_synced:!1,updated_at:new Date().toISOString()}).catch(()=>{}),await n(s,`update`,r,l.uuid,l.revision??null),{data:{...l,...r},status:200,offline:!0}):e.put(t,r,o)},async delete(r,o={}){if(a.isOnline)return e.delete(r,o);let s=c(r),l=s?await u(s,r):null;return s&&l?(await i[s].delete(l.uuid).catch(()=>{}),await n(s,`delete`,{},l.uuid,l.revision??null),{data:{message:t.t(`QueuedForDeletion`)},status:200,offline:!0}):e.delete(r,o)}};export{d as t};