// tools/design-to-spec.ts — MCP tool handler for design_to_spec (SPEC-159)
import { generateSpecFromDesign } from '../engine/design-to-spec/index.js';
import { compactResult, formatKeyValue } from './output-formatter.js';
/**
 * Handles the design_to_spec tool: parses a design description into a structured
 * spec with UI contracts, acceptance criteria per screen, responsive breakpoints,
 * and WCAG 2.1 AA accessibility requirements.
 *
 * This tool is Figma-agnostic — it works with text descriptions.
 * When Figma MCP data is available, the LLM should pass the design context
 * as the designDescription parameter.
 */
export function handleDesignToSpec(params) {
    const { projectId, designDescription, screens, platform, includeA11y } = params;
    if (!designDescription.trim()) {
        return {
            content: [
                {
                    type: 'text',
                    text: 'designDescription is required and must not be empty. Provide a text description of the design screens and components, or paste design context from Figma MCP.',
                },
            ],
            isError: true,
        };
    }
    if (designDescription.length > 50_000) {
        return {
            content: [
                {
                    type: 'text',
                    text: `designDescription exceeds the 50,000 character limit (received ${designDescription.length} chars). Trim the description or split it across multiple tool calls.`,
                },
            ],
            isError: true,
        };
    }
    try {
        const result = generateSpecFromDesign({
            projectId,
            designDescription,
            screens,
            platform,
            includeA11y,
        });
        const a11yMsg = result.a11yRequirements.length > 0
            ? `, ${result.a11yRequirements.length} a11y requirements`
            : '';
        const message = `Design spec generated: ${result.screens.length} screen(s), ${result.acceptanceCriteria.length} acceptance criteria${a11yMsg}`;
        return compactResult(formatKeyValue({
            projectId,
            platform: platform ?? 'web',
            screensAnalyzed: result.screens.length,
            acceptanceCriteriaCount: result.acceptanceCriteria.length,
            a11yRequirementsCount: result.a11yRequirements.length,
            breakpointsCount: result.breakpoints.length,
            generatedAt: result.generatedAt,
            message,
            specMarkdown: result.specMarkdown,
            uiContractMarkdown: result.uiContractMarkdown,
        }, 'Design Spec'));
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        return {
            content: [
                {
                    type: 'text',
                    text: `Failed to generate spec from design: ${message}. Check that the designDescription is a valid text description of screens and components.`,
                },
            ],
            isError: true,
        };
    }
}
//# sourceMappingURL=design-to-spec.js.map