const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/pdf-BnEvgIXZ.js","assets/vendor-codemirror-D0yxdRpg.js","assets/rolldown-runtime-aKtaBQYM.js","assets/docx-preview-Dxj1EkCO.js","assets/jszip.min-DT31yWkU.js"])))=>i.map(i=>d[i]);
import{i as e}from"./rolldown-runtime-aKtaBQYM.js";import{g as t}from"./vendor-codemirror-D0yxdRpg.js";import{n}from"./excel-worksheet-utils-EyvwNT4X.js";var r=!1;async function i(){let e=await t(()=>import(`./pdf-BnEvgIXZ.js`),__vite__mapDeps([0,1,2]));return r||=(e.GlobalWorkerOptions.workerSrc=new URL(`/assets/pdf.worker.min-iDqQPrd3.mjs`,``+import.meta.url).toString(),!0),e}async function a(e,t){let n=await i(),r=null;try{r=await n.getDocument({data:e}).promise;let i=`<pdf filename="${t}">`,a=await Promise.all(Array.from({length:r.numPages},(e,t)=>{let n=t+1;return(async()=>({pageNum:n,pageText:(await(await r.getPage(n)).getTextContent()).items.flatMap(e=>{let t=`str`in e?e.str:``;return t.trim()?[t]:[]}).join(` `)}))()}));for(let{pageNum:e,pageText:t}of a)i+=`\n<page number="${e}">\n${t}\n</page>`;i+=`
</pdf>`;let s=await o(r);return{extractedText:i,preview:s}}finally{r&&r.destroy()}}async function o(e){try{let t=await e.getPage(1),n=t.getViewport({scale:1}),r=Math.min(160/n.width,160/n.height),i=t.getViewport({scale:r}),a=document.createElement(`canvas`),o=a.getContext(`2d`);return o?(a.height=i.height,a.width=i.width,await t.render({canvasContext:o,viewport:i,canvas:a}).promise,a.toDataURL(`image/png`).split(`,`)[1]):void 0}catch{return}}async function s(e,n){let{parseAsync:r}=await t(async()=>{let{parseAsync:e}=await import(`./docx-preview-Dxj1EkCO.js`);return{parseAsync:e}},__vite__mapDeps([3,2,4]));try{let t=await r(e),i=`<docx filename="${n}">\n<page number="1">\n`,a=t.documentPart?.body;if(a?.children){let e=[];for(let t of a.children){let n=c(t);n&&e.push(n)}i+=e.join(`
`)}return i+=`
</page>
</docx>`,{extractedText:i}}catch(e){throw Error(`Failed to process DOCX: ${String(e)}`)}}function c(e){let t=e,n=``,r=t.type?.toLowerCase()||``;if(r===`paragraph`&&t.children)for(let e of t.children){let t=e;if((t.type?.toLowerCase()||``)===`run`&&t.children)for(let e of t.children){let t=e;(t.type?.toLowerCase()||``)===`text`&&(n+=t.text||``)}}else if(r===`table`){if(t.children){let e=[];for(let n of t.children){let t=n;if((t.type?.toLowerCase()||``)===`tablerow`&&t.children){let n=[];for(let e of t.children){let t=e;if((t.type?.toLowerCase()||``)===`tablecell`&&t.children){let e=[];for(let n of t.children){let t=c(n);t&&e.push(t)}e.length>0&&n.push(e.join(` `))}}n.length>0&&e.push(n.join(` | `))}}e.length>0&&(n=`\n[Table]\n${e.join(`
`)}\n[/Table]\n`)}}else if(t.children&&Array.isArray(t.children)){let e=[];for(let n of t.children){let t=c(n);t&&e.push(t)}n=e.join(` `)}return n.trim()}var l=120,u=12e5;function d(e){let t=[];try{if(typeof DOMParser>`u`){let n=/<a:t[^>]*>([^<]*)<\/a:t>/g,r;for(;(r=n.exec(e))!==null;){let e=r[1]?.trim();e&&t.push(e)}return t}let n=new DOMParser().parseFromString(e,`text/xml`);if(n.querySelector(`parsererror`))return t;let r=n.getElementsByTagNameNS(`http://schemas.openxmlformats.org/drawingml/2006/main`,`t`);if(r.length>0){for(let e=0;e<r.length;e++){let n=r[e]?.textContent?.trim();n&&t.push(n)}return t}let i=n.querySelectorAll(`*|t`);for(let e=0;e<i.length;e++){let n=i[e]?.textContent?.trim();n&&t.push(n)}}catch{}return t}async function f(n,r){let i=(await t(async()=>{let{default:t}=await import(`./jszip.min-DT31yWkU.js`).then(t=>e(t.default,1));return{default:t}},__vite__mapDeps([4,2]))).default;try{let e=await i.loadAsync(n),t=`<pptx filename="${r}">`,a=Object.keys(e.files).filter(e=>e.match(/ppt\/slides\/slide\d+\.xml$/)).sort((e,t)=>Number.parseInt(e.match(/slide(\d+)\.xml$/)?.[1]||`0`,10)-Number.parseInt(t.match(/slide(\d+)\.xml$/)?.[1]||`0`,10)),o=Math.min(a.length,l);a.length>l&&(t+=`\n<!-- truncated: ${a.length} slides, processing first ${l} -->`);let s=await Promise.all(a.slice(0,o).map(async(t,n)=>{let r=e.file(t);return r?{i:n,slideXml:await r.async(`text`)}:{i:n,slideXml:null}})),c=async e=>{if(e>=s.length)return;if(t.length>=u){t+=`\n<!-- extraction stopped: size limit ${u} chars -->`;return}let{i:n,slideXml:r}=s[e];if(r){let e=d(r);if(e.length>0){t+=`\n<slide number="${n+1}">`;let r=e.join(`
`),i=u-t.length-32;t+=i>=r.length?`\n${r}`:`\n${r.slice(0,Math.max(0,i))}`,t+=`
</slide>`}}e%4==3&&e+1<s.length&&await new Promise(e=>setTimeout(e,0)),await c(e+1)};return await c(0),t+=`
</pptx>`,{extractedText:t}}catch(e){throw Error(`Failed to process PPTX: ${String(e)}`)}}async function p(e,r){let i=await t(()=>import(`./xlsx-CKkngM-o.js`),[]);try{let t=i.read(e,{type:`array`}),a=`<excel filename="${r}">`,o=t.SheetNames??[];for(let[e,r]of o.entries()){let o=t.Sheets[r],s=n(i,o,r);a+=`\n<sheet name="${r}" index="${e+1}">\n${s}\n</sheet>`}return a+=`
</excel>`,{extractedText:a}}catch(e){throw Error(`Failed to process Excel: ${String(e)}`)}}export{s as processDocx,p as processExcel,a as processPdf,f as processPptx};