import e from"node:fs";import t from"node:path";import n from"node:os";function r(e){return e?.trim()||void 0}function i(e=process.env,r=n.homedir){let i=a(e,r);return i?t.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=n.homedir){return i(e,r)??t.resolve(process.cwd())}function c(e,t){if(!e.startsWith(`~`))return e;let a=r(t?.home)??i(t?.env??process.env,t?.homedir??n.homedir);return a?e.replace(/^~(?=$|[\\/])/,a):e}function l(e=process.env){return e.OPENCLAW_NIX_MODE===`1`}l();const u=[`.clawdbot`,`.moldbot`,`.moltbot`],d=`.openclaw-qbot.json`,f=[`openclaw.json`,`clawdbot.json`,`moldbot.json`,`moltbot.json`];function p(){return s(process.env,n.homedir)}function m(e){return()=>s(e,n.homedir)}function h(e=p){return u.map(n=>t.join(e(),n))}function g(e=p){return t.join(e(),`.qbotclaw`)}function _(t=process.env,n=m(t)){let r=()=>s(t,n),i=t.OPENCLAW_STATE_DIR?.trim()||t.CLAWDBOT_STATE_DIR?.trim();if(i)return v(i,t,r);let a=g(r);if(t.OPENCLAW_TEST_FAST===`1`)return a;let o=h(r);return e.existsSync(a)?a:o.find(t=>{try{return e.existsSync(t)}catch{return!1}})||a}function v(e,n=process.env,r=m(n)){let i=e.trim();if(!i)return i;if(i.startsWith(`~`)){let e=c(i,{home:s(n,r),env:n,homedir:r});return t.resolve(e)}return t.resolve(i)}const y=_();function b(e=process.env,n=_(e,m(e))){let r=e.OPENCLAW_CONFIG_PATH?.trim()||e.CLAWDBOT_CONFIG_PATH?.trim();return r?v(r,e,m(e)):t.join(n,d)}function x(t=process.env,n=m(t)){return t.OPENCLAW_TEST_FAST===`1`?b(t,_(t,n)):C(t,n).find(t=>{try{return e.existsSync(t)}catch{return!1}})||b(t,_(t,n))}function S(n=process.env,r=_(n,m(n)),i=m(n)){let a=n.OPENCLAW_CONFIG_PATH?.trim();if(a)return v(a,n,i);if(n.OPENCLAW_TEST_FAST===`1`)return t.join(r,d);let o=n.OPENCLAW_STATE_DIR?.trim(),s=[t.join(r,d),...f.map(e=>t.join(r,e))].find(t=>{try{return e.existsSync(t)}catch{return!1}});if(s)return s;if(o)return t.join(r,d);let c=_(n,i);return t.resolve(r)===t.resolve(c)?x(n,i):t.join(r,d)}x();function C(e=process.env,n=m(e)){let r=()=>s(e,n),i=e.OPENCLAW_CONFIG_PATH?.trim()||e.CLAWDBOT_CONFIG_PATH?.trim();if(i)return[v(i,e,r)];let a=[],o=e.OPENCLAW_STATE_DIR?.trim()||e.CLAWDBOT_STATE_DIR?.trim();if(o){let n=v(o,e,r);a.push(t.join(n,d)),a.push(...f.map(e=>t.join(n,e)))}let c=[g(r),...h(r)];for(let e of c)a.push(t.join(e,d)),a.push(...f.map(n=>t.join(e,n)));return a}const w=18789;function T(e=process.env,n=_(e,m(e))){let r=e.OPENCLAW_OAUTH_DIR?.trim();return r?v(r,e,m(e)):t.join(n,`credentials`)}function E(e=process.env,n=_(e,m(e))){return t.join(T(e,n),`oauth.json`)}function D(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:w}export{D as a,_ as c,s as d,C as i,c as l,y as n,T as o,S as r,E as s,w as t,i as u};
