"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupTailwindCSS = setupTailwindCSS;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const child_process_1 = require("child_process");
async function setupTailwindCSS(projectRoot) {
    console.log('🎨 Setting up Tailwind CSS v3...');
    // Check current Tailwind version and downgrade if needed
    await ensureTailwindV3(projectRoot);
    // Setup Tailwind config
    await setupTailwindConfig(projectRoot);
    // Setup PostCSS config
    await setupPostCSSConfig(projectRoot);
    // Update package.json scripts if needed
    await updatePackageJsonScripts(projectRoot);
    console.log('✅ Tailwind CSS v3 configured successfully');
}
async function ensureTailwindV3(projectRoot) {
    const packageJsonPath = path_1.default.join(projectRoot, 'package.json');
    if (await fs_extra_1.default.pathExists(packageJsonPath)) {
        const packageJson = await fs_extra_1.default.readJson(packageJsonPath);
        const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies };
        // Check if Tailwind is installed and what version
        const tailwindVersion = dependencies.tailwindcss;
        if (tailwindVersion) {
            // Check if it's v4 (starts with ^4 or ~4 or 4.x)
            if (tailwindVersion.match(/^[\^~]?4\.|^4\./)) {
                console.log('📦 Tailwind CSS v4 detected, downgrading to v3...');
                // Remove v4 and install v3
                try {
                    (0, child_process_1.execSync)('npm uninstall tailwindcss', { cwd: projectRoot, stdio: 'inherit' });
                    (0, child_process_1.execSync)('npm install -D tailwindcss@^3.4.0 postcss autoprefixer', { cwd: projectRoot, stdio: 'inherit' });
                    console.log('✅ Successfully downgraded to Tailwind CSS v3');
                }
                catch (error) {
                    console.warn('⚠️  Failed to downgrade Tailwind, please manually install v3');
                }
            }
            else if (!tailwindVersion.match(/^[\^~]?3\./)) {
                // Unknown version, install v3
                console.log('📦 Installing Tailwind CSS v3...');
                try {
                    (0, child_process_1.execSync)('npm install -D tailwindcss@^3.4.0 postcss autoprefixer', { cwd: projectRoot, stdio: 'inherit' });
                }
                catch (error) {
                    console.warn('⚠️  Failed to install Tailwind CSS v3');
                }
            }
        }
        else {
            // Tailwind not installed, install v3
            console.log('📦 Installing Tailwind CSS v3...');
            try {
                (0, child_process_1.execSync)('npm install -D tailwindcss@^3.4.0 postcss autoprefixer', { cwd: projectRoot, stdio: 'inherit' });
            }
            catch (error) {
                console.warn('⚠️  Failed to install Tailwind CSS v3');
            }
        }
    }
}
async function setupTailwindConfig(projectRoot) {
    const tailwindConfigPath = path_1.default.join(projectRoot, 'tailwind.config.js');
    const tailwindConfigTsPath = path_1.default.join(projectRoot, 'tailwind.config.ts');
    // Check if config already exists
    if (await fs_extra_1.default.pathExists(tailwindConfigPath) || await fs_extra_1.default.pathExists(tailwindConfigTsPath)) {
        console.log('📄 Tailwind config already exists, skipping...');
        return;
    }
    // Create Tailwind v3 config
    const tailwindConfig = `/** @type {import('tailwindcss').Config} */
export default {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}
`;
    await fs_extra_1.default.writeFile(tailwindConfigPath, tailwindConfig);
    console.log('✅ Created tailwind.config.js');
}
async function setupPostCSSConfig(projectRoot) {
    const postcssConfigPath = path_1.default.join(projectRoot, 'postcss.config.js');
    const postcssConfigTsPath = path_1.default.join(projectRoot, 'postcss.config.ts');
    // Check if PostCSS config already exists
    if (await fs_extra_1.default.pathExists(postcssConfigPath) || await fs_extra_1.default.pathExists(postcssConfigTsPath)) {
        let content = await fs_extra_1.default.readFile(postcssConfigPath, 'utf-8').catch(() => '');
        // Check if tailwindcss plugin is already configured
        if (content.includes('tailwindcss')) {
            console.log('📄 PostCSS config already has Tailwind, skipping...');
            return;
        }
    }
    // Create or update PostCSS config
    const postcssConfig = `export default {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}
`;
    await fs_extra_1.default.writeFile(postcssConfigPath, postcssConfig);
    console.log('✅ Created/updated postcss.config.js');
}
async function updatePackageJsonScripts(projectRoot) {
    const packageJsonPath = path_1.default.join(projectRoot, 'package.json');
    if (await fs_extra_1.default.pathExists(packageJsonPath)) {
        const packageJson = await fs_extra_1.default.readJson(packageJsonPath);
        // Ensure build script exists and includes Tailwind
        if (!packageJson.scripts) {
            packageJson.scripts = {};
        }
        // Add Tailwind build script if not present
        if (!packageJson.scripts['build:css']) {
            packageJson.scripts['build:css'] = 'tailwindcss -i ./src/index.css -o ./dist/output.css --watch';
        }
        await fs_extra_1.default.writeJson(packageJsonPath, packageJson, { spaces: 2 });
        console.log('✅ Updated package.json scripts');
    }
}
//# sourceMappingURL=tailwindSetup.js.map