// engine/figma/version-tracker.ts — Figma version fetching and drift detection (SPEC-428)
export async function fetchLatestFigmaVersion(fileKey, accessToken) {
    const response = await fetch(`https://api.figma.com/v1/files/${fileKey}/versions`, {
        headers: { 'X-Figma-Token': accessToken },
    });
    if (!response.ok) {
        const body = await response.text().catch(() => '');
        throw new Error(`Figma API error ${response.status}: ${body}`);
    }
    const data = (await response.json());
    if (data.versions.length === 0) {
        throw new Error(`No versions found for file ${fileKey}`);
    }
    const latest = data.versions[0];
    if (latest === undefined) {
        throw new Error(`No versions found for file ${fileKey}`);
    }
    return latest.id;
}
export function detectDrift(specs, currentVersion) {
    return specs.map((spec) => ({
        specId: spec.specId,
        section: spec.section,
        storedVersion: spec.figmaVersion ?? null,
        currentVersion,
        driftDetected: spec.figmaVersion !== currentVersion,
    }));
}
export function formatDriftReport(entries) {
    const drifted = entries.filter((e) => e.driftDetected);
    const upToDate = entries.length - drifted.length;
    if (drifted.length === 0) {
        return 'All specs are in sync with the current Figma design.';
    }
    const rows = drifted
        .map((e) => `| ${e.specId} | ${e.section} | ${e.storedVersion ?? 'none'} | ${e.currentVersion} |`)
        .join('\n');
    const footer = upToDate > 0 ? `\n\n${upToDate} spec${upToDate === 1 ? '' : 's'} up to date` : '';
    return (`**Design Drift Detected**\n` +
        `| Spec | Section | Stored Version | Current Version |\n` +
        `|------|---------|---------------|----------------|\n` +
        rows +
        footer);
}
//# sourceMappingURL=version-tracker.js.map