text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = void 0;
  
  var _debug = _interopRequireDefault(require("../../debug"));
  
  var _noSuchCommandText = _interopRequireDefault(require("../../util/noSuchCommandText"));
  
  var _generateCommandsDocumentation = require("../../util/generateCommandsDocumentation");
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  var _default = (args, context) => {
    const [commandName, subCommandName] = args.argsWithoutOptions;
    const {
      commandGroups
    } = context.commandRunner;
  
    if (!commandName) {
      (0, _debug.default)('No command given to "help", showing generate Sanity CLI help');
      context.output.print((0, _generateCommandsDocumentation.generateCommandsDocumentation)(commandGroups));
      return;
    }
  
    const defaultCommand = commandGroups.default.find(cmd => cmd.name === commandName);
  
    if (defaultCommand && !defaultCommand.isGroupRoot) {
      (0, _debug.default)(`Found command in default group with name "${commandName}"`);
      context.output.print((0, _generateCommandsDocumentation.generateCommandDocumentation)(defaultCommand));
      return;
    }
  
    const group = commandGroups[commandName];
  
    if (!subCommandName && !group) {
      (0, _debug.default)(`No subcommand given, and we couldn't find a group with name "${group}"`);
      throw new Error((0, _noSuchCommandText.default)(commandName, null, commandGroups));
    }
  
    if (!subCommandName && group) {
      (0, _debug.default)(`No subcommand given, but found group with name "${commandName}"`);
      context.output.print((0, _generateCommandsDocumentation.generateCommandsDocumentation)(commandGroups, commandName));
      return;
    }
  
    if (subCommandName && !group) {
      (0, _debug.default)(`Subcommand given, but couldn't find group with name "${commandName}"`);
      throw new Error((0, _noSuchCommandText.default)(subCommandName, commandName, commandGroups));
    }
  
    const subCommand = context.commandRunner.resolveSubcommand(group, subCommandName, commandName);
  
    if (!subCommand) {
      (0, _debug.default)(`Subcommand given, but not found in group "${commandName}"`);
      throw new Error((0, _noSuchCommandText.default)(subCommandName, commandName, commandGroups));
    }
  
    (0, _debug.default)('Subcommand "%s" for group "%s" found, showing help', subCommandName, commandName);
    context.output.print((0, _generateCommandsDocumentation.generateCommandDocumentation)(subCommand.command, commandName, subCommandName));
  };
  
  exports.default = _default;