import{u as k,a as j}from"./vendor-fflate-C7GOKjkv.js";const U=20*1024*1024,N=100*1024*1024,R=25*1024*1024,X=8*1024*1024,W=5e3,G=50,V=20*1024*1024,H=40*1024*1024,_=5e5,w=100,D=`

[Document text truncated by OpenChamber]
`,q=new Set(["docx","pptx","xlsx","odt","odp","ods"]),O=new Map([["png","image/png"],["jpg","image/jpeg"],["jpeg","image/jpeg"],["gif","image/gif"],["webp","image/webp"]]),C=e=>{const n=e.lastIndexOf(".");return n===-1?"":e.slice(n+1).toLowerCase()},Z=e=>{const n=e.replace(/\\/g,"/").split("/").pop()||"document",t=n.lastIndexOf(".");return(t>0?n.slice(0,t):n).replace(/[^a-zA-Z0-9._-]+/g,"-")||"document"},E=e=>{const n=[];for(const t of e.replace(/\\/g,"/").split("/"))if(!(!t||t===".")){if(t===".."){if(n.length===0)return;n.pop();continue}n.push(t)}return n.join("/")},b=(e,n)=>{if(n.startsWith("/"))return E(n.slice(1));const t=e.includes("/")?e.slice(0,e.lastIndexOf("/")+1):"";return E(`${t}${n}`)},J=e=>{const n=e.lastIndexOf("/"),t=n===-1?"":e.slice(0,n+1),o=e.slice(n+1);return`${t}_rels/${o}.rels`},M=(e,n)=>{const t=Number.parseInt(e,n);return t<0||t>1114111||t>=55296&&t<=57343?"�":String.fromCodePoint(t)},g=e=>e.replace(/&#x([0-9a-f]+);/gi,(n,t)=>M(t,16)).replace(/&#([0-9]+);/g,(n,t)=>M(t,10)).replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"').replace(/&apos;/g,"'").replace(/&amp;/g,"&"),u=(e,n)=>{const t=n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),o=e.match(new RegExp(`(?:^|\\s)${t}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`,"i"));return g(o?.[1]??o?.[2]??"")||void 0},p=(e,n)=>{const t=n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return Array.from(e.matchAll(new RegExp(`<${t}\\b[^>]*>[\\s\\S]*?<\\/${t}>`,"gi")),o=>o[0])},K=new TextDecoder,h=(e,n)=>{const t=e[n];return t?K.decode(t):""},f=(e,n)=>{const t=new Map,o=h(e,J(n));for(const s of o.matchAll(/<Relationship\b[^>]*\/?\s*>/gi)){const r=u(s[0],"Id"),c=u(s[0],"Target");!r||!c||t.set(r,{target:c,type:u(s[0],"Type")??""})}return t},v=e=>{const n=e.charCodeAt(0);return n<=31||n===127},Q=e=>Array.from(e).some(v),tt=e=>{const n=e.replace(/\\/g,"/").split("/").pop()||"embedded image";return Array.from(n.slice(0,200),t=>t==="["||t==="]"||v(t)?"_":t).join("")},T=(e,n)=>n.every((t,o)=>e[o]===t),x=(e,n,t)=>{if(e.byteLength<n+t.length)return!1;for(let o=0;o<t.length;o+=1)if(e[n+o]!==t.charCodeAt(o))return!1;return!0},et=(e,n)=>{switch(n){case"png":return T(e,[137,80,78,71,13,10,26,10]);case"jpg":case"jpeg":return T(e,[255,216,255]);case"gif":return x(e,0,"GIF87a")||x(e,0,"GIF89a");case"webp":return x(e,0,"RIFF")&&x(e,8,"WEBP");default:return!1}};class nt{constructor(n,t,o){this.archive=n,this.documentName=t,this.reservedFilenames=new Set(Array.from(o,s=>s.toLowerCase()))}files=[];filenames=new Map;count=0;imageBytes=0;reservedFilenames;citation(n){if(!n)return"[Embedded image reference could not be resolved]";const t=E(n);if(!t)return"[Unsafe embedded image path omitted]";const o=this.filenames.get(t);if(o)return`[${o}]`;const s=this.archive[t],r=C(t),c=O.get(r),a=tt(t);if(!s||!c)return`[Unsupported embedded image omitted: ${a}]`;if(!et(s,r))return`[Invalid embedded image omitted: ${a}]`;if(this.files.length>=G||s.byteLength>V||this.imageBytes+s.byteLength>H)return`[Embedded image omitted by attachment limits: ${a}]`;const l=r==="jpeg"?"jpg":r;let i;do this.count+=1,i=`${Z(this.documentName)}-image-${this.count}.${l}`;while(this.reservedFilenames.has(i.toLowerCase()));return this.reservedFilenames.add(i.toLowerCase()),this.filenames.set(t,i),this.files.push(new File([s],i,{type:c})),this.imageBytes+=s.byteLength,`[${i}]`}all(){return this.files}}const A=(e,n,t)=>{if(!t)return;const o=n.get(t);return o?b(e,o.target):void 0},F=(e,n,t,o)=>{const s=[],r=/<(?:w:t|a:t|text:span)\b[^>]*>([\s\S]*?)<\/(?:w:t|a:t|text:span)>|<(?:w:tab|text:tab)\b[^>]*\/?>|<(?:w:br|a:br|text:line-break)\b[^>]*\/?>|<(?:a:blip|v:imagedata)\b[^>]*>|<draw:image\b[^>]*>/gi;for(const c of e.matchAll(r)){if(c[1]!==void 0){s.push(g(c[1]).replace(/<[^>]+>/g,""));continue}if(/tab/i.test(c[0])){s.push("	");continue}if(/br|line-break/i.test(c[0])){s.push(`
`);continue}const a=u(c[0],"r:embed")??u(c[0],"r:id"),l=u(c[0],"xlink:href"),i=l?b(n,l):A(n,t,a);s.push(`
${o.citation(i)}
`)}return s.join("").replace(/[ \t]+\n/g,`
`).trim()},$=(e,n,t,o,s)=>p(e,n).map(r=>F(r,t,o,s)).filter(Boolean),ot=(e,n)=>{const t="word/document.xml",o=h(e,t);if(!o)return;const s=["# Document",...$(o,"w:p",t,f(e,t),n)],r=Object.keys(e).filter(c=>/^word\/(?:header|footer)\d+\.xml$/i.test(c)).sort();for(const c of r){const a=$(h(e,c),"w:p",c,f(e,c),n);a.length>0&&s.push(`## ${c.includes("header")?"Header":"Footer"}`,...a)}return`${s.join(`

`)}
`},st=(e,n)=>Object.keys(e).filter(t=>n.test(t)).sort((t,o)=>{const s=Number(t.match(/(\d+)(?=\.xml$)/)?.[1]??0),r=Number(o.match(/(\d+)(?=\.xml$)/)?.[1]??0);return s-r}),rt=(e,n)=>{const t=st(e,/^ppt\/slides\/slide\d+\.xml$/i);if(t.length===0)return;const o=["# Presentation"];return t.forEach((s,r)=>{const c=f(e,s),a=Array.from(h(e,s).matchAll(/<a:p\b[^>]*>[\s\S]*?<\/a:p>|<p:pic\b[^>]*>[\s\S]*?<\/p:pic>/gi),d=>F(d[0],s,c,n)).filter(Boolean);o.push(`## Slide ${r+1}`,...a.length>0?a:["[Empty slide]"]);const l=Array.from(c.values()).find(d=>d.type.endsWith("/notesSlide")),i=l?b(s,l.target):void 0;if(!i)return;const m=$(h(e,i),"a:p",i,f(e,i),n);m.length>0&&o.push(`### Slide ${r+1} notes`,...m)}),`${o.join(`

`)}
`},y=e=>{let n=e+1,t="";for(;n>0;)n-=1,t=String.fromCharCode(65+n%26)+t,n=Math.floor(n/26);return t},ct=e=>{let n=0;for(const t of e.toUpperCase())n=n*26+t.charCodeAt(0)-64;return n-1},z=e=>/[\t\r\n"]/.test(e)?`"${e.replace(/"/g,'""')}"`:e,at=(e,n)=>{const t=u(e.match(/^<c\b[^>]*>/i)?.[0]??"","t");if(t==="inlineStr")return Array.from(e.matchAll(/<t\b[^>]*>([\s\S]*?)<\/t>/gi),s=>g(s[1]).replace(/<[^>]+>/g,"")).join("");const o=e.match(/<v\b[^>]*>([\s\S]*?)<\/v>/i)?.[1]??"";return t==="s"?n[Number(o)]??"":t==="b"?o==="1"?"TRUE":"FALSE":g(o)},it=e=>e.lastColumn-e.firstColumn+1<=Math.max(32,e.cells.length*4),lt=e=>{const n=new Map(e.cells.map(t=>[t.column,t.value]));return Array.from({length:e.lastColumn-e.firstColumn+1},(t,o)=>z(n.get(e.firstColumn+o)??"")).join("	")},mt=(e,n)=>{const t=[];for(const o of p(e,"row")){const s=[];for(const a of o.matchAll(/<c\b[^>]*>[\s\S]*?<\/c>/gi)){const l=a[0].match(/^<c\b[^>]*>/i)?.[0]??"",i=u(l,"r"),m=i?.match(/^([a-z]+)([1-9]\d*)$/i),d=at(a[0],n);!i||!m||!d||s.push({reference:i,column:ct(m[1]),row:Number(m[2]),value:d})}s.sort((a,l)=>a.column-l.column);const r=s[0],c=s.at(-1);!r||!c||t.push({cells:s,firstColumn:r.column,lastColumn:c.column,row:r.row})}return t},ut=e=>{const n=[];let t=[];const o=()=>{const s=t[0],r=t.at(-1);!s||!r||(n.push([`Range: ${y(s.firstColumn)}${s.row}:${y(s.lastColumn)}${r.row}`,...t.map(lt)].join(`
`)),t=[])};for(const s of e){const r=t.at(-1);if(!it(s)){o(),n.push(`Cells: ${s.cells.map(c=>`${c.reference}	${z(c.value)}`).join(" | ")}`);continue}r&&(s.row!==r.row+1||s.firstColumn!==r.firstColumn||s.lastColumn!==r.lastColumn)&&o(),t.push(s)}return o(),n},dt=(e,n,t)=>{const o=h(e,n),s=f(e,n),r=[];for(const c of o.matchAll(/<drawing\b[^>]*r:id=(?:"([^"]+)"|'([^']+)')[^>]*\/?\s*>/gi)){const a=A(n,s,c[1]??c[2]);if(!a)continue;const l=h(e,a),i=f(e,a);for(const m of l.matchAll(/<xdr:(?:oneCellAnchor|twoCellAnchor)\b[^>]*>([\s\S]*?)<\/xdr:(?:oneCellAnchor|twoCellAnchor)>/gi)){const d=m[1],L=Number(d.match(/<xdr:col>(\d+)<\/xdr:col>/i)?.[1]??0),P=Number(d.match(/<xdr:row>(\d+)<\/xdr:row>/i)?.[1]??0),I=d.match(/<a:blip\b[^>]*r:embed=(?:"([^"]+)"|'([^']+)')[^>]*>/i),Y=A(a,i,I?.[1]??I?.[2]);r.push(`Image at ${y(L)}${P+1}: ${t.citation(Y)}`)}}return r},ht=(e,n)=>{const t="xl/workbook.xml",o=h(e,t);if(!o)return;const s=f(e,t),r=p(h(e,"xl/sharedStrings.xml"),"si").map(a=>Array.from(a.matchAll(/<t\b[^>]*>([\s\S]*?)<\/t>/gi),l=>g(l[1]).replace(/<[^>]+>/g,"")).join("")),c=["# Workbook"];for(const a of o.matchAll(/<sheet\b[^>]*\/?\s*>/gi)){const l=u(a[0],"name")??"Sheet",i=u(a[0],"r:id"),m=A(t,s,i);if(!m)continue;c.push(`## Sheet: ${l}`);const d=ut(mt(h(e,m),r));c.push(...d.length>0?d:["[Empty sheet]"],...dt(e,m,n))}return`${c.join(`

`)}
`},pt=e=>{const n=u(e,"text:c");if(!n)return" ";const t=Number(n);if(Number.isSafeInteger(t)&&t>0&&t<=w)return" ".repeat(t);const o=Number.isSafeInteger(t)&&t>w?`${t-w} additional spaces omitted`:"Additional spaces omitted";return`${" ".repeat(w)}[${o}]`},ft=(e,n,t)=>e.replace(/<draw:image\b[^>]*>/gi,o=>{const s=b(n,u(o,"xlink:href")??"");return`
${t.citation(s)}
`}).replace(/<text:tab\b[^>]*\/?\s*>/gi,"	").replace(/<text:line-break\b[^>]*\/?\s*>/gi,`
`).replace(/<text:s\b[^>]*\/?\s*>/gi,pt).replace(/<[^>]+>/g,""),S=(e,n,t)=>{const o=[],s=/<text:(p|h)\b[^>]*>[\s\S]*?<\/text:\1>|<draw:image\b[^>]*>/gi;for(const r of e.matchAll(s)){const c=/^<draw:image\b/i.test(r[0])?t.citation(b(n,u(r[0],"xlink:href")??"")):g(ft(r[0],n,t)).replace(/[ \t]+\n/g,`
`).trim();c&&o.push(c)}return o},gt=(e,n)=>{const t="content.xml",o=h(e,t);if(o)return`${["# Document",...S(o,t,n)].join(`

`)}
`},bt=(e,n)=>{const t="content.xml",o=h(e,t);if(!o)return;const s=["# Presentation"];return p(o,"draw:page").forEach((c,a)=>{const l=c.match(/^<draw:page\b[^>]*>/i)?.[0]??"",i=u(l,"draw:name")??String(a+1);s.push(`## Slide: ${i}`,...S(c,t,n))}),`${s.join(`

`)}
`},wt=(e,n)=>{const t="content.xml",o=h(e,t);if(!o)return;const s=["# Workbook"];for(const r of p(o,"table:table")){const c=r.match(/^<table:table\b[^>]*>/i)?.[0]??"";s.push(`## Sheet: ${u(c,"table:name")??"Sheet"}`);for(const a of p(r,"table:shapes"))s.push(...S(a,t,n));for(const a of p(r,"table:table-row")){const l=p(a,"table:table-cell").map(i=>S(i,t,n).join(" "));l.some(Boolean)&&s.push(l.join(" | "))}}return`${s.join(`

`)}
`},xt=()=>{let e=0,n=0;return t=>{if(e+=1,n+=t.originalSize,e>W)throw new Error("Document contains too many files");if(t.originalSize>R)throw new Error("Document contains an oversized file");if(/\.(?:xml|rels)$/i.test(t.name)&&t.originalSize>X)throw new Error("Document contains XML that is too large to process safely");if(n>N)throw new Error("Document expands beyond the 100 MB safety limit");const o=E(t.name),s=t.name.endsWith("/")?t.name.slice(0,-1):t.name;if(!o||o!==s||t.name.includes("\\")||/^[a-z]:/i.test(t.name)||Q(t.name))throw new Error("Document contains an unsafe file path")}},B=e=>{const n=C(e.name);return n==="xml"||n==="rels"||O.has(n)},Et=async e=>{const n=xt();if("Bun"in globalThis){k(e,{filter:t=>(n(t),!1)});return}await new Promise((t,o)=>{j(e,{filter:s=>(n(s),!1)},s=>{s?o(s):t()})})},At=e=>{let n=0;for(const[t,o]of Object.entries(e)){if(n+=o.byteLength,o.byteLength>R)throw new Error("Document contains an oversized file");if(/\.(?:xml|rels)$/i.test(t)&&o.byteLength>X)throw new Error("Document contains XML that is too large to process safely");if(n>N)throw new Error("Document expands beyond the 100 MB safety limit")}},St=async e=>{if(e.size>U)throw new Error("Document exceeds the 20 MB attachment limit");const n=new Uint8Array(await e.arrayBuffer());await Et(n);const t="Bun"in globalThis?k(n,{filter:B}):await new Promise((o,s)=>{j(n,{filter:B},(r,c)=>{r?s(r):o(c)})});return At(t),t},$t=(e,n,t)=>{switch(e){case"docx":return ot(n,t);case"pptx":return rt(n,t);case"xlsx":return ht(n,t);case"odt":return gt(n,t);case"odp":return bt(n,t);case"ods":return wt(n,t);default:return}},yt=(e,n)=>{if(e.length<=_)return e;let t=_-D.length;const o=e.lastIndexOf("[",t-1),s=e.lastIndexOf("]",t-1);if(o>s){const r=e.indexOf("]",o),c=r===-1?"":e.slice(o+1,r);n.has(c)&&(t=o)}return`${e.slice(0,t)}${D}`},Ct=e=>{const n=new Set;for(const t of e.matchAll(/\[([^\]\r\n]+)\]/g))n.add(t[1]);return n},_t=async(e,n=[])=>{const t=C(e.name);if(!q.has(t))return;const o=await St(e),s=new nt(o,e.name,n),r=$t(t,o,s);if(!r)return;const c=s.all(),a=new Set(c.map(m=>m.name)),l=yt(r,a),i=Ct(l);return{textFile:new File([l],e.name,{type:"text/plain"}),images:c.filter(m=>i.has(m.name))}};export{_t as extractDocumentAttachments};
