import{A as f,a as h,b as w,c as y,d as m,e as g,f as A,g as E,h as S}from"./contracts.generated-CKCU3GwF.js";import{z as r}from"./jwt-decode.esm-Bw7_fA5B.js";(function(){try{var i=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},t=new Error().stack;t&&(i._sentryDebugIds=i._sentryDebugIds||{},i._sentryDebugIds[t]="7e2497bc-dfbf-4e52-9189-892a6b9042d5",i._sentryDebugIdIdentifier="sentry-dbid-7e2497bc-dfbf-4e52-9189-892a6b9042d5")}catch{}})();class $ extends Error{constructor(t){super(`File already exists: ${t}`),this.filename=t,this.name="FileAlreadyExistsError"}}const j=r.object({valid:r.boolean(),error:r.string().nullable()}),x=r.object({valid:r.boolean(),error:r.string().nullable(),missing_packages:r.array(r.string()).nullable()});class C extends Error{constructor(t,e,a){super(`File modified externally since last read: ${t}`),this.filePath=t,this.currentMtime=e,this.expectedMtime=a,this.name="StaleFileError"}}class _{async list(t,e){const a=new URLSearchParams;t&&a.append("path",t),e&&a.append("mode",e);const o=await(await fetch(`/_editor/api/codebase/files?${a.toString()}`)).json();return f.parse(o)}async create(t,e,a,s,o){const n=new URLSearchParams;a&&n.append("overwrite","true");const p=n.toString(),b=p?`/_editor/api/codebase/files/${t}?${p}`:`/_editor/api/codebase/files/${t}`,l={};s!=null&&(l["X-Expected-Mtime"]=String(s)),o?.userMessageId&&(l["X-Abstra-User-Message-Id"]=o.userMessageId);const d=await fetch(b,{method:"POST",headers:l,body:e?new Uint8Array(e).buffer:void 0});if(d.status===412){let c={};try{c=await d.json()}catch{}throw new C(t,c.currentMtime,c.expectedMtime)}if(!d.ok){const c=await d.text();throw new Error(`Failed to create file: ${c} (status: ${d.status})`)}const u=await d.json();return h.parse(u)}async delete(t){const e=await fetch(`/_editor/api/codebase/files/${t}`,{method:"DELETE",headers:{"Content-Type":"application/json"}});if(!e.ok){const s=await e.text();throw new Error(`Failed to delete file: ${s} (status: ${e.status})`)}const a=await e.json();return w.parse(a)}async edit(t,e){const s=await(await fetch(`/_editor/api/codebase/files/${t}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:e})})).json();return y.parse(s)}async retrieve(t){const e=await fetch(`/_editor/api/codebase/files/${t}`);if(!e.ok)throw new Error(`Failed to retrieve file: ${e.status}`);const a=await e.arrayBuffer();return new Uint8Array(a)}async downloadFolder(t){const e=await fetch(`/_editor/api/codebase/files-zip/${t}`);if(!e.ok){const a=await e.text();throw new Error(`Failed to download folder: ${a} (status: ${e.status})`)}return await e.blob()}async rename(t,e){const a=m.parse({pathParts:t,newPathParts:e}),s=await fetch("/_editor/api/codebase/files",{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)});if(s.status===409){const n=e[e.length-1]??e.join("/");throw new $(n)}if(!s.ok){const n=await s.text();throw new Error(`Failed to rename file: ${n} (status: ${s.status})`)}const o=await s.json();return g.parse(o)}async mkdir(t){const e=await fetch(`/_editor/api/codebase/dir/${t}`,{method:"POST",headers:{"Content-Type":"application/json"}});if(!e.ok){const s=await e.text();throw new Error(`Failed to create directory: ${s} (status: ${e.status})`)}const a=await e.json();return A.parse(a)}async settings(){const e=await(await fetch("/_editor/api/codebase/settings")).json();return E.parse(e)}async typeCheck(t){const a=await(await fetch(`/_editor/api/codebase/type-check/${t}`,{method:"POST"})).json();return S.parse(a)}async validateAbstraJson(t){try{const e=await fetch("/_editor/api/codebase/validate-abstra-json",{method:"POST",headers:{"Content-Type":"application/json"},body:t});if(!e.ok)return{valid:!1,error:`Validation request failed: ${e.status}`};const a=await e.json();return j.parse(a)}catch{return{valid:!0,error:null}}}async validateRequirements(t){try{const e=await fetch("/_editor/api/codebase/validate-requirements",{method:"POST",headers:{"Content-Type":"text/plain"},body:t});if(!e.ok)return{valid:!1,error:`Validation request failed: ${e.status}`,missing_packages:null};const a=await e.json();return x.parse(a)}catch{return{valid:!0,error:null,missing_packages:null}}}}export{_ as C,C as S};
//# sourceMappingURL=files-Cr-2y8jQ.js.map
