#!/usr/bin/env node
import {a as a$1}from'./chunk-LKAT7IAK.js';import'./chunk-XHJ27CER.js';import {execSync,spawn}from'child_process';import*as c from'fs';import*as r from'path';import*as p from'os';import {fileURLToPath}from'url';import e from'chalk';var b=fileURLToPath(import.meta.url),w=r.dirname(b),a=r.join(p.homedir(),".paradigm","conductor","bin","conductor");async function k(t){let n=a$1.command("conductor");if(t.install){await C(t,n);return}let i=x(t,n);if(i){if(t.build){let o=d();if(o){l(o,t,n);let s=r.join(o,".build","release","conductor");f(s,n);return}else n.error("--build requires the Paradigm monorepo"),console.log(e.gray("  Run from inside the a-paradigm repo, or use --install to update the global binary.")),process.exit(1);}f(i,n);}}function x(t,n){if(c.existsSync(a))return a;let i=d();if(i){let o=r.join(i,".build","release","conductor");if(c.existsSync(o)||(n.info("Dev binary not found, building\u2026"),l(i,t,n),c.existsSync(o)))return o}return n.error("Conductor binary not found"),console.log(""),console.log(e.white("  To install Conductor:")),console.log(e.cyan("    cd <paradigm-repo> && paradigm conductor --install")),console.log(""),console.log(e.gray("  This builds the native binary and installs it to ~/.paradigm/conductor/bin/")),console.log(e.gray("  After that, `paradigm conductor` works from any directory.")),console.log(""),process.exit(1),null}async function C(t,n){let i=d();i||(n.error("Cannot install \u2014 not in the Paradigm monorepo"),console.log(e.gray("  Run this command from inside the a-paradigm repository.")),process.exit(1));let o=r.join(i,".build","release","conductor");(!c.existsSync(o)||t.build)&&l(i,t,n),c.existsSync(o)||(n.error("Build did not produce a binary"),process.exit(1));let s=r.dirname(a);c.mkdirSync(s,{recursive:true}),c.copyFileSync(o,a),c.chmodSync(a,493);let m=(c.statSync(a).size/(1024*1024)).toFixed(1);n.success(`Conductor installed (${m} MB)`),console.log(""),console.log(e.green("  \u2713 ")+e.white("Installed to ")+e.cyan("~/.paradigm/conductor/bin/conductor")),console.log(e.green("  \u2713 ")+e.white("Run ")+e.cyan("paradigm conductor")+e.white(" from any directory")),console.log("");}function l(t,n,i){i.info("Building Conductor\u2026");try{execSync("swift build -c release",{cwd:t,stdio:n.verbose?"inherit":"pipe"}),i.success("Build complete");}catch(o){i.error("Build failed");let s=o.message||"";s.includes("xcode-select")?(console.log(e.gray("  Xcode Command Line Tools are required.")),console.log(e.gray("  Install with: xcode-select --install"))):console.log(e.gray(`  ${s.slice(0,200)}`)),process.exit(1);}}function f(t,n){n.info("Launching Conductor\u2026"),spawn(t,[],{detached:true,stdio:"ignore"}).unref();let o=t===a;console.log(e.cyan(`
  Paradigm Conductor is running.`)),o&&console.log(e.gray("  Binary: ~/.paradigm/conductor/bin/conductor")),console.log(e.gray("  Look for the waveform icon in your menu bar.")),console.log(e.gray(`  Quit via the menu bar icon or Cmd+Q.
`));}function d(){let t=r.resolve(w,"..");for(let o=0;o<5;o++){let s=r.join(t,"packages","conductor");if(c.existsSync(r.join(s,"Package.swift")))return s;t=r.dirname(t);}let n=r.join(process.cwd(),"packages","conductor");if(c.existsSync(r.join(n,"Package.swift")))return n;if(c.existsSync(r.join(process.cwd(),"Package.swift"))&&r.basename(process.cwd())==="conductor")return process.cwd();let i=process.cwd();for(let o=0;o<5;o++){i=r.dirname(i);let s=r.join(i,"packages","conductor");if(c.existsSync(r.join(s,"Package.swift")))return s}return null}export{k as conductorCommand};