import{A as b,a as u,b as h,c as w,d as y,e as m,f as S,g,h as A}from"./contracts.generated-BALdEKZ5.js";import{z as r}from"./jwt-decode.esm-CZajAnkk.js";(function(){try{var o=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},t=new Error().stack;t&&(o._sentryDebugIds=o._sentryDebugIds||{},o._sentryDebugIds[t]="1e509ae9-5170-4274-a08f-f02f4e42bfdf",o._sentryDebugIdIdentifier="sentry-dbid-1e509ae9-5170-4274-a08f-f02f4e42bfdf")}catch{}})();const $=r.object({valid:r.boolean(),error:r.string().nullable()}),E=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 j{async list(t,e){const a=new URLSearchParams;t&&a.append("path",t),e&&a.append("mode",e);const i=await(await fetch(`/_editor/api/codebase/files?${a.toString()}`)).json();return b.parse(i)}async create(t,e,a,s){const i=new URLSearchParams;a&&i.append("overwrite","true");const c=i.toString(),p=c?`/_editor/api/codebase/files/${t}?${c}`:`/_editor/api/codebase/files/${t}`,l={};s!=null&&(l["X-Expected-Mtime"]=String(s));const n=await fetch(p,{method:"POST",headers:l,body:e?new Uint8Array(e).buffer:void 0});if(n.status===412){let d={};try{d=await n.json()}catch{}throw new C(t,d.currentMtime,d.expectedMtime)}if(!n.ok){const d=await n.text();throw new Error(`Failed to create file: ${d} (status: ${n.status})`)}const f=await n.json();return u.parse(f)}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 h.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 w.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=y.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.ok){const c=await s.text();throw new Error(`Failed to rename file: ${c} (status: ${s.status})`)}const i=await s.json();return m.parse(i)}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 S.parse(a)}async settings(){const e=await(await fetch("/_editor/api/codebase/settings")).json();return g.parse(e)}async typeCheck(t){const a=await(await fetch(`/_editor/api/codebase/type-check/${t}`,{method:"POST"})).json();return A.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 $.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 E.parse(a)}catch{return{valid:!0,error:null,missing_packages:null}}}}export{j as C,C as S};
//# sourceMappingURL=files-D9_9W1Hf.js.map
