// register-sdd-tools.ts — Canonical MCP surface for Planu's SDD workflow.
import { z } from 'zod';
import { registerResources } from '../resources/registry.js';
import { registerFromEntries } from './tool-entry.js';
import coreToolsRegistry from './tool-registry/core-tools.js';
import { registerSpecTools } from './register-spec-tools.js';
import { registerDesignStackTools } from './register-platform-tools/design-stack-tools.js';
import { registerQualityComplianceGroupTools } from './tool-registry/group-quality-compliance.js';
import { registerInfraGroupTools } from './tool-registry/group-infra.js';
import { safe } from './safe-handler.js';
import { handleCreateRule } from './create-rule.js';
import { handleCreateSkill } from './create-skill.js';
import { handleReconcileUniversalRules } from './reconcile-universal-rules.js';
import { registerPrompts } from '../prompts/index.js';
export const OFFICIAL_SDD_TOOL_NAMES = [
    'planu_status',
    'facilitate',
    'init_project',
    'clarify_requirements',
    'create_spec',
    'challenge_spec',
    'check_readiness',
    'update_status',
    'update_status_batch',
    'package_handoff',
    'validate',
    'reconcile_spec',
    'create_rule',
    'create_skill',
    'skill_search',
    'configure_code_graph',
    'code_graph_status',
];
const OFFICIAL_SDD_TOOL_SET = new Set(OFFICIAL_SDD_TOOL_NAMES);
const noop = () => undefined;
const DUMMY_TOOL_SLOT = {
    enable: noop,
    disable: noop,
    enabled: true,
};
function registerOnly(server, names, register) {
    const scopedServer = new Proxy(server, {
        get(target, prop, receiver) {
            if (prop === 'registerTool' || prop === 'tool') {
                return (...args) => {
                    const name = args[0];
                    if (!names.has(name)) {
                        return DUMMY_TOOL_SLOT;
                    }
                    const originalMethod = Reflect.get(target, prop, receiver);
                    return originalMethod.apply(target, args);
                };
            }
            // eslint-disable-next-line @typescript-eslint/no-unsafe-return
            return Reflect.get(target, prop, receiver);
        },
    });
    register(scopedServer);
}
function registerUniversalRulesTools(s) {
    s.registerTool('create_rule', {
        description: 'Create a host-aware rule file for a Planu project. For Claude Code, writes to `.claude/rules/{name}.md`. For Codex, inserts a delimited block in `AGENTS.md`. For Gemini, inserts a delimited block in `.gemini/conventions.md`. Use host: "auto" to detect the current host automatically.',
        inputSchema: {
            projectPath: z
                .string()
                .describe('Absolute path to the project root where the rule will be written'),
            name: z
                .string()
                .min(1)
                .describe('Rule identifier — kebab-case, no extension (e.g. "team-style").'),
            content: z.string().min(1).describe('Markdown body of the rule (no frontmatter required)'),
            host: z
                .enum(['claude-code', 'codex', 'gemini', 'auto'])
                .default('auto')
                .describe('Target host or auto-detection.'),
            overwriteExisting: z
                .boolean()
                .optional()
                .describe('When true, overwrite an existing file with different content.'),
        },
        annotations: { readOnlyHint: false, destructiveHint: false, title: 'Create Rule' },
    }, safe((args) => handleCreateRule(args)));
    s.registerTool('create_skill', {
        description: 'Create a host-aware skill for a Planu project. For Claude Code, writes to `.claude/skills/{name}/SKILL.md`. For Codex, inserts a skill block in `AGENTS.md`. For Gemini, writes to `.gemini/skills/{name}.md`. Use host: "auto" to detect the current host automatically.',
        inputSchema: {
            projectPath: z
                .string()
                .describe('Absolute path to the project root where the skill will be written'),
            name: z
                .string()
                .min(1)
                .describe('Skill identifier — kebab-case, no extension (e.g. "planu-status-check").'),
            content: z.string().min(1).describe('Markdown body of the skill instructions'),
            host: z
                .enum(['claude-code', 'codex', 'gemini', 'auto'])
                .default('auto')
                .describe('Target host or auto-detection.'),
            overwriteExisting: z
                .boolean()
                .optional()
                .describe('When true, overwrite an existing skill with different content.'),
            description: z
                .string()
                .optional()
                .describe('Short one-line description for the skill YAML frontmatter'),
        },
        annotations: { readOnlyHint: false, destructiveHint: false, title: 'Create Skill' },
    }, safe((args) => handleCreateSkill(args)));
    s.registerTool('reconcile_universal_rules', {
        description: 'Install missing Planu universal rules without touching user-modified ones. Advanced maintenance helper; not part of the official MCP SDD surface.',
        inputSchema: {
            projectPath: z.string().optional().describe('Absolute path to the project root'),
            host: z.enum(['claude-code', 'codex', 'gemini']).optional().describe('Target host'),
        },
        annotations: {
            readOnlyHint: false,
            destructiveHint: false,
            title: 'Reconcile Universal Rules',
        },
    }, safe((args) => handleReconcileUniversalRules(args)));
}
export function registerSddTools(server) {
    registerResources(server);
    registerFromEntries(server, coreToolsRegistry.filter((entry) => OFFICIAL_SDD_TOOL_SET.has(entry.name)));
    registerOnly(server, OFFICIAL_SDD_TOOL_SET, registerSpecTools);
    registerOnly(server, OFFICIAL_SDD_TOOL_SET, registerDesignStackTools);
    registerOnly(server, OFFICIAL_SDD_TOOL_SET, registerQualityComplianceGroupTools);
    registerOnly(server, OFFICIAL_SDD_TOOL_SET, registerInfraGroupTools);
    registerOnly(server, OFFICIAL_SDD_TOOL_SET, registerUniversalRulesTools);
    registerPrompts(server);
}
//# sourceMappingURL=register-sdd-tools.js.map