'use strict';

var O2P6E7GR_cjs = require('./O2P6E7GR.cjs');
var I4DRCDSO_cjs = require('./I4DRCDSO.cjs');
var _2NTYK3VO_cjs = require('./2NTYK3VO.cjs');
var _5GPMTH2I_cjs = require('./5GPMTH2I.cjs');
var _24WEKBY3_cjs = require('./24WEKBY3.cjs');
var commander = require('commander');
var esToolkit = require('es-toolkit');
var fs = require('fs');
var path = require('path');
var process = require('process');

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

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

// src/cli.ts
_24WEKBY3_cjs.init_cjs_shims();
var createCli = /* @__PURE__ */ _24WEKBY3_cjs.__name((context, registerExtraCommands) => {
  const { tools } = context;
  const toolVersionOptions = Object.fromEntries(tools.map(({ name }) => [
    `${esToolkit.camelCase(name)}Version`,
    name
  ]));
  const parseGlobalOptions = /* @__PURE__ */ _24WEKBY3_cjs.__name((command) => {
    const { cwd, volume, ...options } = command.opts();
    return {
      ...options,
      cwd: cwd ?? process__default.default.cwd(),
      volumes: volume,
      versions: Object.fromEntries(Object.entries(options).map(([name, version]) => [
        toolVersionOptions[name],
        version
      ]).filter(([tool]) => tool))
    };
  }, "parseGlobalOptions");
  const program = new commander.Command();
  program.name("lz-tool").description("\u{1F680} LayerZero VM tooling with intelligent version management\n\nUsage pattern:\n  lz-tool [global-options] <tool> [tool-args...]\n\nExamples:\n  lz-tool -e bash sui --help\n  lz-tool --sui-version 1.38.0 sui client\n\nNote: All lz-tool options must appear BEFORE the tool name.").version("1.0.0").enablePositionalOptions();
  if (process__default.default.argv.includes("--list-versions")) {
    O2P6E7GR_cjs.displayVersionCombinations(context);
    process__default.default.exit(0);
  }
  program.option("-c, --cwd <path>", "Current working directory", (input) => {
    if (typeof input !== "string") {
      throw new Error("cwd flag must be a string");
    }
    if (!path__default.default.isAbsolute(input)) {
      throw new Error("cwd must be an absolute path if provided");
    }
    if (!fs__default.default.existsSync(input)) {
      throw new Error("cwd does not exist");
    }
    if (!fs__default.default.statSync(input).isDirectory()) {
      throw new Error("cwd must be a directory");
    }
    return input;
  }).option("-e, --custom-entrypoint <entrypoint>", "Override the default Docker entrypoint for the tool").option("--script <script>", 'Execute a custom script using bash -c in the Docker container (e.g., --script "npm install && npm test")').option("--env <name=value>", "Set environment variables for Docker container (e.g., --env NODE_ENV=production)", (input, variables) => {
    const [name, ...valueParts] = input.split("=");
    if (!name || !valueParts.length) {
      throw new Error(`Invalid environment variable format: ${input}. Use --env NAME=VALUE`);
    }
    return [
      ...variables,
      {
        name,
        value: valueParts.join("=")
      }
    ];
  }, []).option("-v, --volume <type:hostPath:containerPath[:name]>", "Volume mappings in the format type:hostPath:containerPath[:name] (e.g., host:/host/path:/container/path or isolate::/container/path:volumeName)", (input, volumes) => {
    const [type, hostPath, containerPath, name] = input.split(":");
    switch (type) {
      case "host":
        if (!hostPath) {
          throw new Error(`Host path is required for volume type 'host'`);
        }
        break;
      case "isolate":
        if (!name) {
          throw new Error(`Name is required for volume type 'isolate'`);
        }
        break;
      default:
        throw new Error(`Invalid volume type: ${type}`);
    }
    if (!containerPath) {
      throw new Error(`Container path is required`);
    }
    return [
      ...volumes,
      _2NTYK3VO_cjs.volumeMappingSchema.parse({
        type,
        hostPath,
        containerPath,
        name
      })
    ];
  }, []).option("-p, --publish <host_port:container_port>", "Publish a container's port(s) to the host");
  for (const tool of tools) {
    const defaultVersion = _5GPMTH2I_cjs.getToolDefaultVersion(context, tool.name);
    const supportedVersions = _5GPMTH2I_cjs.getToolSupportedVersions(context, tool.name);
    program.option(`--${tool.name}-version <version>`, `Specify ${tool.name} version (default: ${defaultVersion}, supported: ${supportedVersions.join(", ")})`);
  }
  program.option("--list-versions", "Display all supported version combinations and exit");
  program.command("version-info <tool>").description("Display detailed version information for a specific tool").action((toolName) => O2P6E7GR_cjs.displayToolVersionInfo(context, toolName));
  for (const tool of tools) {
    program.command(tool.name, {
      isDefault: false
    }).description(`Run ${tool.name} with version checking`).passThroughOptions(true).allowUnknownOption().helpOption(false).argument("[args...]", "Arguments to pass to the tool").action(async (args) => {
      await I4DRCDSO_cjs.executeToolCommand(context, tool.name, args, parseGlobalOptions(program));
    });
  }
  registerExtraCommands?.(program, parseGlobalOptions);
  return program;
}, "createCli");
var runCli = /* @__PURE__ */ _24WEKBY3_cjs.__name(async (config, registerExtraCommands) => {
  try {
    await createCli(config, registerExtraCommands).parseAsync();
  } catch (error) {
    console.error("\u274C VM tool execution failed", error);
    process__default.default.exit(1);
  }
}, "runCli");

exports.runCli = runCli;
//# sourceMappingURL=5BNKRW3P.cjs.map
//# sourceMappingURL=5BNKRW3P.cjs.map