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