// @bun
import {
  require_src
} from "./chunk-g8mm42v1.js";
import {
  __toESM
} from "./chunk-dhs2bg35.js";

// ../adk/dist/commands/bp-cli.js
var import_debug = __toESM(require_src(), 1);
import os from "os";
import path from "path";
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
var AdkError;
var init_errors = __esm(() => {
  AdkError = class AdkError2 extends Error {
    static __IS_ADK_BASE_ERROR = true;
    code;
    expected;
    details;
    suggestion;
    constructor(opts) {
      super(opts.message, opts.cause !== undefined ? { cause: opts.cause } : undefined);
      this.name = this.constructor.name;
      this.code = opts.code;
      this.expected = opts.expected ?? false;
      if (opts.details !== undefined) {
        this.details = opts.details;
      }
      if (opts.suggestion !== undefined) {
        this.suggestion = opts.suggestion;
      }
    }
  };
});
var init_src = __esm(() => {
  init_errors();
});
init_src();
var debug = import_debug.default("adk:bp-cli");
var BP_CLI_VERSION = "6.8.6";
var BP_CLI_INSTALL_ALL = path.join(os.homedir(), ".adk", `bp-cli`);
var BP_CLI_INSTALL_DIR = path.join(BP_CLI_INSTALL_ALL, BP_CLI_VERSION);
var BP_CLI_BIN_PATH = path.join(BP_CLI_INSTALL_DIR, "node_modules", "@botpress", "cli", "bin.js");
debug("BP_CLI_VERSION=%s (injected at build time)", BP_CLI_VERSION);
debug("BP_CLI_INSTALL_ALL=%s", BP_CLI_INSTALL_ALL);
debug("BP_CLI_INSTALL_DIR=%s", BP_CLI_INSTALL_DIR);
debug("BP_CLI_BIN_PATH=%s", BP_CLI_BIN_PATH);

// src/utils/install-bp-cli.ts
var import_debug2 = __toESM(require_src(), 1);
var {$ } = globalThis.Bun;
import { mkdir, stat } from "fs/promises";
import { homedir } from "os";
import path2 from "path";
async function fileExists(filePath) {
  try {
    await stat(filePath);
    return true;
  } catch {
    return false;
  }
}
var debug2 = import_debug2.default("adk:install-bp-cli");
async function findBunConfig() {
  const localBunfig = path2.join(process.cwd(), "bunfig.toml");
  if (await fileExists(localBunfig)) {
    debug2("found local bunfig.toml: %s", localBunfig);
    return localBunfig;
  }
  const globalBunfig = path2.join(homedir(), ".bunfig.toml");
  if (await fileExists(globalBunfig)) {
    debug2("found global bunfig.toml: %s", globalBunfig);
    return globalBunfig;
  }
  debug2("no bunfig.toml found");
  return null;
}
async function installBpCLI() {
  debug2("installBpCLI() called");
  debug2("BP_CLI_VERSION=%s", BP_CLI_VERSION);
  debug2("BP_CLI_INSTALL_DIR=%s", BP_CLI_INSTALL_DIR);
  debug2("BP_CLI_BIN_PATH=%s", BP_CLI_BIN_PATH);
  const binExists = await fileExists(BP_CLI_BIN_PATH);
  debug2("BP_CLI_BIN_PATH exists=%s", binExists);
  if (binExists) {
    debug2("bp CLI already installed, skipping installation");
    return;
  }
  debug2("bp CLI not found, installing @botpress/cli@%s", BP_CLI_VERSION);
  await mkdir(BP_CLI_INSTALL_DIR, { recursive: true });
  debug2("created install directory: %s", BP_CLI_INSTALL_DIR);
  const bunfigPath = await findBunConfig();
  const configFlag = bunfigPath ? `--config=${bunfigPath}` : "";
  debug2("using config flag: %s", configFlag || "(none)");
  const env = { ...process.env };
  debug2("running: bun init -y");
  await $`BUN_BE_BUN=1 ${process.execPath} init -y`.cwd(BP_CLI_INSTALL_DIR).env(env);
  debug2("running: bun add %s @botpress/cli@%s", configFlag, BP_CLI_VERSION);
  if (configFlag) {
    await $`BUN_BE_BUN=1 ${process.execPath} add ${configFlag} @botpress/cli@${BP_CLI_VERSION}`.cwd(BP_CLI_INSTALL_DIR).env(env);
  } else {
    await $`BUN_BE_BUN=1 ${process.execPath} add @botpress/cli@${BP_CLI_VERSION}`.cwd(BP_CLI_INSTALL_DIR).env(env);
  }
  debug2("bp CLI installation complete");
}
export {
  installBpCLI
};
