/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { type Config, ACTIVATE_SKILL_TOOL_NAME } from '@google/gemini-cli-core';
import { CommandKind, type SlashCommand } from '../ui/commands/types.js';
import { type ICommandLoader } from './types.js';

/**
 * Loads Agent Skills as slash commands.
 */
export class SkillCommandLoader implements ICommandLoader {
  constructor(private config: Config | null) {}

  /**
   * Discovers all available skills from the SkillManager and converts
   * them into executable slash commands.
   *
   * @param _signal An AbortSignal (unused for this synchronous loader).
   * @returns A promise that resolves to an array of `SlashCommand` objects.
   */
  async loadCommands(_signal: AbortSignal): Promise<SlashCommand[]> {
    if (!this.config || !this.config.isSkillsSupportEnabled()) {
      return [];
    }

    const skillManager = this.config.getSkillManager();
    if (!skillManager || !skillManager.isAdminEnabled()) {
      return [];
    }

    // Only include enabled skills that are not built-in (since built-in skills
    // are usually internal or have dedicated slash commands).
    // Actually, the user says "if you have a skill called google-foo", so we
    // should probably include all user/workspace/extension skills.
    const skills = skillManager.getDisplayableSkills();

    return skills.map((skill) => ({
      name: skill.name,
      description: skill.description || `Activate the ${skill.name} skill`,
      kind: CommandKind.SKILL,
      autoExecute: true,
      action: async (_context, args) => ({
          type: 'tool',
          toolName: ACTIVATE_SKILL_TOOL_NAME,
          toolArgs: { name: skill.name },
          postSubmitPrompt: args.trim().length > 0 ? args.trim() : undefined,
        }),
    }));
  }
}
