// hosts/gemini/multimodal-spec-flow.ts — Multimodal-first spec creation pipeline (SPEC-591)
// When Gemini provides an image attachment with a create_spec request,
// automatically chains inject_component_context + figma-tokens extraction.
/** Image MIME types that trigger the multimodal spec pipeline */
const IMAGE_MIME_TYPES = new Set([
    'image/png',
    'image/jpeg',
    'image/jpg',
    'image/gif',
    'image/webp',
    'image/svg+xml',
]);
/** PDF MIME type */
const PDF_MIME_TYPE = 'application/pdf';
/** Figma export tokens detection heuristic — filename or metadata hint */
const FIGMA_TOKEN_HINTS = ['figma', 'design-tokens', 'tokens.json', 'variables.json'];
/**
 * Analyse Gemini's multimodal content parts to determine input shape.
 * Does not perform I/O — purely inspects the attachment metadata.
 */
export function analyseMultimodalInput(attachmentRefs) {
    const hasImage = attachmentRefs.some((a) => IMAGE_MIME_TYPES.has(a.mimeType));
    const hasPdf = attachmentRefs.some((a) => a.mimeType === PDF_MIME_TYPE);
    const attachmentMimeTypes = attachmentRefs.map((a) => a.mimeType);
    const hasFigmaTokens = attachmentRefs.some((a) => FIGMA_TOKEN_HINTS.some((hint) => a.fileName?.toLowerCase().includes(hint)) ||
        a.mimeType === 'application/json');
    return {
        hasImage,
        hasPdf,
        attachmentMimeTypes,
        hasFigmaTokens,
        attachmentRefs,
    };
}
/**
 * Execute the multimodal spec-creation pipeline.
 *
 * When Gemini provides an image attachment:
 *   1. Calls injectComponentContextFn with visual description hint
 *   2. If Figma tokens are detected, calls extractFigmaTokensFn
 *
 * The caller provides the actual tool functions as dependencies
 * (ports & adapters pattern — keeps this module free of direct MCP coupling).
 *
 * @param input - Analysed multimodal input from analyseMultimodalInput()
 * @param projectPath - Project root path for context injection
 * @param injectComponentContextFn - Port for inject_component_context tool
 * @param extractFigmaTokensFn - Port for figma-tokens extraction tool
 */
export async function runMultimodalSpecFlow(input, projectPath, injectComponentContextFn, extractFigmaTokensFn) {
    if (!input.hasImage && !input.hasPdf) {
        return {
            triggered: false,
            componentContextInjected: false,
            figmaTokensExtracted: false,
            summary: 'No image or PDF attachment — multimodal pipeline skipped',
            warnings: [],
        };
    }
    const warnings = [];
    let componentContextInjected = false;
    let figmaTokensExtracted = false;
    // Step 1: inject_component_context — enriches spec with visual component hints
    try {
        const hint = buildVisualHint(input);
        await injectComponentContextFn(projectPath, hint);
        componentContextInjected = true;
    }
    catch (err) {
        warnings.push(`inject_component_context failed: ${err instanceof Error ? err.message : String(err)}`);
    }
    // Step 2: figma-tokens extraction — only if tokens file detected
    if (input.hasFigmaTokens) {
        try {
            await extractFigmaTokensFn(projectPath);
            figmaTokensExtracted = true;
        }
        catch (err) {
            warnings.push(`figma-tokens extraction failed: ${err instanceof Error ? err.message : String(err)}`);
        }
    }
    const parts = [];
    if (componentContextInjected) {
        parts.push('inject_component_context ✓');
    }
    if (figmaTokensExtracted) {
        parts.push('figma-tokens ✓');
    }
    if (warnings.length > 0) {
        parts.push(`${warnings.length} warning(s)`);
    }
    return {
        triggered: true,
        componentContextInjected,
        figmaTokensExtracted,
        summary: `Multimodal pipeline: ${parts.join(', ')}`,
        warnings,
    };
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
function buildVisualHint(input) {
    const parts = [];
    if (input.hasImage) {
        const imageTypes = input.attachmentRefs
            .filter((a) => IMAGE_MIME_TYPES.has(a.mimeType))
            .map((a) => a.fileName ?? a.mimeType)
            .join(', ');
        parts.push(`Image attachment(s): ${imageTypes}`);
    }
    if (input.hasPdf) {
        parts.push('PDF attachment detected');
    }
    if (input.hasFigmaTokens) {
        parts.push('Figma design tokens detected');
    }
    return parts.join('. ');
}
//# sourceMappingURL=multimodal-spec-flow.js.map