import{p as e}from"./subsystem-kTFXkQM5.js";import{n as t,t as n}from"./openclaw-root-CnsBf6ZU.js";import{t as r}from"./exec-CiwCrGw3.js";import{fileURLToPath as i}from"node:url";import a from"node:path";import o from"node:fs";const s=[`dist`,`control-ui`,`index.html`];function c(e){return a.join(e,...s)}async function l(e={}){let t=e.root?c(e.root):await d({argv1:e.argv1??process.argv[1],moduleUrl:e.moduleUrl});return{indexPath:t,exists:!!(t&&o.existsSync(t))}}function u(e=process.argv[1]){if(!e)return null;let t=a.resolve(e),n=t.split(a.sep),r=n.lastIndexOf(`src`);if(r!==-1){let e=n.slice(0,r).join(a.sep);if(o.existsSync(a.join(e,`ui`,`vite.config.ts`)))return e}let i=a.dirname(t);for(let e=0;e<8;e++){if(o.existsSync(a.join(i,`package.json`))&&o.existsSync(a.join(i,`ui`,`vite.config.ts`)))return i;let e=a.dirname(i);if(e===i)break;i=e}return null}async function d(e){let t=typeof e==`string`?e:e?.argv1??process.argv[1],r=typeof e==`object`?e?.moduleUrl:void 0;if(!t)return null;let i=a.resolve(t),s=[i];try{let e=o.realpathSync(i);e!==i&&s.push(e)}catch{}for(let e of s){let t=a.dirname(e);if(a.basename(t)===`dist`)return a.join(t,`control-ui`,`index.html`)}let c=await n({argv1:i,moduleUrl:r});if(c)return a.join(c,`dist`,`control-ui`,`index.html`);let l=new Set(s.map(e=>a.dirname(e)));for(let e of l){let t=e;for(let e=0;e<8;e++){let e=a.join(t,`package.json`),n=a.join(t,`dist`,`control-ui`,`index.html`);if(o.existsSync(e))try{let t=o.readFileSync(e,`utf-8`);if(JSON.parse(t).name===`openclaw`)return o.existsSync(n)?n:null;break}catch{break}let r=a.dirname(t);if(r===t)break;t=r}}return null}function f(e,t){let n,r;try{n=o.realpathSync(e)}catch{n=a.resolve(e)}try{r=o.realpathSync(t)}catch{r=a.resolve(t)}return n===r}function p(e,t){t&&e.add(a.resolve(t))}function m(e){let t=a.resolve(e);try{let e=o.statSync(t);if(e.isFile())return a.basename(t)===`index.html`?a.dirname(t):null;if(e.isDirectory()){let e=a.join(t,`index.html`);return o.existsSync(e)?t:null}}catch{return null}return null}function h(e={}){let n=new Set,r=e.argv1??process.argv[1],s=e.cwd??process.cwd(),c=e.moduleUrl?a.dirname(i(e.moduleUrl)):null,l=r?a.dirname(a.resolve(r)):null,u=(()=>{if(!r)return null;try{return a.dirname(o.realpathSync(a.resolve(r)))}catch{return null}})(),d=(()=>{try{let t=e.execPath??process.execPath;return a.dirname(o.realpathSync(t))}catch{return null}})(),f=t({argv1:r,moduleUrl:e.moduleUrl,cwd:s});p(n,d?a.join(d,`control-ui`):null),c&&(p(n,a.join(c,`control-ui`)),p(n,a.join(c,`../control-ui`)),p(n,a.join(c,`../../dist/control-ui`))),l&&(p(n,a.join(l,`dist`,`control-ui`)),p(n,a.join(l,`control-ui`))),u&&u!==l&&(p(n,a.join(u,`dist`,`control-ui`)),p(n,a.join(u,`control-ui`))),f&&p(n,a.join(f,`dist`,`control-ui`)),p(n,a.join(s,`dist`,`control-ui`));for(let e of n){let t=a.join(e,`index.html`);if(o.existsSync(t))return e}return null}function g(e,n={}){let r=n.argv1??process.argv[1],i=n.cwd??process.cwd(),o=t({argv1:r,moduleUrl:n.moduleUrl,cwd:i});return o?f(e,a.join(o,`dist`,`control-ui`)):!1}function _(e){let t=e.split(/\r?\n/g).map(e=>e.trim()).filter(Boolean);if(!t.length)return;let n=t.at(-1);if(n)return n.length>240?`${n.slice(0,239)}…`:n}async function v(t=e,n){let i=await l({argv1:process.argv[1]}),s=i.indexPath;if(i.exists)return{ok:!0,built:!1};let d=u(process.argv[1]);if(!d)return{ok:!1,built:!1,message:`${s?`Missing Control UI assets at ${s}`:`Missing Control UI assets`}. Build them with \`pnpm ui:build\` (auto-installs UI deps).`};let f=c(d);if(o.existsSync(f))return{ok:!0,built:!1};let p=a.join(d,`scripts`,`ui.js`);if(!o.existsSync(p))return{ok:!1,built:!1,message:`Control UI assets missing but ${p} is unavailable.`};t.log(`Control UI assets missing; building (ui:build, auto-installs UI deps)…`);let m=await r([process.execPath,p,`build`],{cwd:d,timeoutMs:n?.timeoutMs??10*6e4});return m.code===0?o.existsSync(f)?{ok:!0,built:!0}:{ok:!1,built:!0,message:`Control UI build completed but ${f} is still missing.`}:{ok:!1,built:!1,message:`Control UI build failed: ${_(m.stderr)??`exit ${m.code}`}`}}export{m as a,c as i,g as n,h as o,l as r,v as t};
