// SPEC-597-EXEMPT: canonical mapping (npm-package↔framework, keyword catalogue)
// — must enumerate framework names by design; no detection logic to migrate.
// engine/figma/code-connect-fetcher.ts — Figma Code Connect / Dev Resources (SPEC-426)
const FIGMA_API_BASE = 'https://api.figma.com/v1';
// ---------------------------------------------------------------------------
// inferFramework — infer frontend framework from a code URL
// ---------------------------------------------------------------------------
function inferFramework(url) {
    const lower = url.toLowerCase();
    if (lower.includes('react') || lower.includes('jsx') || lower.includes('tsx')) {
        return 'react';
    }
    if (lower.includes('vue')) {
        return 'vue';
    }
    if (lower.includes('angular')) {
        return 'angular';
    }
    if (lower.includes('swift') || lower.includes('ios')) {
        return 'ios';
    }
    if (lower.includes('kotlin') || lower.includes('android')) {
        return 'android';
    }
    return 'unknown';
}
// ---------------------------------------------------------------------------
// fetchCodeConnectMappings — fetch dev resources from Figma API
// ---------------------------------------------------------------------------
export async function fetchCodeConnectMappings(fileKey, accessToken) {
    const response = await fetch(`${FIGMA_API_BASE}/files/${fileKey}/dev_resources`, {
        headers: { 'X-Figma-Token': accessToken },
    });
    if (!response.ok) {
        const body = await response.text().catch(() => '');
        throw new Error(`Figma Dev Resources API error ${response.status}: ${body}`);
    }
    const data = (await response.json());
    const entries = data.dev_resources.map((resource) => ({
        figmaNodeId: resource.node_id,
        componentName: resource.name,
        codeUrl: resource.url,
        framework: inferFramework(resource.url),
        syncedAt: resource.updated_at,
    }));
    return entries.sort((a, b) => a.componentName.localeCompare(b.componentName));
}
// ---------------------------------------------------------------------------
// buildCodeConnectMap — index entries by figmaNodeId
// ---------------------------------------------------------------------------
export function buildCodeConnectMap(entries) {
    const map = new Map();
    for (const entry of entries) {
        map.set(entry.figmaNodeId, entry);
    }
    return map;
}
// ---------------------------------------------------------------------------
// formatCodeConnectAsMarkdown — render entries grouped by framework
// ---------------------------------------------------------------------------
export function formatCodeConnectAsMarkdown(entries) {
    if (entries.length === 0) {
        return 'No Code Connect mappings found. Run sync_figma_code_connect to map components.';
    }
    const byFramework = new Map();
    for (const entry of entries) {
        const existing = byFramework.get(entry.framework) ?? [];
        existing.push(entry);
        byFramework.set(entry.framework, existing);
    }
    const sections = [];
    for (const [framework, frameworkEntries] of byFramework) {
        const label = framework.charAt(0).toUpperCase() + framework.slice(1);
        const rows = frameworkEntries.map((e) => `| ${e.componentName} | ${e.codeUrl} |`).join('\n');
        sections.push(`**${label} Components**\n| Component | Code URL |\n|-----------|----------|\n${rows}`);
    }
    return sections.join('\n\n');
}
// ---------------------------------------------------------------------------
// calculateCoverage — percentage of nodeIds that have a mapping
// ---------------------------------------------------------------------------
export function calculateCoverage(totalNodeIds, entries) {
    if (totalNodeIds.length === 0) {
        return 0;
    }
    const mappedIds = new Set(entries.map((e) => e.figmaNodeId));
    const matched = totalNodeIds.filter((id) => mappedIds.has(id)).length;
    return Math.round((matched / totalNodeIds.length) * 100);
}
//# sourceMappingURL=code-connect-fetcher.js.map