import{g as e}from"./paths-C1weIcCE.js";import{Y as t,ci as n,ho as r}from"./sessions-BhoLcf5b.js";import{n as i}from"./fetch-timeout-CLw1U7VS.js";import{t as a}from"./fetch-CCLWsYaQ.js";import{n as o,s,t as c}from"./ir-D8z2qRSr.js";import{t as l}from"./outbound-attachment-DPGhcaxv.js";function u(e){let t=e.toLowerCase();return t=t.replace(/^https?:\/\//,``),t=t.replace(/^www\./,``),t=t.replace(/\/+$/,``),t}function d(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 f(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 p(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 m(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 h(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=u(o),i=n;if(n.startsWith(`mailto:`)&&(i=n.slice(7)),t!==u(i)){let t=` (${n})`;r+=t,a.push({pos:e.end,length:t.length})}}i=e.end}r+=t.slice(i);let o=m(e.styles.map(e=>{let t=d(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:f(p(o.map(e=>({start:e.start,length:e.end-e.start,style:e.style})),c))}}function g(e,t={}){return h(o(e??``,{linkify:!0,enableSpoilers:!0,headingStyle:`bold`,blockquotePrefix:`> `,tableMode:t.tableMode}))}function _(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 v(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:f(_(r,o,o+e.length))});break}let e=y(a.slice(0,t));e<=0&&(e=t);let s=a.slice(0,e).trimEnd();s.length>0&&i.push({text:s,styles:f(_(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 y(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 b(e,t,n={}){let r=c(o(e??``,{linkify:!0,enableSpoilers:!0,headingStyle:`bold`,blockquotePrefix:`> `,tableMode:n.tableMode}),t),i=[];for(let e of r){let n=h(e);n.text.length>t?i.push(...v(n,t)):i.push(n)}return i}const x=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=a();if(!e)throw Error(`fetch is not available`);return e}function w(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 T(e,t,r){let a=S(r.baseUrl),o=n(),s=JSON.stringify({jsonrpc:`2.0`,method:e,params:t,id:o}),c=await i(`${a}/api/v1/rpc`,{method:`POST`,headers:{"Content-Type":`application/json`},body:s},r.timeoutMs??x,C());if(c.status===201)return;let l=await c.text();if(!l)throw Error(`Signal RPC empty response (status ${c.status})`);let u=w(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 E(e,t=x){let n=S(e);try{let e=await i(`${n}/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 D(e){let t=S(e.baseUrl),n=new URL(`${t}/api/v1/events`);e.account&&n.searchParams.set(`account`,e.account);let r=a();if(!r)throw Error(`fetch is not available`);let i=await r(n,{method:`GET`,headers:{Accept:`text/event-stream`},signal:e.abortSignal});if(!i.ok||!i.body)throw Error(`Signal SSE failed (${i.status} ${i.statusText||`error`})`);let o=i.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 O(e,n){let i=!!e.baseUrl?.trim(),a=!!e.account?.trim(),o=n||(!i||!a?r({cfg:t(),accountId:e.accountId}):void 0),s=e.baseUrl?.trim()||o?.baseUrl;if(!s)throw Error(`Signal base URL is required`);return{baseUrl:s,account:e.account?.trim()||o?.config.account?.trim()}}function k(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 A(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 j(n,i,a={}){let o=a.cfg??t(),c=r({cfg:o,accountId:a.accountId}),{baseUrl:u,account:d}=O(a,c),f=k(n),p=i??``,m=!1,h=[],_=a.textMode??`markdown`,v=typeof a.maxBytes==`number`?a.maxBytes:typeof c.config.mediaMaxMb==`number`?c.config.mediaMaxMb*1024*1024:typeof o.agents?.defaults?.mediaMaxMb==`number`?o.agents.defaults.mediaMaxMb*1024*1024:8*1024*1024,y;if(a.mediaUrl?.trim()){let t=await l(a.mediaUrl.trim(),v,{localRoots:a.mediaLocalRoots});y=[t.path];let n=e(t.contentType??void 0);!p&&n&&(p=n===`image`?`<media:image>`:`<media:${n}>`,m=!0)}if(p.trim()&&!m)if(_===`plain`)h=a.textStyles??[];else{let e=s({cfg:o,channel:`signal`,accountId:c.accountId}),t=g(p,{tableMode:e});p=t.text,h=t.styles}if(!p.trim()&&(!y||y.length===0))throw Error(`Signal send requires text or media`);let b={message:p};h.length>0&&(b[`text-style`]=h.map(e=>`${e.start}:${e.length}:${e.style}`)),d&&(b.account=d),y&&y.length>0&&(b.attachments=y);let x=A(f,{recipient:!0,group:!0,username:!0});if(!x)throw Error(`Signal recipient is required`);Object.assign(b,x);let S=(await T(`send`,b,{baseUrl:u,timeoutMs:a.timeoutMs}))?.timestamp;return{messageId:S?String(S):`unknown`,timestamp:S}}async function M(e,t={}){let{baseUrl:n,account:r}=O(t),i=A(k(e),{recipient:!0,group:!0});if(!i)return!1;let a={...i};return r&&(a.account=r),t.stop&&(a.stop=!0),await T(`sendTyping`,a,{baseUrl:n,timeoutMs:t.timeoutMs}),!0}async function N(e,t,n={}){if(!Number.isFinite(t)||t<=0)return!1;let{baseUrl:r,account:i}=O(n),a=A(k(e),{recipient:!0});if(!a)return!1;let o={...a,targetTimestamp:t,type:n.type??`read`};return i&&(o.account=i),await T(`sendReceipt`,o,{baseUrl:r,timeoutMs:n.timeoutMs}),!0}export{E as a,b as c,O as i,N as n,T as o,M as r,D as s,j as t};
