import{A as w,a as y,b as m,c as $,d as g,e as E,f as A,h as S,i as x}from"./codebaseEvents-s4M7UMV6.js";import{z as n}from"./jwt-decode.esm-CX61wVx3.js";import{i as C}from"./externalPlayer-D7i6YlKj.js";(function(){try{var r=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},t=new Error().stack;t&&(r._sentryDebugIds=r._sentryDebugIds||{},r._sentryDebugIds[t]="25fd953c-03ee-414e-8f27-7d41e401eb9b",r._sentryDebugIdIdentifier="sentry-dbid-25fd953c-03ee-414e-8f27-7d41e401eb9b")}catch{}})();const i=()=>C()?"/_web-editor/api/codebase":"/_editor/api/codebase";class j extends Error{constructor(t){super(`File already exists: ${t}`),this.filename=t,this.name="FileAlreadyExistsError"}}const T=n.object({valid:n.boolean(),error:n.string().nullable()}),F=n.object({valid:n.boolean(),error:n.string().nullable(),missing_packages:n.array(n.string()).nullable()});class k extends Error{constructor(t,e,s){super(`File modified externally since last read: ${t}`),this.filePath=t,this.currentMtime=e,this.expectedMtime=s,this.name="StaleFileError"}}class v extends Error{constructor(t){super(t),this.message=t,this.name="PathConflictError"}}async function L(r){if(r.status!==409)return;let t="This path is already in use.";try{const e=await r.json();if(typeof e=="object"&&e!==null&&"message"in e){const s=e.message;typeof s=="string"&&(t=s)}}catch{}throw new v(t)}class I{async list(t,e){const s=new URLSearchParams;t&&s.append("path",t),e&&s.append("mode",e);const a=e==="module"?"/_editor/api/codebase":i(),o=await(await fetch(`${a}/files?${s.toString()}`)).json();return w.parse(o)}async create(t,e,s,a,d){const o=new URLSearchParams;s&&o.append("overwrite","true");const p=o.toString(),u=p?`${i()}/files/${t}?${p}`:`${i()}/files/${t}`,f={};a!=null&&(f["X-Expected-Mtime"]=String(a)),d?.userMessageId&&(f["X-Abstra-User-Message-Id"]=d.userMessageId);const b=e?e instanceof Blob?e:new Uint8Array(e).buffer:void 0,c=await fetch(u,{method:"POST",headers:f,body:b});if(c.status===412){let l={};try{l=await c.json()}catch{}throw new k(t,l.currentMtime,l.expectedMtime)}if(!c.ok){const l=await c.text();throw new Error(`Failed to create file: ${l} (status: ${c.status})`)}const h=await c.json();return y.parse(h)}async delete(t){const e=await fetch(`${i()}/files/${t}`,{method:"DELETE",headers:{"Content-Type":"application/json"}});if(!e.ok){const a=await e.text();throw new Error(`Failed to delete file: ${a} (status: ${e.status})`)}const s=await e.json();return m.parse(s)}async edit(t,e){const s=await fetch(`${i()}/files/${t}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:e})});if(!s.ok){const d=await s.text();throw new Error(`Failed to edit file: ${d} (status: ${s.status})`)}const a=await s.json();return $.parse(a)}async retrieve(t){const e=await fetch(`${i()}/files/${t}`);if(!e.ok)throw new Error(`Failed to retrieve file: ${e.status}`);const s=await e.arrayBuffer();return new Uint8Array(s)}async downloadFolder(t){const e=await fetch(`/_editor/api/codebase/files-zip/${t}`);if(!e.ok){const s=await e.text();throw new Error(`Failed to download folder: ${s} (status: ${e.status})`)}return await e.blob()}async rename(t,e){const s=g.parse({pathParts:t,newPathParts:e}),a=await fetch(`${i()}/files`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify(s)});if(a.status===409){const o=e[e.length-1]??e.join("/");throw new j(o)}if(!a.ok){const o=await a.text();throw new Error(`Failed to rename file: ${o} (status: ${a.status})`)}const d=await a.json();return E.parse(d)}async mkdir(t){const e=await fetch(`${i()}/dir/${t}`,{method:"POST",headers:{"Content-Type":"application/json"}});if(!e.ok){const a=await e.text();throw new Error(`Failed to create directory: ${a} (status: ${e.status})`)}const s=await e.json();return A.parse(s)}async settings(){const e=await(await fetch("/_editor/api/codebase/settings")).json();return S.parse(e)}async typeCheck(t){const s=await(await fetch(`/_editor/api/codebase/type-check/${t}`,{method:"POST"})).json();return x.parse(s)}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 s=await e.json();return T.parse(s)}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 s=await e.json();return F.parse(s)}catch{return{valid:!0,error:null,missing_packages:null}}}}export{I as C,v as P,k as S,L as t};
//# sourceMappingURL=files-IqWfdQBD.js.map
