const e=require(`./paths-a2-BgYPx.cjs`),t=require(`./sessions-Bc5xPyOc.cjs`),n=require(`./fetch-timeout-BygUD0rd.cjs`),r=require(`./fetch-ClKErpni.cjs`),i=require(`./ir-sz96fofW.cjs`),a=require(`./outbound-attachment-D8I-hfa8.cjs`);function o(e){let t=e.toLowerCase();return t=t.replace(/^https?:\/\//,``),t=t.replace(/^www\./,``),t=t.replace(/\/+$/,``),t}function s(e){switch(e){case`bold`:return`BOLD`;case`italic`:return`ITALIC`;case`strikethrough`:return`STRIKETHROUGH`;case`code`:case`code_block`:return`MONOSPACE`;case`spoiler`:return`SPOILER`;default:return null}}function c(e){let t=[...e].toSorted((e,t)=>e.start===t.start?e.length===t.length?e.style.localeCompare(t.style):e.length-t.length: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.start+t.length){let n=t.start+t.length;t.length=Math.max(n,e.start+e.length)-t.start;continue}n.push({...e})}return n}function l(e,t){let n=[];for(let r of e){let e=Math.max(0,Math.min(r.start,t)),i=Math.min(r.start+r.length,t)-e;i>0&&n.push({start:e,length:i,style:r.style})}return n}function u(e,t){if(t.length===0)return e;let n=[...t].toSorted((e,t)=>e.pos-t.pos),r=e,i=0;for(let e of n){let t=e.pos+i,n=[];for(let i of r){if(i.end<=t){n.push(i);continue}if(i.start>=t){n.push({start:i.start+e.length,end:i.end+e.length,style:i.style});continue}if(i.start<t&&i.end>t){t>i.start&&n.push({start:i.start,end:t,style:i.style});let r=t+e.length,a=i.end+e.length;a>r&&n.push({start:r,end:a,style:i.style})}}r=n,i+=e.length}return r}function d(e){let t=e.text??``;if(!t)return{text:``,styles:[]};let n=[...e.links].toSorted((e,t)=>e.start-t.start),r=``,i=0,a=[];for(let e of n){if(e.start<i)continue;r+=t.slice(i,e.end);let n=e.href.trim(),s=t.slice(e.start,e.end).trim();if(n)if(!s)r+=n,a.push({pos:e.end,length:n.length});else{let t=o(s),i=n;if(n.startsWith(`mailto:`)&&(i=n.slice(7)),t!==o(i)){let t=` (${n})`;r+=t,a.push({pos:e.end,length:t.length})}}i=e.end}r+=t.slice(i);let d=u(e.styles.map(e=>{let t=s(e.style);return t?{start:e.start,end:e.end,style:t}:null}).filter(e=>e!==null),a),f=r.trimEnd(),p=f.length;return{text:f,styles:c(l(d.map(e=>({start:e.start,length:e.end-e.start,style:e.style})),p))}}function f(e,t={}){return d(i.n(e??``,{linkify:!0,enableSpoilers:!0,headingStyle:`bold`,blockquotePrefix:`> `,tableMode:t.tableMode}))}function p(e,t,n){let r=[];for(let i of e){let e=i.start+i.length,a=Math.max(i.start,t),o=Math.min(e,n);o>a&&r.push({start:a-t,length:o-a,style:i.style})}return r}function m(e,t){let{text:n,styles:r}=e;if(n.length<=t)return[e];let i=[],a=n,o=0;for(;a.length>0;){if(a.length<=t){let e=a.trimEnd();e.length>0&&i.push({text:e,styles:c(p(r,o,o+e.length))});break}let e=h(a.slice(0,t));e<=0&&(e=t);let s=a.slice(0,e).trimEnd();s.length>0&&i.push({text:s,styles:c(p(r,o,o+s.length))});let l=e<a.length&&/\s/.test(a[e]),u=Math.min(a.length,e+(l?1:0));a=a.slice(u).trimStart(),o=n.length-a.length}return i}function h(e){let t=-1,n=-1,r=0;for(let i=0;i<e.length;i++){let a=e[i];if(a===`(`){r++;continue}if(a===`)`&&r>0){r--;continue}r===0&&(a===`
`?t=i:/\s/.test(a)&&(n=i))}return t>0?t:n}function g(e,t,n={}){let r=i.t(i.n(e??``,{linkify:!0,enableSpoilers:!0,headingStyle:`bold`,blockquotePrefix:`> `,tableMode:n.tableMode}),t),a=[];for(let e of r){let n=d(e);n.text.length>t?a.push(...m(n,t)):a.push(n)}return a}const _=1e4;function v(e){let t=e.trim();if(!t)throw Error(`Signal base URL is required`);return/^https?:\/\//i.test(t)?t.replace(/\/+$/,``):`http://${t}`.replace(/\/+$/,``)}function y(){let e=r.t();if(!e)throw Error(`fetch is not available`);return e}function b(e,t){let n;try{n=JSON.parse(e)}catch(e){throw Error(`Signal RPC returned malformed JSON (status ${t})`,{cause:e})}if(!n||typeof n!=`object`)throw Error(`Signal RPC returned invalid response envelope (status ${t})`);let r=n,i=Object.hasOwn(r,`result`);if(!r.error&&!i)throw Error(`Signal RPC returned invalid response envelope (status ${t})`);return r}async function x(e,r,i){let a=v(i.baseUrl),o=t.li(),s=JSON.stringify({jsonrpc:`2.0`,method:e,params:r,id:o}),c=await n.n(`${a}/api/v1/rpc`,{method:`POST`,headers:{"Content-Type":`application/json`},body:s},i.timeoutMs??_,y());if(c.status===201)return;let l=await c.text();if(!l)throw Error(`Signal RPC empty response (status ${c.status})`);let u=b(l,c.status);if(u.error){let e=u.error.code??`unknown`,t=u.error.message??`Signal RPC error`;throw Error(`Signal RPC ${e}: ${t}`)}return u.result}async function S(e,t=_){let r=v(e);try{let e=await n.n(`${r}/api/v1/check`,{method:`GET`},t,y());return e.ok?{ok:!0,status:e.status,error:null}:{ok:!1,status:e.status,error:`HTTP ${e.status}`}}catch(e){return{ok:!1,status:null,error:e instanceof Error?e.message:String(e)}}}async function C(e){let t=v(e.baseUrl),n=new URL(`${t}/api/v1/events`);e.account&&n.searchParams.set(`account`,e.account);let i=r.t();if(!i)throw Error(`fetch is not available`);let a=await i(n,{method:`GET`,headers:{Accept:`text/event-stream`},signal:e.abortSignal});if(!a.ok||!a.body)throw Error(`Signal SSE failed (${a.status} ${a.statusText||`error`})`);let o=a.body.getReader(),s=new TextDecoder,c=``,l={},u=()=>{!l.data&&!l.event&&!l.id||(e.onEvent({event:l.event,data:l.data,id:l.id}),l={})};for(;;){let{value:e,done:t}=await o.read();if(t)break;c+=s.decode(e,{stream:!0});let n=c.indexOf(`
`);for(;n!==-1;){let e=c.slice(0,n);if(c=c.slice(n+1),e.endsWith(`\r`)&&(e=e.slice(0,-1)),e===``){u(),n=c.indexOf(`
`);continue}if(e.startsWith(`:`)){n=c.indexOf(`
`);continue}let[t,...r]=e.split(`:`),i=t.trim(),a=r.join(`:`),o=a.startsWith(` `)?a.slice(1):a;i===`event`?l.event=o:i===`data`?l.data=l.data?`${l.data}\n${o}`:o:i===`id`&&(l.id=o),n=c.indexOf(`
`)}}u()}function w(e,n){let r=!!e.baseUrl?.trim(),i=!!e.account?.trim(),a=n||(!r||!i?t.yo({cfg:t.Y(),accountId:e.accountId}):void 0),o=e.baseUrl?.trim()||a?.baseUrl;if(!o)throw Error(`Signal base URL is required`);return{baseUrl:o,account:e.account?.trim()||a?.config.account?.trim()}}function T(e){let t=e.trim();if(!t)throw Error(`Signal recipient is required`);t.toLowerCase().startsWith(`signal:`)&&(t=t.slice(7).trim());let n=t.toLowerCase();return n.startsWith(`group:`)?{type:`group`,groupId:t.slice(6).trim()}:n.startsWith(`username:`)?{type:`username`,username:t.slice(9).trim()}:n.startsWith(`u:`)?{type:`username`,username:t.trim()}:{type:`recipient`,recipient:t}}function E(e,t){return e.type===`recipient`?t.recipient?{recipient:[e.recipient]}:null:e.type===`group`?t.group?{groupId:e.groupId}:null:e.type===`username`&&t.username?{username:[e.username]}:null}async function D(n,r,o={}){let s=o.cfg??t.Y(),c=t.yo({cfg:s,accountId:o.accountId}),{baseUrl:l,account:u}=w(o,c),d=T(n),p=r??``,m=!1,h=[],g=o.textMode??`markdown`,_=typeof o.maxBytes==`number`?o.maxBytes:typeof c.config.mediaMaxMb==`number`?c.config.mediaMaxMb*1024*1024:typeof s.agents?.defaults?.mediaMaxMb==`number`?s.agents.defaults.mediaMaxMb*1024*1024:8*1024*1024,v;if(o.mediaUrl?.trim()){let t=await a.t(o.mediaUrl.trim(),_,{localRoots:o.mediaLocalRoots});v=[t.path];let n=e.g(t.contentType??void 0);!p&&n&&(p=n===`image`?`<media:image>`:`<media:${n}>`,m=!0)}if(p.trim()&&!m)if(g===`plain`)h=o.textStyles??[];else{let e=i.s({cfg:s,channel:`signal`,accountId:c.accountId}),t=f(p,{tableMode:e});p=t.text,h=t.styles}if(!p.trim()&&(!v||v.length===0))throw Error(`Signal send requires text or media`);let y={message:p};h.length>0&&(y[`text-style`]=h.map(e=>`${e.start}:${e.length}:${e.style}`)),u&&(y.account=u),v&&v.length>0&&(y.attachments=v);let b=E(d,{recipient:!0,group:!0,username:!0});if(!b)throw Error(`Signal recipient is required`);Object.assign(y,b);let S=(await x(`send`,y,{baseUrl:l,timeoutMs:o.timeoutMs}))?.timestamp;return{messageId:S?String(S):`unknown`,timestamp:S}}async function O(e,t={}){let{baseUrl:n,account:r}=w(t),i=E(T(e),{recipient:!0,group:!0});if(!i)return!1;let a={...i};return r&&(a.account=r),t.stop&&(a.stop=!0),await x(`sendTyping`,a,{baseUrl:n,timeoutMs:t.timeoutMs}),!0}async function k(e,t,n={}){if(!Number.isFinite(t)||t<=0)return!1;let{baseUrl:r,account:i}=w(n),a=E(T(e),{recipient:!0});if(!a)return!1;let o={...a,targetTimestamp:t,type:n.type??`read`};return i&&(o.account=i),await x(`sendReceipt`,o,{baseUrl:r,timeoutMs:n.timeoutMs}),!0}Object.defineProperty(exports,`a`,{enumerable:!0,get:function(){return S}}),Object.defineProperty(exports,`c`,{enumerable:!0,get:function(){return g}}),Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return w}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return k}}),Object.defineProperty(exports,`o`,{enumerable:!0,get:function(){return x}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return O}}),Object.defineProperty(exports,`s`,{enumerable:!0,get:function(){return C}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return D}});
