export interface SlashCommandItem {
  name: string;
  description?: string;
}

/** True while the user is still typing the command name after `/`. */
export function isSlashCommandToken(value: string): boolean {
  return /^\/[^\s]*$/.test(value);
}

export function slashQuery(value: string): string {
  return value.slice(1);
}

export function parseSlashInput(text: string): { cmdName: string; args: string } | null {
  const match = text.match(/^\/(\S+)\s*([\s\S]*)$/);
  if (!match) return null;
  return { cmdName: match[1]!, args: (match[2] ?? '').trim() };
}

export function mergeCommandsByName<T extends SlashCommandItem>(...lists: T[][]): T[] {
  const byName = new Map<string, T>();
  for (const list of lists) {
    for (const cmd of list) {
      byName.set(cmd.name, cmd);
    }
  }
  return [...byName.values()].sort((a, b) => a.name.localeCompare(b.name));
}
