import{_ as b}from"./preload-helper-2LWRYvkK.js";import{f as R}from"./local-folders-Bgzoz3Cn.js";const ut="offscreen-decode",S=512*1024,$=5*1024*1024,x=20*1024*1024,v=4e4,j={png:"png",jpg:"jpeg",jpeg:"jpeg",gif:"gif",webp:"webp"};function z(e){const t=e.toLowerCase().split("/").pop()??"",n=t.lastIndexOf(".");return n===-1?"":t.slice(n+1)}function k(e,t){return{ok:!1,kind:"too_large",message:`File is ${Math.round(e/1024)} KB, over the ${Math.round(t/1024)} KB limit for this type.`}}function y(e){return e.length<=v?{text:e,truncated:!1}:{text:e.slice(0,v),truncated:!0}}async function N(e,t,n={}){const o=z(t),r=j[o];return r?O(e,r):o==="xlsx"||o==="xls"||o==="xlsm"?B(e,n.styles??!1):o==="docx"?W(e,n.styles??!1):o==="pdf"?rt(e):I(e)}async function I(e){if(e.size>S)return k(e.size,S);let t;try{t=new Uint8Array(await e.arrayBuffer())}catch(a){return{ok:!1,kind:"io",message:a.message}}const n=P(t);if(n===null)return{ok:!1,kind:"binary",message:"Looks like a binary file. Readable types: text/code, PDF, Word (.docx), Excel (.xlsx), and images."};const{text:o,truncated:r}=y(n);return{ok:!0,value:{kind:"text",text:o,format:"plain",truncated:r}}}function P(e){let t="utf-8",n=0;e.length>=2&&e[0]===255&&e[1]===254?(t="utf-16le",n=2):e.length>=2&&e[0]===254&&e[1]===255?(t="utf-16be",n=2):e.length>=3&&e[0]===239&&e[1]===187&&e[2]===191&&(n=3);let o;try{o=new TextDecoder(t,{fatal:!1}).decode(e.subarray(n))}catch{return null}return M(o)?null:o}function M(e){const t=Math.min(e.length,8192);for(let n=0;n<t;n++)if(e.charCodeAt(n)===0)return!0;return!1}async function O(e,t){if(e.size>$)return k(e.size,$);try{const n=new Uint8Array(await e.arrayBuffer());return{ok:!0,value:{kind:"image",base64:U(n),imageFormat:t}}}catch(n){return{ok:!1,kind:"io",message:n.message}}}function U(e){let t="";for(let o=0;o<e.length;o+=32768)t+=String.fromCharCode(...e.subarray(o,o+32768));return btoa(t)}const F=200;async function B(e,t){if(e.size>x)return k(e.size,x);try{const n=await b(()=>import("./xlsx-D_0l8YDs.js"),[]),o=new Uint8Array(await e.arrayBuffer()),r=n.read(o,{type:"array",cellDates:!0,cellStyles:t}),a=t?await q(o):null,l=[];for(const c of r.SheetNames){const f=r.Sheets[c];if(!f)continue;const d=L(n,f);if(!d)continue;const m=[`# Sheet: ${c}  (one JSON object per row; "row" = real row number, other keys = column letters, so {"row":15,"F":"x"} is cell F15. Empty rows/cols are omitted — read the address from the keys, NEVER count lines.)`,d],g=X(n,f);g&&m.push(g);const h=H(n,f);if(h&&m.push(h),t){const p=V(f,a==null?void 0:a.get(c));p&&m.push(p)}l.push(m.join(`
`))}const s=l.join(`

`);if(!s)return{ok:!1,kind:"empty",message:"Spreadsheet has no readable cells."};const{text:i,truncated:u}=y(s);return{ok:!0,value:{kind:"text",text:i,format:"xlsx",truncated:u}}}catch(n){return{ok:!1,kind:"io",message:`Could not parse spreadsheet: ${n.message}`}}}function L(e,t){const n=t["!ref"];if(!n)return"";const o=e.utils.decode_range(n),r=new Map;for(let s=o.s.r;s<=o.e.r;s++)for(let i=o.s.c;i<=o.e.c;i++){const u=t[e.utils.encode_cell({r:s,c:i})];if(!u)continue;const c=String(u.w??u.v??"");if(c==="")continue;let f=r.get(s);f||(f=new Map,r.set(s,f)),f.set(i,c)}return r.size===0?"":[...r.keys()].sort((s,i)=>s-i).map(s=>{const i=r.get(s);if(!i)return"";const u={row:s+1};for(const c of[...i.keys()].sort((f,d)=>f-d))u[e.utils.encode_col(c)]=i.get(c);return JSON.stringify(u)}).join(`
`)}function X(e,t){const n=t["!merges"];return!Array.isArray(n)||n.length===0?"":`Merged cells: ${n.map(r=>e.utils.encode_range(r)).join(", ")}`}function H(e,t){var o;const n=[];for(const r of Object.keys(t)){if(r.startsWith("!"))continue;const a=(o=t[r])==null?void 0:o.c;if(Array.isArray(a)&&a.length>0){const l=a.map(s=>(s.t??"").trim()).filter(Boolean).join(" ");l&&n.push(`  ${r}: ${JSON.stringify(l)}`)}}return n.length?`Comments:
${n.join(`
`)}`:""}function V(e,t){var l,s,i,u;const n=new Set;for(const c of Object.keys(e))c.startsWith("!")||n.add(c);if(t)for(const c of t.keys())n.add(c);const o=[];let r=!1;for(const c of[...n].sort(J)){if(o.length>=F){r=!0;break}const f=[],d=t==null?void 0:t.get(c);d!=null&&d.bold&&f.push("bold"),d!=null&&d.rgb&&f.push(`font #${d.rgb}`);const m=(l=e[c])==null?void 0:l.s,g=((s=m==null?void 0:m.fgColor)==null?void 0:s.rgb)??((u=(i=m==null?void 0:m.fill)==null?void 0:i.fgColor)==null?void 0:u.rgb);g&&f.push(`fill #${g}`),f.length&&o.push(`  ${c}: ${f.join(", ")}`)}if(!o.length)return"";const a=r?`
  … (more styled cells omitted)`:"";return`Cell styles (best-effort):
${o.join(`
`)}${a}`}function J(e,t){const n=e.match(/^([A-Z]+)(\d+)$/),o=t.match(/^([A-Z]+)(\d+)$/);if(!n||!o)return e.localeCompare(t);const r=Number(n[2]),a=Number(o[2]);return r!==a?r-a:n[1].localeCompare(o[1])}function K(e){var a,l,s,i;const t=[],n=((a=e.match(/<fonts\b[^>]*>([\s\S]*?)<\/fonts>/))==null?void 0:a[1])??"";for(const u of n.matchAll(/<font\b[^>]*>([\s\S]*?)<\/font>|<font\b[^>]*\/>/g)){const c=u[1]??"",f=/<b\b[^>]*\/?>/.test(c)||void 0,d=(l=c.match(/<color\b[^>]*\brgb="([0-9A-Fa-f]{6,8})"/))==null?void 0:l[1];t.push({bold:f,rgb:d?d.slice(-6).toUpperCase():void 0})}const o=[],r=((s=e.match(/<cellXfs\b[^>]*>([\s\S]*?)<\/cellXfs>/))==null?void 0:s[1])??"";for(const u of r.matchAll(/<xf\b[^>]*?\/?>/g))o.push(Number(((i=u[0].match(/\bfontId="(\d+)"/))==null?void 0:i[1])??0));return{fonts:t,xfFontIds:o}}function G(e,t,n){var r,a;const o=new Map;for(const l of e.matchAll(/<c\b[^>]*?>/g)){const s=l[0],i=(r=s.match(/\br="([A-Z]+\d+)"/))==null?void 0:r[1],u=(a=s.match(/\bs="(\d+)"/))==null?void 0:a[1];if(!i||u===void 0)continue;const c=n[t[Number(u)]??-1];if(!c)continue;const f=c.rgb&&c.rgb!=="000000"?c.rgb:void 0;(f||c.bold)&&o.set(i,{rgb:f,bold:c.bold})}return o}function Y(e,t){var r,a,l,s;const n=new Map;for(const i of t.matchAll(/<Relationship\b[^>]*>/g)){const u=(r=i[0].match(/\bId="([^"]+)"/))==null?void 0:r[1],c=(a=i[0].match(/\bTarget="([^"]+)"/))==null?void 0:a[1];u&&c&&n.set(u,c)}const o=new Map;for(const i of e.matchAll(/<sheet\b[^>]*>/g)){const u=(l=i[0].match(/\bname="([^"]+)"/))==null?void 0:l[1],c=(s=i[0].match(/\br:id="([^"]+)"/))==null?void 0:s[1],f=c?n.get(c):void 0;!u||!f||o.set(_(u),`xl/${f.replace(/^\/?xl\//,"").replace(/^\//,"")}`)}return o}async function q(e){const t=new Map;try{const{unzipSync:n,strFromU8:o}=await b(async()=>{const{unzipSync:c,strFromU8:f}=await import("./browser-DGemqFXA.js");return{unzipSync:c,strFromU8:f}},[]),r=n(e),a=c=>r[c]?o(r[c]):"",l=a("xl/styles.xml");if(!l)return t;const{fonts:s,xfFontIds:i}=K(l),u=Y(a("xl/workbook.xml"),a("xl/_rels/workbook.xml.rels"));for(const[c,f]of u){const d=a(f);if(!d)continue;const m=G(d,i,s);m.size&&t.set(c,m)}}catch{}return t}async function W(e,t){if(e.size>x)return k(e.size,x);const n=await e.arrayBuffer();if(t)try{const o=await b(()=>import("./index-bH25tDFr.js").then(l=>l.i),[]),{value:r}=await o.convertToHtml({arrayBuffer:n}),a=nt(r).trim();if(a){const l=await et(n),{text:s,truncated:i}=y(a);return{ok:!0,value:{kind:"text",text:l?`${s}

${l}`:s,format:"docx",truncated:i}}}}catch{}try{const{unzipSync:o,strFromU8:r}=await b(async()=>{const{unzipSync:c,strFromU8:f}=await import("./browser-DGemqFXA.js");return{unzipSync:c,strFromU8:f}},[]),l=o(new Uint8Array(n),{filter:c=>c.name==="word/document.xml"})["word/document.xml"];if(!l)return{ok:!1,kind:"empty",message:"Not a readable .docx (no document.xml)."};const s=Z(r(l)).trim();if(!s)return{ok:!1,kind:"empty",message:"Document has no readable text."};const{text:i,truncated:u}=y(s);return{ok:!0,value:{kind:"text",text:i,format:"docx",truncated:u}}}catch(o){return{ok:!1,kind:"io",message:`Could not parse document: ${o.message}`}}}function Z(e){const t=e.replace(/<w:tab\b[^>]*\/>/g,"	").replace(/<w:br\b[^>]*\/>/g,`
`).replace(/<\/w:p>/g,`
`);let n="";const o=/<w:t\b[^>]*>([\s\S]*?)<\/w:t>|([\n\t])/g;let r;for(;(r=o.exec(t))!==null;)r[1]!==void 0?n+=_(r[1]):r[2]&&(n+=r[2]);return n}function Q(e){var n,o,r,a,l,s;const t=[];for(const i of e.matchAll(/<w:r\b[^>]*>([\s\S]*?)<\/w:r>/g)){const u=i[1]??"",c=((n=u.match(/<w:rPr\b[^>]*>([\s\S]*?)<\/w:rPr>/))==null?void 0:n[1])??"",f=(r=(o=c.match(/<w:color\b[^>]*\bw:val="([0-9A-Fa-f]{6})"/))==null?void 0:o[1])==null?void 0:r.toUpperCase(),d=(a=c.match(/<w:highlight\b[^>]*\bw:val="([^"]+)"/))==null?void 0:a[1],m=(s=(l=c.match(/<w:shd\b[^>]*\bw:fill="([0-9A-Fa-f]{6})"/))==null?void 0:l[1])==null?void 0:s.toUpperCase();let g="";for(const D of u.matchAll(/<w:t\b[^>]*>([\s\S]*?)<\/w:t>/g))g+=D[1]??"";if(g=_(g).trim(),!g)continue;const h=f&&f!=="000000"?f:void 0,p=d&&d!=="none"?d:void 0,A=m&&m!=="FFFFFF"?m:void 0;(h||p||A)&&t.push({text:g,color:h,highlight:p,fill:A})}return t}function tt(e){if(!e.length)return"";const t=[];let n=!1;for(const r of e){if(t.length>=F){n=!0;break}const a=[];r.color&&a.push(`font #${r.color}`),r.highlight&&a.push(`highlight ${r.highlight}`),r.fill&&a.push(`fill #${r.fill}`);const l=r.text.length>60?`${r.text.slice(0,57)}…`:r.text;t.push(`  ${JSON.stringify(l)}: ${a.join(", ")}`)}const o=n?`
  … (more coloured runs omitted)`:"";return`Coloured text (best-effort):
${t.join(`
`)}${o}`}async function et(e){try{const{unzipSync:t,strFromU8:n}=await b(async()=>{const{unzipSync:a,strFromU8:l}=await import("./browser-DGemqFXA.js");return{unzipSync:a,strFromU8:l}},[]),r=t(new Uint8Array(e),{filter:a=>a.name==="word/document.xml"})["word/document.xml"];return r?tt(Q(n(r))):""}catch{return""}}function _(e){return e.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"').replace(/&apos;/g,"'").replace(/&amp;/g,"&")}function nt(e){let t=e;return t=t.replace(/<(strong|b)\b[^>]*>([\s\S]*?)<\/\1>/gi,(n,o,r)=>`**${w(r)}**`),t=t.replace(/<(em|i)\b[^>]*>([\s\S]*?)<\/\1>/gi,(n,o,r)=>`*${w(r)}*`),t=t.replace(/<a\b[^>]*href="([^"]*)"[^>]*>([\s\S]*?)<\/a>/gi,(n,o,r)=>`[${w(r)}](${o})`),t=t.replace(/<table\b[^>]*>([\s\S]*?)<\/table>/gi,(n,o)=>ot(o)),t=t.replace(/<h([1-6])\b[^>]*>([\s\S]*?)<\/h\1>/gi,(n,o,r)=>`
${"#".repeat(Number(o))} ${w(r)}
`),t=t.replace(/<li\b[^>]*>([\s\S]*?)<\/li>/gi,(n,o)=>`- ${w(o)}
`),t=t.replace(/<\/?(ul|ol)\b[^>]*>/gi,`
`),t=t.replace(/<\/p>/gi,`

`).replace(/<p\b[^>]*>/gi,""),t=t.replace(/<br\s*\/?>/gi,`
`),t=_(t.replace(/<[^>]+>/g,"")),t.replace(/\n{3,}/g,`

`).trim()}function w(e){return _(e.replace(/<[^>]+>/g,"")).trim()}function ot(e){const t=[],n=/<tr\b[^>]*>([\s\S]*?)<\/tr>/gi;let o;for(;(o=n.exec(e))!==null;){const s=[],i=/<(td|th)\b[^>]*>([\s\S]*?)<\/\1>/gi;let u;for(;(u=i.exec(o[1]??""))!==null;)s.push(w(u[2]??"").replace(/\|/g,"\\|").replace(/\n/g," "));s.length&&t.push(s)}if(!t.length)return"";const r=Math.max(...t.map(s=>s.length)),a=s=>{const i=[...s];for(;i.length<r;)i.push("");return i},l=[`| ${a(t[0]??[]).join(" | ")} |`,`| ${Array(r).fill("---").join(" | ")} |`];for(const s of t.slice(1))l.push(`| ${a(s).join(" | ")} |`);return`
${l.join(`
`)}
`}async function rt(e){if(e.size>x)return k(e.size,x);let t;try{const{getDocumentProxy:a,extractText:l}=await b(async()=>{const{getDocumentProxy:u,extractText:c}=await import("./index-CDgo472Q.js");return{getDocumentProxy:u,extractText:c}},[]),s=await a(new Uint8Array(await e.arrayBuffer()),{disableFontFace:!0,useSystemFonts:!1,isEvalSupported:!1}),i=await l(s,{mergePages:!0});t=Array.isArray(i.text)?i.text.join(`
`):i.text}catch(a){return{ok:!1,kind:"io",message:`Could not parse PDF: ${a.message}`}}const n=t.trim();if(!n)return{ok:!1,kind:"scanned_pdf",message:"This PDF has no extractable text — it is likely scanned / image-only. Reading scanned PDFs as images is not supported yet."};const{text:o,truncated:r}=y(n);return{ok:!0,value:{kind:"text",text:o,format:"pdf",truncated:r}}}const st=2e3,at=8;function T(e){const t=e.replaceAll("\\","/").split("/").map(n=>n.trim()).filter(n=>n.length>0&&n!==".");return t.some(n=>n==="..")?null:t}async function ct(e){try{return await e.queryPermission({mode:"read"})==="granted"}catch{return!0}}async function E(e){const t=await R(e);return t?await ct(t.handle)?{ok:!0,value:t.handle}:{ok:!1,kind:"no_permission",message:`Read permission for "${e}" has lapsed. Ask the user to re-grant it from the panel's Local folders section (it needs a click there).`}:{ok:!1,kind:"no_folder",message:`No local folder named "${e}" has been added. Ask the user to add it from the panel's Local folders section.`}}async function C(e,t){let n=e;for(const o of t)try{n=await n.getDirectoryHandle(o)}catch{return{ok:!1,kind:"not_found",message:`Directory not found: ${o}`}}return{ok:!0,value:n}}async function ft(e,t="",n=!1){const o=await E(e);if(!o.ok)return o;const r=T(t);if(r===null)return{ok:!1,kind:"not_found",message:`Invalid path: ${t}`};const a=await C(o.value,r);if(!a.ok)return a;const l=r.join("/"),s=[];let i=!1;const u=[{handle:a.value,prefix:l,depth:0}];for(;u.length>0;){const{handle:c,prefix:f,depth:d}=u.shift();try{for await(const[m,g]of c.entries()){if(s.length>=st){i=!0;break}const h=f?`${f}/${m}`:m;if(g.kind==="directory")s.push({path:h,kind:"directory"}),n&&d+1<at&&u.push({handle:g,prefix:h,depth:d+1});else{let p;if(!n)try{p=(await g.getFile()).size}catch{}s.push({path:h,kind:"file",size:p})}}}catch(m){return{ok:!1,kind:"io",message:m.message}}if(i)break}return s.sort((c,f)=>c.path.localeCompare(f.path)),{ok:!0,value:{entries:s,truncated:i}}}async function dt(e,t,n={}){const o=await E(e);if(!o.ok)return o;const r=T(t);if(r===null||r.length===0)return{ok:!1,kind:"not_found",message:`Invalid file path: ${t}`};const a=r[r.length-1],l=await C(o.value,r.slice(0,-1));if(!l.ok)return l;let s;try{s=await(await l.value.getFileHandle(a)).getFile()}catch{return{ok:!1,kind:"not_found",message:`File not found: ${t}`}}const i=r.join("/"),u=await N(s,i,n);return u.ok?{ok:!0,value:{path:i,size:s.size,...u.value}}:u}export{ut as O,ft as l,dt as r};
