import{g as e}from"./paths-CAzfAyaR.js";import{Kn as t,ss as n,xi as r}from"./model-auth-Dm6LMYK2.js";import{n as i}from"./fetch-timeout-DkYpp08Q.js";import{i as a,n as o,t as s}from"./ir-cfe-38gS.js";import{t as c}from"./fetch-B1GR6-u4.js";import{t as l}from"./outbound-attachment-BHU9pkKA.js";const u=1e4;function d(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 f(){let e=c();if(!e)throw Error(`fetch is not available`);return e}function p(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 m(e,n,r){let a=d(r.baseUrl),o=t(),s=JSON.stringify({jsonrpc:`2.0`,method:e,params:n,id:o}),c=await i(`${a}/api/v1/rpc`,{method:`POST`,headers:{"Content-Type":`application/json`},body:s},r.timeoutMs??u,f());if(c.status===201)return;let l=await c.text();if(!l)throw Error(`Signal RPC empty response (status ${c.status})`);let m=p(l,c.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 h(e,t=u){let n=d(e);try{let e=await i(`${n}/api/v1/check`,{method:`GET`},t,f());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 g(e){let t=d(e.baseUrl),n=new URL(`${t}/api/v1/events`);e.account&&n.searchParams.set(`account`,e.account);let r=c();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 a=i.body.getReader(),o=new TextDecoder,s=``,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 a.read();if(t)break;s+=o.decode(e,{stream:!0});let n=s.indexOf(`
`);for(;n!==-1;){let e=s.slice(0,n);if(s=s.slice(n+1),e.endsWith(`\r`)&&(e=e.slice(0,-1)),e===``){u(),n=s.indexOf(`
`);continue}if(e.startsWith(`:`)){n=s.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=s.indexOf(`
`)}}u()}function _(e,t){let i=!!e.baseUrl?.trim(),a=!!e.account?.trim(),o=t||(!i||!a?n({cfg:r(),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 v(e){let t=e.toLowerCase();return t=t.replace(/^https?:\/\//,``),t=t.replace(/^www\./,``),t=t.replace(/\/+$/,``),t}function y(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 b(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 x(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 S(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 C(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=v(o),i=n;if(n.startsWith(`mailto:`)&&(i=n.slice(7)),t!==v(i)){let t=` (${n})`;r+=t,a.push({pos:e.end,length:t.length})}}i=e.end}r+=t.slice(i);let o=S(e.styles.map(e=>{let t=y(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:b(x(o.map(e=>({start:e.start,length:e.end-e.start,style:e.style})),c))}}function w(e,t={}){return C(o(e??``,{linkify:!0,enableSpoilers:!0,headingStyle:`bold`,blockquotePrefix:`> `,tableMode:t.tableMode}))}function T(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 E(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:b(T(r,o,o+e.length))});break}let e=D(a.slice(0,t));e<=0&&(e=t);let s=a.slice(0,e).trimEnd();s.length>0&&i.push({text:s,styles:b(T(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 D(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 O(e,t,n={}){let r=s(o(e??``,{linkify:!0,enableSpoilers:!0,headingStyle:`bold`,blockquotePrefix:`> `,tableMode:n.tableMode}),t),i=[];for(let e of r){let n=C(e);n.text.length>t?i.push(...E(n,t)):i.push(n)}return i}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(t,i,o={}){let s=o.cfg??r(),c=n({cfg:s,accountId:o.accountId}),{baseUrl:u,account:d}=_(o,c),f=k(t),p=i??``,h=!1,g=[],v=o.textMode??`markdown`,y=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,b;if(o.mediaUrl?.trim()){let t=await l(o.mediaUrl.trim(),y,{localRoots:o.mediaLocalRoots});b=[t.path];let n=e(t.contentType??void 0);!p&&n&&(p=n===`image`?`<media:image>`:`<media:${n}>`,h=!0)}if(p.trim()&&!h)if(v===`plain`)g=o.textStyles??[];else{let e=a({cfg:s,channel:`signal`,accountId:c.accountId}),t=w(p,{tableMode:e});p=t.text,g=t.styles}if(!p.trim()&&(!b||b.length===0))throw Error(`Signal send requires text or media`);let x={message:p};g.length>0&&(x[`text-style`]=g.map(e=>`${e.start}:${e.length}:${e.style}`)),d&&(x.account=d),b&&b.length>0&&(x.attachments=b);let S=A(f,{recipient:!0,group:!0,username:!0});if(!S)throw Error(`Signal recipient is required`);Object.assign(x,S);let C=(await m(`send`,x,{baseUrl:u,timeoutMs:o.timeoutMs}))?.timestamp;return{messageId:C?String(C):`unknown`,timestamp:C}}async function M(e,t={}){let{baseUrl:n,account:r}=_(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 m(`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}=_(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 m(`sendReceipt`,o,{baseUrl:r,timeoutMs:n.timeoutMs}),!0}export{_ as a,g as c,O as i,N as n,h as o,M as r,m as s,j as t};
