// tools/figma-token-handler.ts — Design Token Sync handlers (SPEC-433)
import { getFigmaTokens } from '../storage/figma-store.js';
import { writeTokenFile, detectTokenDrift } from '../engine/figma/token-sync-writer.js';
// ---------------------------------------------------------------------------
// sync_figma_tokens_to_code handler (SPEC-433)
// ---------------------------------------------------------------------------
export async function handleSyncFigmaTokensToCode(input) {
    const tokens = await getFigmaTokens(input.projectPath);
    if (tokens.length === 0) {
        return {
            content: [{ type: 'text', text: 'No tokens found. Run extract_figma_design_tokens first.' }],
            isError: true,
        };
    }
    const options = {
        format: input.format,
        outputPath: input.outputPath,
        prefix: input.prefix,
    };
    const result = await writeTokenFile(input.projectPath, options, tokens);
    const preview = result.content.slice(0, 500);
    const ellipsis = result.content.length > 500 ? '\n...' : '';
    return {
        content: [
            {
                type: 'text',
                text: `✓ Wrote ${result.tokensWritten.toString()} tokens to ${result.outputPath}\n\nFormat: ${result.format}\n\nPreview:\n\`\`\`${result.format}\n${preview}${ellipsis}\n\`\`\``,
            },
        ],
    };
}
// ---------------------------------------------------------------------------
// validate_token_consistency handler (SPEC-433)
// ---------------------------------------------------------------------------
export async function handleValidateTokenConsistency(input) {
    const tokens = await getFigmaTokens(input.projectPath);
    if (tokens.length === 0) {
        return {
            content: [{ type: 'text', text: 'No stored tokens. Run extract_figma_design_tokens first.' }],
            isError: true,
        };
    }
    let fileContent;
    try {
        const { readFile } = await import('node:fs/promises');
        fileContent = await readFile(input.filePath, 'utf-8');
    }
    catch {
        return {
            content: [{ type: 'text', text: `Cannot read file: ${input.filePath}` }],
            isError: true,
        };
    }
    const drifts = detectTokenDrift(tokens, fileContent, input.format);
    const drifted = drifts.filter((d) => d.drifted);
    if (drifted.length === 0) {
        return {
            content: [
                {
                    type: 'text',
                    text: `✓ All ${tokens.length.toString()} tokens are in sync with ${input.filePath}`,
                },
            ],
        };
    }
    const lines = [
        `⚠ ${drifted.length.toString()} tokens out of sync in ${input.filePath}:`,
        '',
        '| Token | Stored | In File |',
        '|-------|--------|---------|',
        ...drifted.map((d) => `| ${d.name} | ${d.storedValue} | ${d.codeValue} |`),
        '',
        'Run `sync_figma_tokens_to_code` to update the file.',
    ];
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
//# sourceMappingURL=figma-token-handler.js.map