"use strict";
/* eslint-disable */
const fs = require('fs');
const path = require('path');
/**
 * Script to check and fix icon name mismatches
 * Ensures the 'name' property in each icon file matches the filename
 */
function processIconFiles(directoryPath) {
    try {
        // Read all files in the directory
        const files = fs.readdirSync(directoryPath);
        // Filter only .ts files
        const tsFiles = files.filter(file => file.endsWith('.ts'));
        console.log(`Found ${tsFiles.length} TypeScript files to process...\n`);
        let changedFiles = 0;
        let errors = 0;
        tsFiles.forEach(filename => {
            try {
                const filePath = path.join(directoryPath, filename);
                const expectedName = path.basename(filename, '.ts'); // Remove .ts extension
                // Read file content
                const fileContent = fs.readFileSync(filePath, 'utf8');
                // Find the name property using regex
                const nameRegex = /name:\s*['"`]([^'"`]+)['"`]/;
                const match = fileContent.match(nameRegex);
                if (!match) {
                    console.log(`❌ ${filename}: Could not find 'name' property`);
                    errors++;
                    return;
                }
                const currentName = match[1];
                // Check if name matches filename
                if (currentName === expectedName) {
                    console.log(`✅ ${filename}: Name matches (${currentName})`);
                }
                else {
                    console.log(`🔄 ${filename}: Fixing name from '${currentName}' to '${expectedName}'`);
                    // Replace the name property
                    const updatedContent = fileContent.replace(nameRegex, `name: '${expectedName}'`);
                    // Write back to file
                    fs.writeFileSync(filePath, updatedContent, 'utf8');
                    changedFiles++;
                    console.log(`✅ ${filename}: Updated successfully`);
                }
            }
            catch (error) {
                console.error(`❌ Error processing ${filename}:`, error.message);
                errors++;
            }
        });
        // Summary
        console.log(`\n📊 Summary:`);
        console.log(`- Total files processed: ${tsFiles.length}`);
        console.log(`- Files changed: ${changedFiles}`);
        console.log(`- Errors: ${errors}`);
        console.log(`- No changes needed: ${tsFiles.length - changedFiles - errors}`);
    }
    catch (error) {
        console.error('❌ Error reading directory:', error.message);
    }
}
/**
 * Enhanced version with more features
 */
function processIconFilesAdvanced(directoryPath, options = {}) {
    const { dryRun = false, backup = false, recursive = false, filePattern = /\.ts$/ } = options;
    console.log(`🔍 Processing icons in: ${directoryPath}`);
    console.log(`Options: ${JSON.stringify(options, null, 2)}\n`);
    function processDirectory(dirPath) {
        const items = fs.readdirSync(dirPath, { withFileTypes: true });
        let results = { processed: 0, changed: 0, errors: 0 };
        for (const item of items) {
            const itemPath = path.join(dirPath, item.name);
            if (item.isDirectory() && recursive) {
                const subResults = processDirectory(itemPath);
                results.processed += subResults.processed;
                results.changed += subResults.changed;
                results.errors += subResults.errors;
            }
            else if (item.isFile() && filePattern.test(item.name)) {
                const result = processFile(itemPath);
                results.processed++;
                results.changed += result.changed ? 1 : 0;
                results.errors += result.error ? 1 : 0;
            }
        }
        return results;
    }
    function processFile(filePath) {
        try {
            const filename = path.basename(filePath);
            const expectedName = path.basename(filename, path.extname(filename));
            const fileContent = fs.readFileSync(filePath, 'utf8');
            // More robust regex to handle different quote types and spacing
            const nameRegex = /name:\s*(['"`])([^'"`]+)\1/;
            const match = fileContent.match(nameRegex);
            if (!match) {
                console.log(`❌ ${filename}: Could not find 'name' property`);
                return { error: true };
            }
            const [fullMatch, quote, currentName] = match;
            if (currentName === expectedName) {
                console.log(`✅ ${filename}: Name matches (${currentName})`);
                return { changed: false };
            }
            console.log(`🔄 ${filename}: ${dryRun ? 'Would fix' : 'Fixing'} name from '${currentName}' to '${expectedName}'`);
            if (!dryRun) {
                // Create backup if requested
                if (backup) {
                    const backupPath = `${filePath}.backup`;
                    fs.copyFileSync(filePath, backupPath);
                    console.log(`💾 Created backup: ${path.basename(backupPath)}`);
                }
                // Replace the name property, preserving the quote style
                const updatedContent = fileContent.replace(nameRegex, `name: ${quote}${expectedName}${quote}`);
                fs.writeFileSync(filePath, updatedContent, 'utf8');
                console.log(`✅ ${filename}: Updated successfully`);
            }
            return { changed: true };
        }
        catch (error) {
            console.error(`❌ Error processing ${path.basename(filePath)}:`, error.message);
            return { error: true };
        }
    }
    const results = processDirectory(directoryPath);
    console.log(`\n📊 Summary:`);
    console.log(`- Total files processed: ${results.processed}`);
    console.log(`- Files ${dryRun ? 'that would be changed' : 'changed'}: ${results.changed}`);
    console.log(`- Errors: ${results.errors}`);
    console.log(`- No changes needed: ${results.processed - results.changed - results.errors}`);
    return results;
}
// Usage examples:
// Basic usage
const iconsDirectory = path.join(__dirname, '../baseIcons/brand');
processIconFiles(iconsDirectory);
// Advanced usage with options
// processIconFilesAdvanced(iconsDirectory, {
//   dryRun: true,        // Preview changes without making them
//   backup: true,        // Create backup files
//   recursive: true,     // Process subdirectories
//   filePattern: /\.ts$/ // File pattern to match
// });
// Process multiple directories
// const directories = [
//   'src/icons/baseIcons/ui',
//   'src/icons/baseIcons/base',
//   'src/icons/baseIcons/brand'
// ];
// 
// directories.forEach(dir => {
//   console.log(`\n🔄 Processing directory: ${dir}`);
//   processIconFilesAdvanced(dir, { dryRun: false, backup: true });
// });
//# sourceMappingURL=fix-icon-names.js.map