// engine/figma/implementation-rules.ts — SPEC-488: Figma MCP implementation rules injection
/** Official Figma MCP server rules — always injected when returning design context. */
const BASE_FIGMA_RULES = [
    'Use localhost image/SVG sources directly — NEVER create placeholders when a localhost source is provided',
    'DO NOT install or import new icon packages — all icons and assets come from the Figma payload',
    'DO NOT use or create placeholder elements (gray boxes, empty divs) when a localhost source exists',
    'Use design tokens/variable names from figmaTokens — NEVER hardcode hex colors, spacing, or typography values',
    'Fetch structured design context via get_design_context first, then get_screenshot for visual validation',
];
/** Code Connect rule injected when mappings exist. */
function codeConnectRule(entries) {
    const names = entries.map((e) => e.componentName).join(', ');
    return `Code Connect: use mapped codebase components (${names}) instead of generating new UI code`;
}
/** Stack adaptation rule when project is not React+Tailwind. */
function stackAdaptationRule(stack, framework) {
    const isReact = stack.some((s) => /^react$/i.test(s)) || /^react/i.test(framework ?? '');
    const isTailwind = stack.some((s) => /tailwind/i.test(s));
    if (isReact && isTailwind) {
        return null; // Default already matches — no override needed
    }
    const detectedStack = stack
        .filter((s) => !/^(node|npm|yarn|pnpm|vite|webpack)$/i.test(s))
        .slice(0, 3)
        .join(', ') || 'detected project stack';
    const stackLabel = framework ?? detectedStack;
    return `Adapt Figma output from React+Tailwind default to: ${stackLabel}`;
}
/** Token summary rule — lists extracted token categories if available. */
function tokenSummaryRule(tokens) {
    if (tokens.length === 0) {
        return null;
    }
    const types = [...new Set(tokens.map((t) => t.category))].slice(0, 4).join(', ');
    return `Design tokens available (${String(tokens.length)} tokens: ${types}) — reference by name, never hardcode values`;
}
/**
 * Build the full list of implementation rules for a Figma context response.
 * Rules come from: Figma MCP official guide + Code Connect mappings + project stack.
 */
export function buildFigmaImplementationRules(opts) {
    const rules = [...BASE_FIGMA_RULES];
    if (opts.codeConnectEntries.length > 0) {
        rules.push(codeConnectRule(opts.codeConnectEntries));
    }
    const stackRule = stackAdaptationRule(opts.stack, opts.framework);
    if (stackRule !== null) {
        rules.push(stackRule);
    }
    const tokenRule = tokenSummaryRule(opts.tokens);
    if (tokenRule !== null) {
        rules.push(tokenRule);
    }
    return rules;
}
/** Build the content of .claude/rules/figma-design-system.md for client projects. */
export function buildFigmaRulesFileContent(opts) {
    const rules = buildFigmaImplementationRules(opts);
    const codeConnectSection = opts.codeConnectEntries.length > 0
        ? [
            '',
            '## Code Connect — use these components, do NOT regenerate',
            '',
            ...opts.codeConnectEntries.map((e) => `- **${e.componentName}** (nodeId: \`${e.figmaNodeId}\`) → \`${e.codeUrl}\``),
        ].join('\n')
        : '';
    const tokenSection = opts.tokens.length > 0
        ? [
            '',
            '## Design Tokens (never hardcode these values)',
            '',
            ...opts.tokens.slice(0, 20).map((t) => `- \`${t.name}\` = ${t.value} (${t.category})`),
            opts.tokens.length > 20
                ? `- … and ${String(opts.tokens.length - 20)} more — run extract_figma_design_tokens`
                : '',
        ]
            .filter(Boolean)
            .join('\n')
        : '';
    return [
        '# Figma Design System Rules',
        '',
        '> Auto-generated by Planu (SPEC-488). Source: Figma MCP server official guide.',
        '> Re-run `generate_figma_rules_file` after syncing new tokens or Code Connect mappings.',
        '',
        '## Implementation Rules (MANDATORY)',
        '',
        ...rules.map((r) => `- ${r}`),
        codeConnectSection,
        tokenSection,
        '',
        '## Workflow',
        '',
        '1. `get_design_context` → structured React+Tailwind (adapt to project stack)',
        '2. `get_screenshot` → visual validation of layout fidelity',
        '3. Use localhost asset URLs directly — never create placeholders',
        '4. Reference design tokens by name, never hardcode hex/spacing values',
        '5. Check Code Connect mappings above before generating any new UI component',
    ].join('\n');
}
//# sourceMappingURL=implementation-rules.js.map