import e from"node:fs";import t from"node:os";import n from"node:path";function r(e){return e?.trim()||void 0}function i(e=process.env,r=t.homedir){let i=a(e,r);return i?n.resolve(i):void 0}function a(e,t){let n=r(e.OPENCLAW_HOME);if(n){if(n===`~`||n.startsWith(`~/`)||n.startsWith(`~\\`)){let i=r(e.HOME)??r(e.USERPROFILE)??o(t);return i?n.replace(/^~(?=$|[\\/])/,i):void 0}return n}return r(e.HOME)||r(e.USERPROFILE)||o(t)}function o(e){try{return r(e())}catch{return}}function s(e=process.env,r=t.homedir){return i(e,r)??n.resolve(process.cwd())}function c(e,n){if(!e.startsWith(`~`))return e;let a=r(n?.home)??i(n?.env??process.env,n?.homedir??t.homedir);return a?e.replace(/^~(?=$|[\\/])/,a):e}function l(e=process.env){return e.OPENCLAW_NIX_MODE===`1`}const u=l(),d=[`.clawdbot`,`.moldbot`,`.moltbot`],f=`.openclaw-qbot.json`,p=[`openclaw.json`,`clawdbot.json`,`moldbot.json`,`moltbot.json`];function m(){return s(process.env,t.homedir)}function h(e){return()=>s(e,t.homedir)}function g(e=m){return d.map(t=>n.join(e(),t))}function _(e=m){return n.join(e(),`.qbotclaw`)}function v(e=m){return g(e)}function y(e=m){return _(e)}function b(t=process.env,n=h(t)){let r=()=>s(t,n),i=t.OPENCLAW_STATE_DIR?.trim()||t.CLAWDBOT_STATE_DIR?.trim();if(i)return x(i,t,r);let a=_(r);if(t.OPENCLAW_TEST_FAST===`1`)return a;let o=g(r);return e.existsSync(a)?a:o.find(t=>{try{return e.existsSync(t)}catch{return!1}})||a}function x(e,t=process.env,r=h(t)){let i=e.trim();if(!i)return i;if(i.startsWith(`~`)){let e=c(i,{home:s(t,r),env:t,homedir:r});return n.resolve(e)}return n.resolve(i)}const S=b();function C(e=process.env,t=b(e,h(e))){let r=e.OPENCLAW_CONFIG_PATH?.trim()||e.CLAWDBOT_CONFIG_PATH?.trim();return r?x(r,e,h(e)):n.join(t,f)}function w(t=process.env,n=h(t)){return t.OPENCLAW_TEST_FAST===`1`?C(t,b(t,n)):D(t,n).find(t=>{try{return e.existsSync(t)}catch{return!1}})||C(t,b(t,n))}function T(t=process.env,r=b(t,h(t)),i=h(t)){let a=t.OPENCLAW_CONFIG_PATH?.trim();if(a)return x(a,t,i);if(t.OPENCLAW_TEST_FAST===`1`)return n.join(r,f);let o=t.OPENCLAW_STATE_DIR?.trim(),s=[n.join(r,f),...p.map(e=>n.join(r,e))].find(t=>{try{return e.existsSync(t)}catch{return!1}});if(s)return s;if(o)return n.join(r,f);let c=b(t,i);return n.resolve(r)===n.resolve(c)?w(t,i):n.join(r,f)}const E=w();function D(e=process.env,t=h(e)){let r=()=>s(e,t),i=e.OPENCLAW_CONFIG_PATH?.trim()||e.CLAWDBOT_CONFIG_PATH?.trim();if(i)return[x(i,e,r)];let a=[],o=e.OPENCLAW_STATE_DIR?.trim()||e.CLAWDBOT_STATE_DIR?.trim();if(o){let t=x(o,e,r);a.push(n.join(t,f)),a.push(...p.map(e=>n.join(t,e)))}let c=[_(r),...g(r)];for(let e of c)a.push(n.join(e,f)),a.push(...p.map(t=>n.join(e,t)));return a}const O=18789;function k(e=t.tmpdir){let r=e(),i=typeof process.getuid==`function`?process.getuid():void 0,a=i==null?`openclaw`:`openclaw-${i}`;return n.join(r,a)}function A(e=process.env,t=b(e,h(e))){let r=e.OPENCLAW_OAUTH_DIR?.trim();return r?x(r,e,h(e)):n.join(t,`credentials`)}function j(e=process.env,t=b(e,h(e))){return n.join(A(e,t),`oauth.json`)}function M(e,t=process.env){let n=t.OPENCLAW_GATEWAY_PORT?.trim()||t.CLAWDBOT_GATEWAY_PORT?.trim();if(n){let e=Number.parseInt(n,10);if(Number.isFinite(e)&&e>=0)return e}let r=e?.gateway?.port;return typeof r==`number`&&Number.isFinite(r)&&r>=0?r:O}export{c as _,C as a,D as c,l as d,v as f,b as g,j as h,u as i,k as l,A as m,O as n,T as o,y as p,S as r,w as s,E as t,M as u,i as v,s as y};
