'use strict';

var JBSG7YXG_cjs = require('./JBSG7YXG.cjs');
var fs = require('fs');
var path = require('path');
var toml = require('toml');
var vmTooling = require('@layerzerolabs/vm-tooling');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var fs__default = /*#__PURE__*/_interopDefault(fs);
var path__default = /*#__PURE__*/_interopDefault(path);

// src/commands/sync-toolchain.ts
JBSG7YXG_cjs.init_cjs_shims();
async function syncToolchain(context, options) {
  const filePath = path__default.default.join(options.cwd, "rust-toolchain.toml");
  if (!fs__default.default.existsSync(filePath)) {
    console.info("No rust-toolchain.toml found, skipping toolchain sync");
    return;
  }
  const parsed = toml.parse(fs__default.default.readFileSync(filePath, "utf-8"));
  const channel = parsed.toolchain?.channel;
  if (typeof channel !== "string") {
    throw new Error(`Missing 'toolchain.channel' in ${filePath}`);
  }
  const targets = parsed.toolchain?.targets ?? [];
  const components = parsed.toolchain?.components ?? [];
  const installArgs = [
    `rustup toolchain install ${channel}`
  ];
  for (const target of targets) {
    installArgs.push(`--target ${target}`);
  }
  for (const component of components) {
    installArgs.push(`--component ${component}`);
  }
  const installCmd = installArgs.join(" ");
  const script = [
    "mkdir -p $CARGO_HOME/bin",
    "ln -sf /usr/local/cargo/bin/rustup $CARGO_HOME/bin/rustup",
    installCmd
  ].join(" && ");
  console.info(`\u{1F527} Syncing Rust toolchain: ${installCmd}`);
  await vmTooling.executeToolCommand(context, "stellar", [], {
    ...options,
    script,
    volumes: [
      ...options.volumes,
      {
        type: "isolate",
        containerPath: "/cache/rustup",
        name: "stellar-rustup",
        shared: true,
        locked: true
      }
    ]
  });
}
JBSG7YXG_cjs.__name(syncToolchain, "syncToolchain");

exports.syncToolchain = syncToolchain;
//# sourceMappingURL=7MLNLFZC.cjs.map
//# sourceMappingURL=7MLNLFZC.cjs.map