const e=require(`./paths-Dbz86fcW.cjs`),t=require(`./model-auth-AR9ykNRi.cjs`),n=require(`./fetch-timeout-Coc9xyC3.cjs`),r=require(`./ir-kv8ULOEg.cjs`),i=require(`./fetch-CZewCAvu.cjs`),a=require(`./outbound-attachment-CDo-hHC0.cjs`),o=1e4;function s(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 c(){let e=i.t();if(!e)throw Error(`fetch is not available`);return e}function l(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 u(e,r,i){let a=s(i.baseUrl),u=t.qn(),d=JSON.stringify({jsonrpc:`2.0`,method:e,params:r,id:u}),f=await n.n(`${a}/api/v1/rpc`,{method:`POST`,headers:{"Content-Type":`application/json`},body:d},i.timeoutMs??o,c());if(f.status===201)return;let p=await f.text();if(!p)throw Error(`Signal RPC empty response (status ${f.status})`);let m=l(p,f.status);if(m.error){let e=m.error.code??`unknown`,t=m.error.message??`Signal RPC error`;throw Error(`Signal RPC ${e}: ${t}`)}return m.result}async function d(e,t=o){let r=s(e);try{let e=await n.n(`${r}/api/v1/check`,{method:`GET`},t,c());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 f(e){let t=s(e.baseUrl),n=new URL(`${t}/api/v1/events`);e.account&&n.searchParams.set(`account`,e.account);let r=i.t();if(!r)throw Error(`fetch is not available`);let a=await r(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(),c=new TextDecoder,l=``,u={},d=()=>{!u.data&&!u.event&&!u.id||(e.onEvent({event:u.event,data:u.data,id:u.id}),u={})};for(;;){let{value:e,done:t}=await o.read();if(t)break;l+=c.decode(e,{stream:!0});let n=l.indexOf(`
`);for(;n!==-1;){let e=l.slice(0,n);if(l=l.slice(n+1),e.endsWith(`\r`)&&(e=e.slice(0,-1)),e===``){d(),n=l.indexOf(`
`);continue}if(e.startsWith(`:`)){n=l.indexOf(`
`);continue}let[t,...r]=e.split(`:`),i=t.trim(),a=r.join(`:`),o=a.startsWith(` `)?a.slice(1):a;i===`event`?u.event=o:i===`data`?u.data=u.data?`${u.data}\n${o}`:o:i===`id`&&(u.id=o),n=l.indexOf(`
`)}}d()}function p(e,n){let r=!!e.baseUrl?.trim(),i=!!e.account?.trim(),a=n||(!r||!i?t.ds({cfg:t.Si(),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 m(e){let t=e.toLowerCase();return t=t.replace(/^https?:\/\//,``),t=t.replace(/^www\./,``),t=t.replace(/\/+$/,``),t}function h(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 g(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 _(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 v(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 y(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(),o=t.slice(e.start,e.end).trim();if(n)if(!o)r+=n,a.push({pos:e.end,length:n.length});else{let t=m(o),i=n;if(n.startsWith(`mailto:`)&&(i=n.slice(7)),t!==m(i)){let t=` (${n})`;r+=t,a.push({pos:e.end,length:t.length})}}i=e.end}r+=t.slice(i);let o=v(e.styles.map(e=>{let t=h(e.style);return t?{start:e.start,end:e.end,style:t}:null}).filter(e=>e!==null),a),s=r.trimEnd(),c=s.length;return{text:s,styles:g(_(o.map(e=>({start:e.start,length:e.end-e.start,style:e.style})),c))}}function b(e,t={}){return y(r.n(e??``,{linkify:!0,enableSpoilers:!0,headingStyle:`bold`,blockquotePrefix:`> `,tableMode:t.tableMode}))}function x(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 S(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:g(x(r,o,o+e.length))});break}let e=C(a.slice(0,t));e<=0&&(e=t);let s=a.slice(0,e).trimEnd();s.length>0&&i.push({text:s,styles:g(x(r,o,o+s.length))});let c=e<a.length&&/\s/.test(a[e]),l=Math.min(a.length,e+(c?1:0));a=a.slice(l).trimStart(),o=n.length-a.length}return i}function C(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 w(e,t,n={}){let i=r.t(r.n(e??``,{linkify:!0,enableSpoilers:!0,headingStyle:`bold`,blockquotePrefix:`> `,tableMode:n.tableMode}),t),a=[];for(let e of i){let n=y(e);n.text.length>t?a.push(...S(n,t)):a.push(n)}return a}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,i,o={}){let s=o.cfg??t.Si(),c=t.ds({cfg:s,accountId:o.accountId}),{baseUrl:l,account:d}=p(o,c),f=T(n),m=i??``,h=!1,g=[],_=o.textMode??`markdown`,v=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,y;if(o.mediaUrl?.trim()){let t=await a.t(o.mediaUrl.trim(),v,{localRoots:o.mediaLocalRoots});y=[t.path];let n=e.g(t.contentType??void 0);!m&&n&&(m=n===`image`?`<media:image>`:`<media:${n}>`,h=!0)}if(m.trim()&&!h)if(_===`plain`)g=o.textStyles??[];else{let e=r.i({cfg:s,channel:`signal`,accountId:c.accountId}),t=b(m,{tableMode:e});m=t.text,g=t.styles}if(!m.trim()&&(!y||y.length===0))throw Error(`Signal send requires text or media`);let x={message:m};g.length>0&&(x[`text-style`]=g.map(e=>`${e.start}:${e.length}:${e.style}`)),d&&(x.account=d),y&&y.length>0&&(x.attachments=y);let S=E(f,{recipient:!0,group:!0,username:!0});if(!S)throw Error(`Signal recipient is required`);Object.assign(x,S);let C=(await u(`send`,x,{baseUrl:l,timeoutMs:o.timeoutMs}))?.timestamp;return{messageId:C?String(C):`unknown`,timestamp:C}}async function O(e,t={}){let{baseUrl:n,account:r}=p(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 u(`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}=p(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 u(`sendReceipt`,o,{baseUrl:r,timeoutMs:n.timeoutMs}),!0}Object.defineProperty(exports,`a`,{enumerable:!0,get:function(){return p}}),Object.defineProperty(exports,`c`,{enumerable:!0,get:function(){return f}}),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 d}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return O}}),Object.defineProperty(exports,`s`,{enumerable:!0,get:function(){return u}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return D}});
