import { getFigmaConfig, saveFigmaTokens } from '../../storage/figma-store.js';
import { parseFigmaUrl } from '../../engine/figma/api-client.js';
import { fetchFigmaTokens, groupTokensByCategory, formatTokensAsMarkdown, } from '../../engine/figma/token-extractor.js';
import { FIGMA_NOT_CONNECTED } from './shared.js';
export async function handleExtractFigmaDesignTokens(input) {
    const config = await getFigmaConfig(input.projectPath);
    if (config === null) {
        return FIGMA_NOT_CONNECTED;
    }
    const parsed = parseFigmaUrl(input.figmaUrl);
    let tokens;
    try {
        tokens = await fetchFigmaTokens(parsed.fileKey, config.accessToken);
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        return { content: [{ type: 'text', text: `Error fetching tokens: ${msg}` }], isError: true };
    }
    await saveFigmaTokens(input.projectPath, tokens);
    const groups = groupTokensByCategory(tokens);
    const md = formatTokensAsMarkdown(groups);
    return { content: [{ type: 'text', text: md }] };
}
//# sourceMappingURL=tokens.js.map