import chalk from "chalk";
const overlayOnlyHandler = async () => true;
function parseQuotedArguments(input) {
  const args = [];
  let current = "";
  let inQuotes = false;
  let quoteChar = "";
  let i = 0;
  while (i < input.length) {
    const char = input[i];
    const nextChar = input[i + 1];
    if (!inQuotes && (char === '"' || char === "'")) {
      inQuotes = true;
      quoteChar = char;
    } else if (inQuotes && char === quoteChar) {
      inQuotes = false;
      quoteChar = "";
    } else if (!inQuotes && char === " ") {
      if (current) {
        args.push(current);
        current = "";
      }
    } else if (char === "\\" && nextChar) {
      current += nextChar;
      i++;
    } else {
      current += char;
    }
    i++;
  }
  if (current) {
    args.push(current);
  }
  return args.filter((arg) => arg.length > 0);
}
function parseInput(input) {
  const trimmed = input.trim();
  if (trimmed.startsWith("!")) {
    const shellCommand = trimmed.slice(1).trim();
    return {
      type: "command",
      command: "shell",
      args: [shellCommand],
      rawInput: trimmed
    };
  }
  if (trimmed.startsWith("/")) {
    const args = parseQuotedArguments(trimmed.slice(1));
    const command = args[0] || "";
    const commandArgs = args.slice(1);
    return {
      type: "command",
      command,
      args: commandArgs,
      rawInput: trimmed
    };
  }
  return {
    type: "prompt",
    rawInput: input
  };
}
function getCommandSuggestions(partial, commands) {
  const suggestions = [];
  for (const cmd of commands) {
    if (cmd.name.startsWith(partial)) {
      suggestions.push(cmd.name);
    }
    if (cmd.aliases) {
      for (const alias of cmd.aliases) {
        if (alias.startsWith(partial)) {
          suggestions.push(alias);
        }
      }
    }
  }
  return suggestions.sort();
}
function formatCommandHelp(cmd, detailed = false) {
  let help = chalk.cyan(`/${cmd.name}`) + " - " + cmd.description;
  if (detailed) {
    help += "\n" + chalk.gray(`Usage: ${cmd.usage}`);
    if (cmd.aliases && cmd.aliases.length > 0) {
      help += "\n" + chalk.gray(`Aliases: ${cmd.aliases.map((a) => `/${a}`).join(", ")}`);
    }
    if (cmd.subcommands && cmd.subcommands.length > 0) {
      help += "\n" + chalk.gray("Subcommands:");
      for (const sub of cmd.subcommands) {
        help += "\n  " + chalk.cyan(`/${cmd.name} ${sub.name}`) + " - " + sub.description;
      }
    }
  }
  return help;
}
function displayAllCommands(commands) {
  console.log(chalk.bold.green("\n\u{1F4CB} Available Commands:\n"));
  const categoryOrder = [
    "General",
    "Session Management",
    "Model Management",
    "MCP Management",
    "Plugin Management",
    "Tool Management",
    "Prompt Management",
    "Skill Management",
    "System"
  ];
  const categories = {};
  for (const category of categoryOrder) {
    categories[category] = [];
  }
  for (const cmd of commands) {
    const category = cmd.category || "General";
    if (!categories[category]) {
      categories[category] = [];
    }
    categories[category].push(cmd);
  }
  for (const category of categoryOrder) {
    const cmds = categories[category];
    if (cmds && cmds.length > 0) {
      console.log(chalk.bold.rgb(255, 165, 0)(`${category}:`));
      for (const cmd of cmds) {
        console.log("  " + formatCommandHelp(cmd));
      }
      console.log();
    }
  }
  for (const [category, cmds] of Object.entries(categories)) {
    if (!categoryOrder.includes(category) && cmds.length > 0) {
      console.log(chalk.bold.rgb(255, 165, 0)(`${category}:`));
      for (const cmd of cmds) {
        console.log("  " + formatCommandHelp(cmd));
      }
      console.log();
    }
  }
  console.log(chalk.gray("\u{1F4A1} Tip: Use /help <command> for detailed help on any command"));
  console.log(chalk.gray("\u{1F4A1} Tip: Type your message normally (without /) to chat with the AI\n"));
}
export {
  displayAllCommands,
  formatCommandHelp,
  getCommandSuggestions,
  overlayOnlyHandler,
  parseInput
};
