/**
 * Tailwind CSS transformation function for Semantic UI components (Server)
 * Scans component definition for Tailwind classes and generates JIT CSS
 */

import { generateTailwindCSS } from './generator-server.js';
import { collectContent } from './scanner.js';

export default function TailwindPlugin() {
  return async function transformDefinition(definition) {
    // Collect all content and CSS from the component definition
    const { content, css } = collectContent(definition);

    // Quick check - if no content, return unchanged
    if (!content.trim()) {
      return definition;
    }

    // Generate CSS using @tailwindcss/node with native scanner
    const tailwindCSS = await generateTailwindCSS({
      content,
      css,
    });

    // If no CSS was generated, return unchanged
    if (!tailwindCSS.trim()) {
      return definition;
    }

    // Replace component CSS with Tailwind-enhanced version
    return {
      ...definition,
      css: tailwindCSS,
    };
  };
}
