import{D as e,d as t,g as n,u as r,x as i,y as a}from"./paths-DBmKCtUs.js";import{d as o,m as s}from"./subsystem-hyY_8Y33.js";import{W as c}from"./workspace-Dc-i8PF7.js";import{h as l}from"./utils-HgCTSfjA.js";import{Wn as u,Wr as d,fi as f,mi as p,pi as m,ui as h}from"./model-selection-BDkm-Fer.js";import{a as g,n as ee}from"./fetch-CeRf3vRn.js";import _ from"node:fs/promises";import v from"node:path";import{fileURLToPath as te}from"node:url";import y from"markdown-it";function b(e){let t=[],n,r=0;for(;r<=e.length;){let i=e.indexOf(`
`,r),a=i===-1?e.length:i,o=e.slice(r,a),s=o.match(/^( {0,3})(`{3,}|~{3,})(.*)$/);if(s){let e=s[1],i=s[2],c=i[0],l=i.length;if(!n)n={start:r,markerChar:c,markerLen:l,openLine:o,marker:i,indent:e};else if(n.markerChar===c&&l>=n.markerLen){let e=a;t.push({start:n.start,end:e,openLine:n.openLine,marker:n.marker,indent:n.indent}),n=void 0}}if(i===-1)break;r=i+1}return n&&t.push({start:n.start,end:e.length,openLine:n.openLine,marker:n.marker,indent:n.indent}),t}function x(e,t){let n=0,r=e.length-1;for(;n<=r;){let i=Math.floor((n+r)/2),a=e[i];if(!a)break;if(t<=a.start){r=i-1;continue}if(t>=a.end){n=i+1;continue}return a}}function S(e,t){return!x(e,t)}function ne(e,t,n){if(!e)return[];if(t<=0||e.length<=t)return[e];let r=[],i=e;for(;i.length>t;){let e=n(i.slice(0,t)),a=Number.isFinite(e)&&e>0&&e<=t?e:t,o=i.slice(0,a).trimEnd();o.length>0&&r.push(o);let s=a<i.length&&/\s/.test(i[a]),c=Math.min(i.length,a+(s?1:0));i=i.slice(c).trimStart()}return i.length&&r.push(i),r}const C=`length`;function re(e,t){if(!e)return;let n=c(t),r=e.accounts;if(r&&typeof r==`object`){let e=d(r,n);if(typeof e?.textChunkLimit==`number`)return e.textChunkLimit}return e.textChunkLimit}function ie(e,t,n,r){let i=typeof r?.fallbackLimit==`number`&&r.fallbackLimit>0?r.fallbackLimit:4e3,a=(()=>{if(!(!t||t===`webchat`))return re(e?.channels?.[t]??e?.[t],n)})();return typeof a==`number`&&a>0?a:i}function w(e,t){if(!e)return;let n=c(t),r=e.accounts;if(r&&typeof r==`object`){let e=d(r,n);if(e?.chunkMode)return e.chunkMode}return e.chunkMode}function ae(e,t,n){return!t||t===`webchat`?C:w(e?.channels?.[t]??e?.[t],n)??C}function oe(e,t,n){if(!e)return[];if(t<=0)return e.trim()?[e]:[];let r=n?.splitLongLines!==!1,i=n?.trimLines!==!1,a=le(e,n?.isSafeBreak),o=[],s=0;for(let e of a){let n=e.trim();if(!n){s+=1;continue}let a=Math.max(0,t-1),c=s>0?Math.min(s,a):0,l=c>0?`
`.repeat(c):``;s=0;let u=i?n:e;if(!r||u.length+l.length<=t){o.push(l+u);continue}let d=Math.max(1,t-l.length),f=u.slice(0,d);o.push(l+f);let p=u.slice(d);p&&o.push(...D(p,t))}return s>0&&o.length>0&&(o[o.length-1]+=`
`.repeat(s)),o}function T(e,t,n){if(!e)return[];if(t<=0)return[e];let r=n?.splitLongParagraphs!==!1,i=e.replace(/\r\n?/g,`
`);if(!/\n[\t ]*\n+/.test(i))return i.length<=t||!r?[i]:D(i,t);let a=b(i),o=[],s=/\n[\t ]*\n+/g,c=0;for(let e of i.matchAll(s)){let t=e.index??0;S(a,t)&&(o.push(i.slice(c,t)),c=t+e[0].length)}o.push(i.slice(c));let l=[];for(let e of o){let n=e.replace(/\s+$/g,``);n.trim()&&(n.length<=t?l.push(n):r?l.push(...D(n,t)):l.push(n))}return l}function se(e,t,n){return n===`newline`?T(e,t):D(e,t)}function ce(e,t,n){if(n===`newline`){let n=T(e,t,{splitLongParagraphs:!1}),r=[];for(let e of n){let n=O(e,t);!n.length&&e?r.push(e):r.push(...n)}return r}return O(e,t)}function le(e,t=()=>!0){let n=[],r=0;for(let i=0;i<e.length;i++)e[i]===`
`&&t(i)&&(n.push(e.slice(r,i)),r=i+1);return n.push(e.slice(r)),n}function E(e,t){if(!e)return[];if(t<=0||e.length<=t)return[e]}function D(e,t){return E(e,t)||ne(e,t,e=>{let{lastNewline:t,lastWhitespace:n}=k(e,0,e.length);return t>0?t:n})}function O(e,t){let n=E(e,t);if(n)return n;let r=[],i=b(e),a=0,o;for(;a<e.length;){let n=o?`${o.openLine}\n`:``,s=Math.max(1,t-n.length);if(e.length-a<=s){let t=`${n}${e.slice(a)}`;t.length>0&&r.push(t);break}let c=Math.min(e.length,a+s),l=de(e,a,c,i),u=l>a?l:c,d=S(i,u)?void 0:x(i,u),f=d;if(d){let t=`${d.indent}${d.marker}`,n=a+(s-(t.length+1));if(n<=a)f=void 0,u=c;else{let r=Math.min(e.length,Math.max(a+1,d.start+d.openLine.length+2)),o=a+(s-t.length),l=!1,p=e.lastIndexOf(`
`,Math.max(a,o-1));for(;p>=a;){let t=p+1;if(t<r)break;let n=x(i,t);if(n&&n.start===d.start){u=t,l=!0;break}p=e.lastIndexOf(`
`,p-1)}l||(r>o?(f=void 0,u=c):u=Math.max(r,n))}let r=x(i,u);f=r&&r.start===d.start?r:void 0}let p=e.slice(a,u);if(!p)break;let m=`${n}${p}`,h=u<e.length&&/\s/.test(e[u]),g=Math.min(e.length,u+(h?1:0));if(f){let e=`${f.indent}${f.marker}`;m=m.endsWith(`
`)?`${m}${e}`:`${m}\n${e}`,o=f}else g=ue(e,g),o=void 0;r.push(m),a=g}return r}function ue(e,t=0){let n=t;for(;n<e.length&&e[n]===`
`;)n++;return n}function de(e,t,n,r){let{lastNewline:i,lastWhitespace:a}=k(e,t,n,e=>S(r,e));return i>t?i:a>t?a:-1}function k(e,t,n,r=()=>!0){let i=-1,a=-1,o=0;for(let s=t;s<n;s++){if(!r(s))continue;let t=e[s];if(t===`(`){o+=1;continue}if(t===`)`&&o>0){--o;continue}o===0&&(t===`
`?i=s:/\s/.test(t)&&(a=s))}return{lastNewline:i,lastWhitespace:a}}const fe=new Map([[`signal`,`bullets`],[`whatsapp`,`bullets`],[`mattermost`,`off`]]),A=e=>e===`off`||e===`bullets`||e===`code`;function pe(e,t){if(!e)return;let n=c(t),r=e.accounts;if(r&&typeof r==`object`){let e=d(r,n)?.markdown?.tables;if(A(e))return e}let i=e.markdown?.tables;return A(i)?i:void 0}function me(e){let t=u(e.channel),n=t?fe.get(t)??`code`:`code`;return!t||!e.cfg?n:pe(e.cfg.channels?.[t]??e.cfg?.[t],e.accountId)??n}function j(e){return typeof e.maxBytesOrOptions==`number`||e.maxBytesOrOptions===void 0?{maxBytes:e.maxBytesOrOptions,optimizeImages:e.optimizeImages,ssrfPolicy:e.options?.ssrfPolicy,localRoots:e.options?.localRoots}:{...e.maxBytesOrOptions,optimizeImages:e.optimizeImages?e.maxBytesOrOptions.optimizeImages??!0:!1}}var M=class extends Error{constructor(e,t,n){super(t,n),this.code=e,this.name=`LocalMediaAccessError`}};function N(){return g()}async function he(e,t){if(t===`any`)return;let n=t??N(),r;try{r=await _.realpath(e)}catch{r=v.resolve(e)}if(t===void 0){let t=n.find(e=>v.basename(e)===`workspace`);if(t){let n=v.dirname(t),i=v.relative(n,r);if(i&&!i.startsWith(`..`)&&!v.isAbsolute(i)&&(i.split(v.sep)[0]??``).startsWith(`workspace-`))throw new M(`path-not-allowed`,`Local media path is not under an allowed directory: ${e}`)}}for(let e of n){let t;try{t=await _.realpath(e)}catch{t=v.resolve(e)}if(t===v.parse(t).root)throw new M(`invalid-root`,`Invalid localRoots entry (refuses filesystem root): ${e}. Pass a narrower directory.`);if(r===t||r.startsWith(t+v.sep))return}throw new M(`path-not-allowed`,`Local media path is not under an allowed directory: ${e}`)}const ge=/^image\/hei[cf]$/i,P=/\.(heic|heif)$/i;function F(e,t=2){return(e/1048576).toFixed(t)}function I(e,t,n){return`${e} exceeds ${F(t,0)}MB limit (got ${F(n)}MB)`}function _e(e,t,n){return`${e} could not be reduced below ${F(t,0)}MB (got ${F(n)}MB)`}function L(e){return!!(e.contentType&&ge.test(e.contentType.trim())||e.fileName&&P.test(e.fileName.trim()))}function ve(e){if(!e)return;let t=e.trim();if(!t)return e;let n=v.parse(t);return!n.ext||P.test(n.ext)?v.format({dir:n.dir,name:n.name||t,ext:`.jpg`}):v.format({dir:n.dir,name:n.name,ext:`.jpg`})}function ye(e){if(s()&&!(e.optimized.optimizedSize>=e.originalSize)){if(e.optimized.format===`png`){o(`Optimized PNG (preserving alpha) from ${F(e.originalSize)}MB to ${F(e.optimized.optimizedSize)}MB (side≤${e.optimized.resizeSide}px)`);return}o(`Optimized media from ${F(e.originalSize)}MB to ${F(e.optimized.optimizedSize)}MB (side≤${e.optimized.resizeSide}px, q=${e.optimized.quality})`)}}async function be(e){let{buffer:t,cap:n,meta:r}=e;if((r?.contentType===`image/png`||r?.fileName?.toLowerCase().endsWith(`.png`))&&await f(t)){let e=await m(t,n);if(e.buffer.length<=n)return{...e,format:`png`};s()&&o(`PNG with alpha still exceeds ${F(n,0)}MB after optimization; falling back to JPEG`)}return{...await Ce(t,n,r),format:`jpeg`}}async function R(o,s={}){let{maxBytes:c,optimizeImages:u=!0,ssrfPolicy:d,localRoots:f,sandboxValidated:p=!1,readFile:m}=s;if(o=o.replace(/^\s*MEDIA\s*:\s*/i,``),o.startsWith(`file://`))try{o=te(o)}catch{throw new M(`invalid-file-url`,`Invalid file:// URL: ${o}`)}let h=async(e,t,n)=>{let r=e.length,i=await be({buffer:e,cap:t,meta:n});if(ye({originalSize:r,optimized:i}),i.buffer.length>t)throw Error(_e(`Media`,t,i.buffer.length));let a=i.format===`png`?`image/png`:`image/jpeg`,o=i.format===`jpeg`&&n&&L(n)?ve(n.fileName):n?.fileName;return{buffer:i.buffer,contentType:a,kind:`image`,fileName:o}},g=async e=>{let t=c===void 0?a(e.kind??`document`):c;if(e.kind===`image`){let n=e.contentType===`image/gif`;if(n||!u){if(e.buffer.length>t)throw Error(I(n?`GIF`:`Media`,t,e.buffer.length));return{buffer:e.buffer,contentType:e.contentType,kind:e.kind,fileName:e.fileName}}return{...await h(e.buffer,t,{contentType:e.contentType,fileName:e.fileName})}}if(e.buffer.length>t)throw Error(I(`Media`,t,e.buffer.length));return{buffer:e.buffer,contentType:e.contentType??void 0,kind:e.kind,fileName:e.fileName}};if(/^https?:\/\//i.test(o)){let e=a(`document`),{buffer:t,contentType:r,fileName:i}=await ee({url:o,maxBytes:c===void 0?e:u?Math.max(c,e):c,ssrfPolicy:d});return await g({buffer:t,contentType:r,kind:n(r),fileName:i})}if(o.startsWith(`~`)&&(o=l(o)),(p||f===`any`)&&!m)throw new M(`unsafe-bypass`,`Refusing localRoots bypass without readFile override. Use sandboxValidated with readFile, or pass explicit localRoots.`);p||f===`any`||await he(o,f);let _;if(m)_=await m(o);else try{_=(await e({filePath:o})).buffer}catch(e){throw e instanceof i?e.code===`not-found`?new M(`not-found`,`Local media file not found: ${o}`,{cause:e}):e.code===`not-file`?new M(`not-file`,`Local media path is not a file: ${o}`,{cause:e}):new M(`invalid-path`,`Local media path is not safe to read: ${o}`,{cause:e}):e}let y=await r({buffer:_,filePath:o}),b=n(y),x=v.basename(o)||void 0;if(x&&!v.extname(x)&&y){let e=t(y);e&&(x=`${x}${e}`)}return await g({buffer:_,contentType:y,kind:b,fileName:x})}async function xe(e,t,n){return await R(e,j({maxBytesOrOptions:t,options:n,optimizeImages:!0}))}async function Se(e,t,n){return await R(e,j({maxBytesOrOptions:t,options:n,optimizeImages:!1}))}async function Ce(e,t,n={}){let r=e;if(L(n))try{r=await h(e)}catch(e){throw Error(`HEIC image conversion failed: ${String(e)}`,{cause:e})}let i=[2048,1536,1280,1024,800],a=[80,70,60,50,40],o=null;for(let e of i)for(let n of a)try{let i=await p({buffer:r,maxSide:e,quality:n,withoutEnlargement:!0}),a=i.length;if((!o||a<o.size)&&(o={buffer:i,size:a,resizeSide:e,quality:n}),a<=t)return{buffer:i,optimizedSize:a,resizeSide:e,quality:n}}catch{}if(o)return{buffer:o.buffer,optimizedSize:o.size,resizeSide:o.resizeSide,quality:o.quality};throw Error(`Failed to optimize image`)}function we(e){let t=new y({html:!1,linkify:e.linkify??!0,breaks:!1,typographer:!1});return t.enable(`strikethrough`),e.tableMode&&e.tableMode!==`off`?t.enable(`table`):t.disable(`table`),e.autolink===!1&&t.disable(`autolink`),t}function z(e,t){if(e.attrGet)return e.attrGet(t);if(e.attrs){for(let[n,r]of e.attrs)if(n===t)return r}return null}function B(e,t){return{...e,type:`text`,content:t,children:void 0}}function Te(e){for(let t of e)t.children&&t.children.length>0&&(t.children=Ee(t.children))}function Ee(e){let t=0;for(let n of e){if(n.type!==`text`)continue;let e=n.content??``,r=0;for(;r<e.length;){let n=e.indexOf(`||`,r);if(n===-1)break;t+=1,r=n+2}}if(t<2)return e;let n=t-t%2,r=[],i={spoilerOpen:!1},a=0;for(let t of e){if(t.type!==`text`){r.push(t);continue}let e=t.content??``;if(!e.includes(`||`)){r.push(t);continue}let o=0;for(;o<e.length;){let s=e.indexOf(`||`,o);if(s===-1){o<e.length&&r.push(B(t,e.slice(o)));break}if(a>=n){r.push(B(t,e.slice(o)));break}s>o&&r.push(B(t,e.slice(o,s))),a+=1,i.spoilerOpen=!i.spoilerOpen,r.push({type:i.spoilerOpen?`spoiler_open`:`spoiler_close`}),o=s+2}}return r}function De(){return{text:``,styles:[],openStyles:[],links:[],linkStack:[]}}function V(e){return e.table?.currentCell??e}function H(e,t){if(!t)return;let n=V(e);n.text+=t}function U(e,t){let n=V(e);n.openStyles.push({style:t,start:n.text.length})}function W(e,t){let n=V(e);for(let e=n.openStyles.length-1;e>=0;--e)if(n.openStyles[e]?.style===t){let r=n.openStyles[e].start;n.openStyles.splice(e,1);let i=n.text.length;i>r&&n.styles.push({start:r,end:i,style:t});return}}function G(e){e.env.listStack.length>0||e.table||(e.text+=`

`)}function Oe(e){let t=e.env.listStack,n=t[t.length-1];if(!n)return;n.index+=1;let r=`  `.repeat(Math.max(0,t.length-1)),i=n.type===`ordered`?`${n.index}. `:`• `;e.text+=`${r}${i}`}function ke(e,t){if(!t)return;let n=V(e),r=n.text.length;n.text+=t,n.styles.push({start:r,end:r+t.length,style:`code`})}function Ae(e,t){let n=t??``;n.endsWith(`
`)||(n=`${n}\n`);let r=V(e),i=r.text.length;r.text+=n,r.styles.push({start:i,end:i+n.length,style:`code_block`}),e.env.listStack.length===0&&(r.text+=`
`)}function je(e){let t=V(e),n=t.linkStack.pop();if(!n?.href)return;let r=n.href.trim();if(!r)return;let i=n.labelStart,a=t.text.length;if(a<=i){t.links.push({start:i,end:a,href:r});return}t.links.push({start:i,end:a,href:r})}function Me(){return{headers:[],rows:[],currentRow:[],currentCell:null,inHeader:!1}}function Ne(e){return X(e),{text:e.text,styles:e.styles,links:e.links}}function K(e){let t=e.text,n=0,r=t.length;for(;n<r&&/\s/.test(t[n]??``);)n+=1;for(;r>n&&/\s/.test(t[r-1]??``);)--r;if(n===0&&r===t.length)return e;let i=t.slice(n,r),a=i.length,o=[];for(let t of e.styles){let e=Math.max(0,t.start-n),r=Math.min(a,t.end-n);r>e&&o.push({start:e,end:r,style:t.style})}let s=[];for(let t of e.links){let e=Math.max(0,t.start-n),r=Math.min(a,t.end-n);r>e&&s.push({start:e,end:r,href:t.href})}return{text:i,styles:o,links:s}}function q(e,t){if(!t.text)return;let n=e.text.length;e.text+=t.text;for(let r of t.styles)e.styles.push({start:n+r.start,end:n+r.end,style:r.style});for(let r of t.links)e.links.push({start:n+r.start,end:n+r.end,href:r.href})}function Pe(e,t){t.text&&(e.text+=t.text)}function J(e,t){let{header:n,value:r,columnIndex:i,includeColumnFallback:a}=t;r?.text&&(e.text+=`• `,n?.text?(q(e,n),e.text+=`: `):a&&(e.text+=`Column ${i}: `),q(e,r),e.text+=`
`)}function Fe(e){if(!e.table)return;let t=e.table.headers.map(K),n=e.table.rows.map(e=>e.map(K));if(!(t.length===0&&n.length===0))if(t.length>1&&n.length>0)for(let r of n){if(r.length===0)continue;let n=r[0];if(n?.text){let t=e.text.length;q(e,n);let r=e.text.length;r>t&&e.styles.push({start:t,end:r,style:`bold`}),e.text+=`
`}for(let n=1;n<r.length;n++)J(e,{header:t[n],value:r[n],columnIndex:n,includeColumnFallback:!0});e.text+=`
`}else for(let r of n){for(let n=0;n<r.length;n++)J(e,{header:t[n],value:r[n],columnIndex:n,includeColumnFallback:!1});e.text+=`
`}}function Ie(e){if(!e.table)return;let t=e.table.headers.map(K),n=e.table.rows.map(e=>e.map(K)),r=Math.max(t.length,...n.map(e=>e.length));if(r===0)return;let i=Array.from({length:r},()=>0),a=e=>{for(let t=0;t<r;t+=1){let n=e[t]?.text.length??0;i[t]<n&&(i[t]=n)}};a(t);for(let e of n)a(e);let o=e.text.length,s=t=>{e.text+=`|`;for(let n=0;n<r;n+=1){e.text+=` `;let r=t[n];r&&Pe(e,r);let a=i[n]-(r?.text.length??0);a>0&&(e.text+=` `.repeat(a)),e.text+=` |`}e.text+=`
`};s(t),(()=>{e.text+=`|`;for(let t=0;t<r;t+=1){let n=Math.max(3,i[t]);e.text+=` ${`-`.repeat(n)} |`}e.text+=`
`})();for(let e of n)s(e);let c=e.text.length;c>o&&e.styles.push({start:o,end:c,style:`code_block`}),e.env.listStack.length===0&&(e.text+=`
`)}function Y(e,t){for(let n of e)switch(n.type){case`inline`:n.children&&Y(n.children,t);break;case`text`:H(t,n.content??``);break;case`em_open`:U(t,`italic`);break;case`em_close`:W(t,`italic`);break;case`strong_open`:U(t,`bold`);break;case`strong_close`:W(t,`bold`);break;case`s_open`:U(t,`strikethrough`);break;case`s_close`:W(t,`strikethrough`);break;case`code_inline`:ke(t,n.content??``);break;case`spoiler_open`:t.enableSpoilers&&U(t,`spoiler`);break;case`spoiler_close`:t.enableSpoilers&&W(t,`spoiler`);break;case`link_open`:{let e=z(n,`href`)??``,r=V(t);r.linkStack.push({href:e,labelStart:r.text.length});break}case`link_close`:je(t);break;case`image`:H(t,n.content??``);break;case`softbreak`:case`hardbreak`:H(t,`
`);break;case`paragraph_close`:G(t);break;case`heading_open`:t.headingStyle===`bold`&&U(t,`bold`);break;case`heading_close`:t.headingStyle===`bold`&&W(t,`bold`),G(t);break;case`blockquote_open`:t.blockquotePrefix&&(t.text+=t.blockquotePrefix),U(t,`blockquote`);break;case`blockquote_close`:W(t,`blockquote`);break;case`bullet_list_open`:t.env.listStack.length>0&&(t.text+=`
`),t.env.listStack.push({type:`bullet`,index:0});break;case`bullet_list_close`:t.env.listStack.pop(),t.env.listStack.length===0&&(t.text+=`
`);break;case`ordered_list_open`:{t.env.listStack.length>0&&(t.text+=`
`);let e=Number(z(n,`start`)??`1`);t.env.listStack.push({type:`ordered`,index:e-1});break}case`ordered_list_close`:t.env.listStack.pop(),t.env.listStack.length===0&&(t.text+=`
`);break;case`list_item_open`:Oe(t);break;case`list_item_close`:t.text.endsWith(`
`)||(t.text+=`
`);break;case`code_block`:case`fence`:Ae(t,n.content??``);break;case`html_block`:case`html_inline`:H(t,n.content??``);break;case`table_open`:t.tableMode!==`off`&&(t.table=Me(),t.hasTables=!0);break;case`table_close`:t.table&&(t.tableMode===`bullets`?Fe(t):t.tableMode===`code`&&Ie(t)),t.table=null;break;case`thead_open`:t.table&&(t.table.inHeader=!0);break;case`thead_close`:t.table&&(t.table.inHeader=!1);break;case`tbody_open`:case`tbody_close`:break;case`tr_open`:t.table&&(t.table.currentRow=[]);break;case`tr_close`:t.table&&(t.table.inHeader?t.table.headers=t.table.currentRow:t.table.rows.push(t.table.currentRow),t.table.currentRow=[]);break;case`th_open`:case`td_open`:t.table&&(t.table.currentCell=De());break;case`th_close`:case`td_close`:t.table?.currentCell&&(t.table.currentRow.push(Ne(t.table.currentCell)),t.table.currentCell=null);break;case`hr`:t.text+=`───

`;break;default:n.children&&Y(n.children,t);break}}function X(e){for(let t=e.openStyles.length-1;t>=0;--t){let n=e.openStyles[t],r=e.text.length;r>n.start&&e.styles.push({start:n.start,end:r,style:n.style})}e.openStyles=[]}function Le(e,t){let n=[];for(let r of e){let e=Math.max(0,Math.min(r.start,t)),i=Math.max(e,Math.min(r.end,t));i>e&&n.push({start:e,end:i,style:r.style})}return n}function Re(e,t){let n=[];for(let r of e){let e=Math.max(0,Math.min(r.start,t)),i=Math.max(e,Math.min(r.end,t));i>e&&n.push({start:e,end:i,href:r.href})}return n}function Z(e){let t=[...e].toSorted((e,t)=>e.start===t.start?e.end===t.end?e.style.localeCompare(t.style):e.end-t.end:e.start-t.start),n=[];for(let e of t){let t=n[n.length-1];if(t&&t.style===e.style&&(e.start<t.end||e.start===t.end&&e.style!==`blockquote`)){t.end=Math.max(t.end,e.end);continue}n.push({...e})}return n}function Q(e,t,n){let r=Math.max(e.start,t),i=Math.min(e.end,n);return i<=r?null:{start:r,end:i}}function ze(e,t,n){if(e.length===0)return[];let r=[];for(let i of e){let e=Q(i,t,n);e&&r.push({start:e.start-t,end:e.end-t,style:i.style})}return Z(r)}function Be(e,t,n){if(e.length===0)return[];let r=[];for(let i of e){let e=Q(i,t,n);e&&r.push({start:e.start-t,end:e.end-t,href:i.href})}return r}function Ve(e,t={}){return $(e,t).ir}function $(e,t={}){let n={listStack:[]},r=we(t).parse(e??``,n);t.enableSpoilers&&Te(r);let i=t.tableMode??`off`,a={text:``,styles:[],openStyles:[],links:[],linkStack:[],env:n,headingStyle:t.headingStyle??`none`,blockquotePrefix:t.blockquotePrefix??``,enableSpoilers:t.enableSpoilers??!1,tableMode:i,table:null,hasTables:!1};Y(r,a),X(a);let o=a.text.trimEnd().length,s=0;for(let e of a.styles)e.style===`code_block`&&e.end>s&&(s=e.end);let c=Math.max(o,s);return{ir:{text:c===a.text.length?a.text:a.text.slice(0,c),styles:Z(Le(a.styles,c)),links:Re(a.links,c)},hasTables:a.hasTables}}function He(e,t){if(!e.text)return[];if(t<=0||e.text.length<=t)return[e];let n=D(e.text,t),r=[],i=0;return n.forEach((t,n)=>{if(!t)return;if(n>0)for(;i<e.text.length&&/\s/.test(e.text[i]??``);)i+=1;let a=i,o=Math.min(e.text.length,a+t.length);r.push({text:t,styles:ze(e.styles,a,o),links:Be(e.links,a,o)}),i=o}),r}export{S as _,xe as a,oe as c,ce as d,D as f,x as g,ie as h,N as i,T as l,ae as m,Ve as n,Se as o,se as p,$ as r,me as s,He as t,O as u,b as v};
