import{_ as e,m as t,v as n,y as r}from"./paths-B4IRk3wi.js";import{S as i,T as a}from"./subsystem-BQ1073Mu.js";import o from"node:fs";import s from"node:os";import c from"node:path";function l(e){return typeof e==`object`&&!!e&&!Array.isArray(e)&&Object.prototype.toString.call(e)===`[object Object]`}async function u(e){await o.promises.mkdir(e,{recursive:!0})}async function d(e){try{return await o.promises.access(e),!0}catch{return!1}}function f(e,t,n){return Math.max(t,Math.min(n,e))}function p(e,t,n){return f(Math.floor(e),t,n)}const m=f;function h(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}function g(e){try{return JSON.parse(e)}catch{return null}}function _(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function v(e){if(e!==`web`)throw Error(`Web channel must be 'web'`)}function y(e){let t=e.replace(/^whatsapp:/,``).trim().replace(/[^\d+]/g,``);return t.startsWith(`+`)?`+${t.slice(1)}`:`+${t}`}function b(e,t){if(!e||!Array.isArray(t)||t.length===0)return!1;let n=y(e);return t.some(e=>{if(e===`*`)return!1;try{return y(String(e))===n}catch{return!1}})}function x(e){let t=e.replace(/^whatsapp:/,``).trim();return t.includes(`@`)?t:`${y(t).replace(/\D/g,``)}@s.whatsapp.net`}function S(e){let n=new Set,r=e=>{e&&n.add(j(e))};r(e?.authDir);for(let t of e?.lidMappingDirs??[])r(t);return r(t()),r(c.join(B,`credentials`)),[...n]}function C(e,t){let n=`lid-mapping-${e}_reverse.json`,r=S(t);for(let e of r){let t=c.join(e,n);try{let e=o.readFileSync(t,`utf8`),n=JSON.parse(e);if(n==null)continue;return y(String(n))}catch{}}return null}function w(e,t){let n=e.match(/^(\d+)(?::\d+)?@(s\.whatsapp\.net|hosted)$/);if(n)return`+${n[1]}`;let r=e.match(/^(\d+)(?::\d+)?@(lid|hosted\.lid)$/);if(r){let e=r[1],n=C(e,t);if(n)return n;(t?.logMissing??a())&&i(`LID mapping not found for ${e}; skipping inbound message`)}return null}async function T(e,t){if(!e)return null;let n=w(e,t);if(n)return n;if(!/(@lid|@hosted\.lid)$/.test(e)||!t?.lidLookup?.getPNForLID)return null;try{let n=await t.lidLookup.getPNForLID(e);return n?w(n,t):null}catch(t){return a()&&i(`LID mapping lookup failed for ${e}: ${String(t)}`),null}}function E(e){return new Promise(t=>setTimeout(t,e))}function D(e){return e>=55296&&e<=56319}function O(e){return e>=56320&&e<=57343}function k(e,t,n){let r=e.length,i=t<0?Math.max(r+t,0):Math.min(t,r),a=n===void 0?r:n<0?Math.max(r+n,0):Math.min(n,r);if(a<i){let e=i;i=a,a=e}return i>0&&i<r&&O(e.charCodeAt(i))&&D(e.charCodeAt(i-1))&&(i+=1),a>0&&a<r&&D(e.charCodeAt(a-1))&&O(e.charCodeAt(a))&&--a,e.slice(i,a)}function A(e,t){let n=Math.max(0,Math.floor(t));return e.length<=n?e:k(e,0,n)}function j(t){if(!t)return``;let n=t.trim();if(!n)return n;if(n.startsWith(`~`)){let t=e(n,{home:r(process.env,s.homedir),env:process.env,homedir:s.homedir});return c.resolve(t)}return c.resolve(n)}function M(e=process.env,t=s.homedir){let n=e.OPENCLAW_STATE_DIR?.trim()||e.CLAWDBOT_STATE_DIR?.trim();if(n)return j(n);let i=c.join(r(e,t),`.qbotclaw`);try{if(o.existsSync(i))return i}catch{}return i}function N(){return n(process.env,s.homedir)}function P(){let e=N();if(e)return process.env.OPENCLAW_HOME?.trim()?{home:e,prefix:`$OPENCLAW_HOME`}:{home:e,prefix:`~`}}function F(e){if(!e)return e;let t=P();if(!t)return e;let{home:n,prefix:r}=t;return e===n?r:e.startsWith(`${n}/`)||e.startsWith(`${n}\\`)?`${r}${e.slice(n.length)}`:e}function I(e){if(!e)return e;let t=P();return t?e.split(t.home).join(t.prefix):e}function L(e){return F(e)}function R(e){return I(e)}function z(e,t,n){let r=e.replaceAll(`\x1B`,``),i=t.replaceAll(`\x1B`,``);return n?.force===!0||n?.force!==!1&&process.stdout.isTTY?`\u001b]8;;${i}\u0007${r}\u001b]8;;\u0007`:n?.fallback??`${r} (${i})`}const B=M();export{E as C,l as D,A as E,F as S,x as T,N as _,f as a,g as b,u as c,_ as d,b as f,M as g,d as h,p as i,h as l,y as m,v as n,L as o,w as p,m as r,R as s,B as t,z as u,T as v,k as w,I as x,j as y};
