import type { CliCommand, CommandDefinition } from "../commands.ts";
import { requireNoExtraArgs } from "../utils.ts";

export type HelpCommand = CliCommand<"help">;

export const helpCommand = {
  names: ["help", "--help", "-h"],
  args: [],
  options: [],
  description: "Show this help",
  parse: parseHelpArgs,
} satisfies CommandDefinition<HelpCommand>;

function parseHelpArgs(args: string[]): HelpCommand {
  requireNoExtraArgs("help", args);

  return {
    kind: "help",
    run: async () => {
      const [{ commandDefinitions }, { formatHelpText }] = await Promise.all([
        import("../commands.ts"),
        import("../help.ts"),
      ]);

      console.log(formatHelpText(commandDefinitions).trimEnd());
    },
  };
}
