// src/xy/xyParseOptions.ts
import { availableParallelism } from "os";
import chalk3 from "chalk";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

// src/lib/packageManagerRuntime.ts
import { spawnSync } from "child_process";
import fs from "fs";
import chalk from "chalk";
var DISPLAY_NAMES = {
  bun: "Bun",
  npm: "npm",
  pnpm: "pnpm",
  yarn: "Yarn"
};
var MAC_INSTALL_COMMANDS = {
  bun: "brew install oven-sh/bun/bun",
  npm: "brew install node",
  pnpm: "brew install pnpm",
  yarn: "brew install yarn"
};
function currentPlatform() {
  if (process.platform === "darwin" || process.platform === "linux" || process.platform === "win32") {
    return process.platform;
  }
  return "unknown";
}
function askYesNo(question) {
  if (!process.stdin.isTTY || !process.stdout.isTTY) {
    return false;
  }
  process.stdout.write(question);
  const buffer = Buffer.alloc(1024);
  const bytesRead = fs.readSync(process.stdin.fd, buffer, 0, buffer.length, null);
  const answer = buffer.toString("utf8", 0, bytesRead).trim().toLowerCase();
  return answer === "y" || answer === "yes";
}
function isCommandInstalled(command) {
  const result = spawnSync(command, ["--version"], {
    encoding: "utf8",
    shell: false,
    stdio: "ignore"
  });
  return !result.error && result.status === 0;
}
function isPackageManagerInstalled(pm) {
  return isCommandInstalled(pm);
}
function getPackageManagerInstallPlan(pm, platform = currentPlatform()) {
  const displayName = DISPLAY_NAMES[pm];
  if (platform === "darwin") {
    const command = MAC_INSTALL_COMMANDS[pm];
    return {
      command,
      manualInstructions: [
        `Install ${displayName} with: ${command}`
      ],
      packageManager: pm,
      platform,
      supported: true
    };
  }
  if (platform === "linux") {
    return {
      manualInstructions: [
        `Automatic installation for ${displayName} is not implemented on Linux yet.`,
        `Install ${displayName} manually and re-run the command.`
      ],
      packageManager: pm,
      platform,
      supported: false
    };
  }
  if (platform === "win32") {
    return {
      manualInstructions: [
        `Automatic installation for ${displayName} is not implemented on Windows yet.`,
        `Install ${displayName} manually and re-run the command.`
      ],
      packageManager: pm,
      platform,
      supported: false
    };
  }
  return {
    manualInstructions: [
      `Automatic installation for ${displayName} is not implemented on ${platform} yet.`,
      `Install ${displayName} manually and re-run the command.`
    ],
    packageManager: pm,
    platform,
    supported: false
  };
}
function printManualInstructions(plan, context) {
  console.error(chalk.yellow(`${DISPLAY_NAMES[plan.packageManager]} is required for "${context.operation}".`));
  for (const line of plan.manualInstructions) {
    console.error(chalk.gray(`  ${line}`));
  }
}
function installWithHomebrew(pm, cwd) {
  const command = MAC_INSTALL_COMMANDS[pm];
  const result = spawnSync("brew", command.replace("brew ", "").split(" "), {
    cwd,
    encoding: "utf8",
    shell: false,
    stdio: "inherit"
  });
  return result.status === 0;
}
function ensurePackageManagerInstalled(pm, context) {
  if (isPackageManagerInstalled(pm)) return true;
  const plan = getPackageManagerInstallPlan(pm, context.platform ?? currentPlatform());
  const displayName = DISPLAY_NAMES[pm];
  console.error(chalk.yellow(`${context.operation} requires ${displayName}, but it is not installed.`));
  if (!plan.supported) {
    printManualInstructions(plan, context);
    return false;
  }
  if (!isCommandInstalled("brew")) {
    console.error(chalk.yellow("Homebrew is not installed, so automatic installation is unavailable."));
    console.error(chalk.gray("  Install Homebrew from https://brew.sh and then run:"));
    console.error(chalk.gray(`  ${plan.command}`));
    return false;
  }
  const confirmed = askYesNo(chalk.cyan(`Install ${displayName} with Homebrew now? [y/N] `));
  if (!confirmed) {
    console.error(chalk.gray(`Skipping install. Run "${plan.command}" manually and then retry.`));
    return false;
  }
  console.error(chalk.cyan(`Installing ${displayName} via Homebrew...
`));
  if (!installWithHomebrew(pm, context.cwd)) {
    console.error(chalk.red(`Homebrew failed to install ${displayName}.`));
    console.error(chalk.gray(`Run "${plan.command}" manually and then retry.`));
    return false;
  }
  if (!isPackageManagerInstalled(pm)) {
    console.error(chalk.red(`${displayName} still does not appear to be installed after Homebrew completed.`));
    console.error(chalk.gray(`Run "${plan.command}" manually and then retry.`));
    return false;
  }
  console.error(chalk.green(`${displayName} installed successfully.`));
  return true;
}
function failureResult() {
  return {
    output: ["", ""],
    pid: 0,
    signal: null,
    status: 1,
    stderr: "",
    stdout: ""
  };
}
function spawnPackageManagerSync(pm, args, options) {
  if (!ensurePackageManagerInstalled(pm, options.context)) {
    return failureResult();
  }
  return spawnSync(pm, args, { encoding: "utf8", ...options });
}

