import fastStringWidth from"../../../../fast-string-width@3.0.2/node_modules/fast-string-width/dist/index.js";const ANSI_ESCAPE_LINK=`]8;;`,GROUP_REGEX=RegExp(`(?:\\[(?<code>\\d+)m|\\${ANSI_ESCAPE_LINK}(?<uri>.*))`,`y`),getClosingCode=e=>{if(e>=30&&e<=37||e>=90&&e<=97)return 39;if(e>=40&&e<=47||e>=100&&e<=107)return 49;if(e===1||e===2)return 22;if(e===3)return 23;if(e===4)return 24;if(e===7)return 27;if(e===8)return 28;if(e===9)return 29;if(e===0)return 0},wrapAnsiCode=e=>`[${e}m`,wrapAnsiHyperlink=e=>`${ANSI_ESCAPE_LINK}${e}`,wrapWord=(n,r,i)=>{let a=r[Symbol.iterator](),o=!1,s=!1,c=n.at(-1),l=c===void 0?0:fastStringWidth(c),u=a.next(),d=a.next(),f=0;for(;!u.done;){let c=u.value,p=fastStringWidth(c);l+p<=i?n[n.length-1]+=c:(n.push(c),l=0),(c===`\x1B`||c===``)&&(o=!0,s=r.startsWith(ANSI_ESCAPE_LINK,f+1)),o?s?c===`\x07`&&(o=!1,s=!1):c===`m`&&(o=!1):(l+=p,l===i&&!d.done&&(n.push(``),l=0)),u=d,d=a.next(),f+=c.length}c=n.at(-1),!l&&c!==void 0&&c.length&&n.length>1&&(n[n.length-2]+=n.pop())},stringVisibleTrimSpacesRight=t=>{let n=t.split(` `),r=n.length;for(;r&&!fastStringWidth(n[r-1]);)r--;return r===n.length?t:n.slice(0,r).join(` `)+n.slice(r).join(``)},exec=(t,r,i={})=>{if(i.trim!==!1&&t.trim()===``)return``;let a=``,o,s,c=t.split(` `),l=[``],u=0;for(let t=0;t<c.length;t++){let n=c[t];if(i.trim!==!1){let t=l.at(-1)??``,n=t.trimStart();t.length!==n.length&&(l[l.length-1]=n,u=fastStringWidth(n))}t!==0&&(u>=r&&(i.wordWrap===!1||i.trim===!1)&&(l.push(``),u=0),(u||i.trim===!1)&&(l[l.length-1]+=` `,u++));let a=fastStringWidth(n);if(i.hard&&a>r){let t=r-u,i=1+Math.floor((a-t-1)/r);Math.floor((a-1)/r)<i&&l.push(``),wrapWord(l,n,r),u=fastStringWidth(l.at(-1)??``);continue}if(u+a>r&&u&&a){if(i.wordWrap===!1&&u<r){wrapWord(l,n,r),u=fastStringWidth(l.at(-1)??``);continue}l.push(``),u=0}if(u+a>r&&i.wordWrap===!1){wrapWord(l,n,r),u=fastStringWidth(l.at(-1)??``);continue}l[l.length-1]+=n,u+=a}i.trim!==!1&&(l=l.map(e=>stringVisibleTrimSpacesRight(e)));let d=l.join(`
`),f=!1;for(let e=0;e<d.length;e++){let t=d[e];if(a+=t,f)f=!1;else if(f=t>=`\ud800`&&t<=`\udbff`,f)continue;if(t===`\x1B`||t===``){GROUP_REGEX.lastIndex=e+1;let t=GROUP_REGEX.exec(d)?.groups;if(t?.code!==void 0){let e=Number.parseFloat(t.code);o=e===39?void 0:e}else t?.uri!==void 0&&(s=t.uri.length===0?void 0:t.uri)}if(d[e+1]===`
`){s&&(a+=wrapAnsiHyperlink(``));let e=o?getClosingCode(o):void 0;o&&e&&(a+=wrapAnsiCode(e))}else t===`
`&&(o&&getClosingCode(o)&&(a+=wrapAnsiCode(o)),s&&(a+=wrapAnsiHyperlink(s)))}return a},CRLF_OR_LF=/\r?\n/;function wrapAnsi(e,t,n){return String(e).normalize().split(CRLF_OR_LF).map(e=>exec(e,t,n)).join(`
`)}export{wrapAnsi};