#!/usr/bin/env node
import {a as a$1}from'./chunk-LKAT7IAK.js';import'./chunk-5TAVYPOV.js';import {execSync,spawn}from'child_process';import*as t from'fs';import*as w from'https';import*as a from'path';import*as g from'os';import {fileURLToPath}from'url';import r from'chalk';var B=fileURLToPath(import.meta.url),O=a.dirname(B),c=a.join(g.homedir(),".paradigm","conductor","bin","conductor"),b="ascend42/a-paradigm";async function j(e){let o=a$1.command("conductor");if(process.platform!=="darwin"&&(o.error("Conductor is macOS only"),console.log(r.gray("  Conductor is a native macOS overlay app (macOS 14 Sonoma or later).")),console.log(r.gray("  It is not available on "+process.platform+".")),process.exit(1)),e.build){let n=x();n||(o.error("--build requires the Paradigm monorepo"),v(),process.exit(1)),C(n,e,o),S(n,o),p(c,o);return}if(!e.install&&t.existsSync(c)){p(c,o);return}e.install?console.log(r.cyan(`
  Reinstalling Conductor\u2026
`)):console.log(r.cyan(`
  Conductor not installed. Installing now\u2026
`)),await R(e,o)||process.exit(1),p(c,o);}async function R(e,o){let i=x();if(i){o.info("Paradigm monorepo found \u2014 building from source\u2026");try{return C(i,e,o),S(i,o),!0}catch{o.warn("Source build failed, trying binary download\u2026");}}return await I(o)?true:(console.log(""),console.log(r.red("  Could not install Conductor automatically.")),console.log(""),console.log(r.white("  Option 1 \u2014 Clone the repo and install:")),v(),console.log(r.white("  Option 2 \u2014 Check GitHub releases for a pre-compiled binary:")),console.log(r.gray(`    https://github.com/${b}/releases`)),console.log(""),false)}function C(e,o,i){i.info("Building Conductor (this takes ~30s)\u2026");try{execSync("swift build -c release",{cwd:e,stdio:o.verbose?"inherit":"pipe"}),i.success("Build complete");}catch(n){let s=n.message||"";throw s.includes("xcode-select")||s.includes("xcrun")||s.includes("no developer")?(console.log(""),console.log(r.yellow("  Xcode Command Line Tools are required to build Conductor.")),console.log(r.gray("  Install with: ")+r.cyan("xcode-select --install")),console.log(r.gray("  Then re-run: ")+r.cyan("paradigm conductor")),console.log("")):o.verbose&&console.log(r.gray(`  ${s.slice(0,400)}`)),n}}function S(e,o){let i=a.join(e,".build","release","Conductor"),n=a.join(e,".build","release","conductor"),s=t.existsSync(i)?i:n;if(!t.existsSync(s))throw new Error("Build did not produce a binary");let d=a.dirname(c);t.mkdirSync(d,{recursive:true}),t.copyFileSync(s,c),t.chmodSync(c,493);let m=(t.statSync(c).size/(1024*1024)).toFixed(1);o.success(`Conductor installed (${m} MB)`),console.log(r.green("  \u2713 ")+r.gray("~/.paradigm/conductor/bin/conductor")),console.log("");}async function I(e){let o=g.arch()==="arm64"?"arm64":"x86_64",i=`https://github.com/${b}/releases/latest/download/conductor-${o}`;e.info(`Downloading Conductor binary (${o})\u2026`);try{await $(i,c),t.chmodSync(c,493);let n=(t.statSync(c).size/(1024*1024)).toFixed(1);return e.success(`Conductor downloaded (${n} MB)`),console.log(r.green("  \u2713 ")+r.gray("~/.paradigm/conductor/bin/conductor")),console.log(""),!0}catch(n){let s=n instanceof Error?n.message:String(n);return s.includes("404")||s.includes("Not Found")?e.warn("No pre-compiled binary available for this release"):e.warn(`Download failed: ${s}`),false}}function $(e,o){return new Promise((i,n)=>{let s=a.dirname(o);t.mkdirSync(s,{recursive:true});let d=(m,y=0)=>{if(y>5){n(new Error("Too many redirects"));return}w.get(m,l=>{if(l.statusCode===301||l.statusCode===302||l.statusCode===307||l.statusCode===308){let f=l.headers.location;if(!f){n(new Error("Redirect with no Location header"));return}l.resume(),d(f,y+1);return}if(l.statusCode!==200){l.resume(),n(new Error(`HTTP ${l.statusCode}`));return}let u=t.createWriteStream(o);l.pipe(u),u.on("finish",()=>u.close(()=>i())),u.on("error",f=>{t.unlink(o,()=>{}),n(f);});}).on("error",n);};d(e);})}function p(e,o){o.info("Launching Conductor\u2026"),spawn(e,[],{detached:true,stdio:"ignore"}).unref(),console.log(r.cyan(`
  Paradigm Conductor is running.`)),console.log(r.gray("  Look for the waveform icon in your menu bar.")),console.log(r.gray(`  To reinstall: paradigm conductor --install
`));}function v(){console.log(""),console.log(r.gray("    git clone https://github.com/ascend42/a-paradigm.git")),console.log(r.gray("    cd a-paradigm")),console.log(r.gray("    paradigm conductor")),console.log("");}function x(){let e=a.resolve(O,"..");for(let i=0;i<5;i++){let n=a.join(e,"packages","conductor");if(t.existsSync(a.join(n,"Package.swift")))return n;e=a.dirname(e);}let o=process.cwd();for(let i=0;i<6;i++){let n=a.join(o,"packages","conductor");if(t.existsSync(a.join(n,"Package.swift")))return n;if(t.existsSync(a.join(o,"Package.swift"))&&a.basename(o)==="conductor")return o;o=a.dirname(o);}return null}
export{j as conductorCommand};