// src/pm/workspaces.ts
import { existsSync, readFileSync } from "fs";
import PATH from "path";
import { globSync } from "glob";
function readJsonFile(path) {
  if (!existsSync(path)) return void 0;
  try {
    return JSON.parse(readFileSync(path, "utf8"));
  } catch {
    return void 0;
  }
}
function readRootPackageJson(cwd = process.cwd()) {
  return readJsonFile(PATH.resolve(cwd, "package.json"));
}
function detectPackageManagerFromPackageJson(cwd = process.cwd()) {
  const packageManager = readRootPackageJson(cwd)?.packageManager;
  if (!packageManager) return void 0;
  return packageManager.split("@")[0];
}

// src/pm/detectPackageManager.ts
import { existsSync as existsSync2 } from "fs";
var knownPackageManagers = /* @__PURE__ */ new Set(["bun", "npm", "pnpm", "yarn"]);
function detectPackageManager() {
  const packageManager = detectPackageManagerFromPackageJson();
  if (packageManager && knownPackageManagers.has(packageManager)) {
    return packageManager;
  }
  if (existsSync2("pnpm-lock.yaml") || existsSync2("pnpm-workspace.yaml")) return "pnpm";
  if (existsSync2("bun.lock") || existsSync2("bun.lockb")) return "bun";
  if (existsSync2("yarn.lock") || existsSync2(".yarnrc.yml")) return "yarn";
  if (existsSync2("package-lock.json") || existsSync2("npm-shrinkwrap.json")) return "npm";
  return "yarn";
}

// src/pm/registry.ts
var implementations = /* @__PURE__ */ new Map();
function getPackageManager(name) {
  const pmName = name ?? detectPackageManager();
  const pm = implementations.get(pmName);
  if (!pm) {
    throw new Error(
      `No package manager implementation registered for "${pmName}". Ensure registerPackageManager() has been called before getPackageManager().`
    );
  }
  return pm;
}

// src/lib/tryRunLocalScript.ts
import { readFileSync as readFileSync2 } from "fs";
import PATH2 from "path";
import chalk2 from "chalk";
function tryRunLocalScript(commandName) {
  if (process.env.XY_LOCAL_SCRIPT === "1") return void 0;
  const rootPkgPath = PATH2.resolve(process.cwd(), "package.json");
  let rootPkg;
  try {
    rootPkg = JSON.parse(readFileSync2(rootPkgPath, "utf8"));
  } catch {
    return void 0;
  }
  if (!rootPkg.scripts?.[commandName]) return void 0;
  const extraArgs = process.argv.slice(process.argv.indexOf(commandName) + 1);
  console.log(chalk2.blue(`Delegating "${commandName}" to local script`));
  const pm = getPackageManager();
  const result = spawnPackageManagerSync(pm.name, ["run", commandName, ...extraArgs], {
    cwd: process.cwd(),
    context: { operation: `Run local "${commandName}" script with ${pm.command}` },
    encoding: "utf8",
    env: {
      FORCE_COLOR: "3",
      ...process.env,
      XY_LOCAL_SCRIPT: "1"
    },
    shell: true,
    stdio: "inherit"
  });
  return result.status ?? 1;
}

// src/xy/xyParseOptions.ts
function scriptNameForPackageManager() {
  switch (detectPackageManager()) {
    case "bun": {
      return "bun run xy";
    }
    case "npm": {
      return "npm exec -- xy";
    }
    case "pnpm": {
      return "pnpm xy";
    }
    default: {
      return "yarn xy";
    }
  }
}
function formatDuration(ms) {
  if (ms < 1e3) return `${ms}ms`;
  if (ms < 6e4) return `${(ms / 1e3).toFixed(2)}s`;
  const minutes = Math.floor(ms / 6e4);
  const seconds = (ms % 6e4 / 1e3).toFixed(1);
  return `${minutes}m ${seconds}s`;
}
var timerRegistered = false;
var xyParseOptions = () => {
  return yargs(hideBin(process.argv)).scriptName(scriptNameForPackageManager()).middleware((argv) => {
    if (process.argv.includes("--no-defer") || process.env.XY_NO_DEFER === "1") return;
    const commandName = argv._[0];
    if (commandName && argv._.length <= 1) {
      const result = tryRunLocalScript(commandName);
      if (result !== void 0) {
        process.exit(result);
      }
    }
  }, true).middleware((argv) => {
    if (!timerRegistered) {
      timerRegistered = true;
      const commandName = argv._[0];
      const jobs = argv.jobs;
      const label = commandName ? ` [${chalk3.white(commandName)}]` : "";
      console.log(
        chalk3.blue("\u2B21 XY") + label + chalk3.gray(` [threads: ${jobs}]`)
      );
      const start = Date.now();
      process.on("exit", () => {
        const exitLabel = commandName ? ` [${chalk3.white(commandName)}]` : "";
        console.log(chalk3.blue(`Finished${exitLabel} in ${formatDuration(Date.now() - start)}`));
      });
    }
  }, true).option("jobs", {
    alias: "j",
    default: availableParallelism(),
    description: "Max parallel jobs",
    type: "number"
  }).option("verbose", {
    alias: "v",
    default: false,
    description: "Run with verbose logging",
    type: "boolean"
  }).option("incremental", {
    alias: "i",
    default: false,
    description: "Attempt to perform the action only on changed packages",
    type: "boolean"
  }).option("profile", {
    alias: "p",
    default: false,
    description: "Profile action",
    type: "boolean"
  }).option("no-defer", {
    default: false,
    description: "Do not defer to local package.json script of the same name",
    type: "boolean"
  });
};
export {
  xyParseOptions
};
//# sourceMappingURL=xyParseOptions.mjs.map