import { getFigmaConfig, getFigmaSnapshot, saveFigmaSnapshot, getFigmaGeneratedSpecs, getFigmaVersions, saveFigmaVersions, } from '../../storage/figma-store.js';
import { getFigmaFile, parseFigmaUrl } from '../../engine/figma/api-client.js';
import { extractFrames } from '../../engine/figma/frame-extractor.js';
import { framesToSnapshot, computeFrameDiff, formatDiffAsMarkdown, } from '../../engine/figma/change-detector.js';
import { fetchLatestFigmaVersion, detectDrift, formatDriftReport, } from '../../engine/figma/version-tracker.js';
import { FIGMA_NOT_CONNECTED } from './shared.js';
export async function handleSyncFigmaChanges(input) {
    const config = await getFigmaConfig(input.projectPath);
    if (config === null) {
        return FIGMA_NOT_CONNECTED;
    }
    const parsed = parseFigmaUrl(input.figmaUrl);
    const figmaFile = await getFigmaFile(parsed.fileKey, config.accessToken);
    const frames = extractFrames(figmaFile);
    const previousSnapshot = await getFigmaSnapshot(input.projectPath);
    const currentSnapshot = framesToSnapshot(frames);
    const diff = computeFrameDiff(previousSnapshot, currentSnapshot);
    await saveFigmaSnapshot(input.projectPath, currentSnapshot);
    const md = formatDiffAsMarkdown(diff);
    return { content: [{ type: 'text', text: md }] };
}
export async function handleCheckFigmaDesignDrift(input) {
    const config = await getFigmaConfig(input.projectPath);
    if (config === null) {
        return FIGMA_NOT_CONNECTED;
    }
    const parsed = parseFigmaUrl(input.figmaUrl);
    const currentVersion = await fetchLatestFigmaVersion(parsed.fileKey, config.accessToken);
    const versions = await getFigmaVersions(input.projectPath);
    const storedVersion = versions[parsed.fileKey];
    const generatedSpecs = await getFigmaGeneratedSpecs(input.projectPath);
    const specsWithVersion = generatedSpecs.map((s) => ({
        specId: s.specId,
        section: s.section,
        figmaVersion: storedVersion,
    }));
    const driftEntries = detectDrift(specsWithVersion, currentVersion);
    versions[parsed.fileKey] = currentVersion;
    await saveFigmaVersions(input.projectPath, versions);
    const md = formatDriftReport(driftEntries);
    return { content: [{ type: 'text', text: md }] };
}
//# sourceMappingURL=changes.js.map