"use strict";
/* eslint-disable */
const fs = require('fs');
const path = require('path');
// Move icon file from one prefix to another
function moveIconFile(filePath, fromPrefix, toPrefix = 'base', suffix = '', preserveOldName = false) {
    try {
        let content = fs.readFileSync(filePath, 'utf8');
        const nameMatch = content.match(/name:\s*'([^']+)'/);
        if (!nameMatch) {
            console.log(`❌ Could not find name property in ${filePath}`);
            return;
        }
        const currentName = nameMatch[1];
        const originalFileName = path.basename(filePath, '.ts');
        if (!currentName.startsWith(fromPrefix)) {
            console.log(`⚠️  Skipping ${originalFileName} - doesn't start with '${fromPrefix}'`);
            return;
        }
        // Create new name with optional suffix
        let newName = currentName.replace(new RegExp(`^${fromPrefix}`), toPrefix);
        if (suffix) {
            newName = newName + suffix;
        }
        // Handle oldName based on preserveOldName flag
        if (!preserveOldName) {
            if (content.includes('oldName:')) {
                content = content.replace(/oldName:\s*'[^']+'/, `oldName: '${currentName}'`);
            }
            else {
                content = content.replace(/name:\s*'([^']+)'/, `name: '$1',\n  oldName: '${currentName}'`);
            }
        }
        else {
            console.log(`🔒 Preserving existing oldName for ${originalFileName}`);
        }
        // Update the name property with new name (including suffix)
        content = content.replace(/name:\s*'[^']+'/, `name: '${newName}'`);
        const targetDir = path.join(path.dirname(filePath), '..', toPrefix);
        const newFilePath = path.join(targetDir, `${newName}.ts`);
        if (!fs.existsSync(targetDir)) {
            fs.mkdirSync(targetDir, { recursive: true });
            console.log(`✅ Created directory: ${targetDir}`);
        }
        if (fs.existsSync(newFilePath)) {
            console.log(`⚠️  Target file ${newName}.ts already exists in ${toPrefix} folder`);
            // If no suffix was provided and file exists, suggest using a suffix
            if (!suffix) {
                console.log(`💡 Suggestion: Use a suffix to avoid conflict:`);
                console.log(`   node scripts/move-icons.js "${filePath}" ${fromPrefix} ${toPrefix} Alt`);
            }
            return;
        }
        fs.writeFileSync(newFilePath, content, 'utf8');
        console.log(`✅ Created ${newName}.ts in ${toPrefix} folder`);
        fs.unlinkSync(filePath);
        console.log(`✅ Moved ${originalFileName}.ts → ${toPrefix}/${newName}.ts`);
    }
    catch (error) {
        console.error(`❌ Error processing ${filePath}:`, error.message);
    }
}
// Process a single file or directory
function processPath(targetPath, fromPrefix = 'cui', toPrefix = 'base', suffix = '', preserveOldName = false) {
    const fullPath = path.resolve(targetPath);
    if (!fs.existsSync(fullPath)) {
        console.log(`❌ Path does not exist: ${fullPath}`);
        return;
    }
    if (fs.statSync(fullPath).isDirectory()) {
        const files = fs.readdirSync(fullPath)
            .filter(file => file.endsWith('.ts'))
            .map(file => path.join(fullPath, file));
        console.log(`📁 Processing ${files.length} files in ${fullPath}`);
        console.log(`🔄 Converting ${fromPrefix} → ${toPrefix}${suffix ? ` (with suffix: ${suffix})` : ''}`);
        console.log(`🔒 Preserve oldName: ${preserveOldName ? 'YES' : 'NO'}`);
        files.forEach(filePath => moveIconFile(filePath, fromPrefix, toPrefix, suffix, preserveOldName));
    }
    else if (fullPath.endsWith('.ts')) {
        moveIconFile(fullPath, fromPrefix, toPrefix, suffix, preserveOldName);
    }
    else {
        console.log('❌ Please provide a .ts file or directory path');
    }
}
// Get command line arguments
const targetPath = process.argv[2] || 'packages/vui/src/icons/baseIcons/cui';
const fromPrefix = process.argv[3] || 'cui';
const toPrefix = process.argv[4] || 'base';
const suffix = process.argv[5] || '';
const preserveOldName = process.argv.includes('--preserve-oldname') || process.argv.includes('--keep-oldname');
// Show help if requested
if (process.argv.includes('--help') || process.argv.includes('-h')) {
    console.log(`
Usage: node scripts/move-icons.js <path> [fromPrefix] [toPrefix] [suffix] [flags]

Parameters:
  path         Path to file or directory (required)
  fromPrefix   Prefix to replace (default: 'cui')
  toPrefix     New prefix (default: 'base')
  suffix       Optional suffix for duplicate names (default: '')

Flags:
  --preserve-oldname   Don't update the oldName property (keep existing value)--keep-oldname       Alias for --preserve-oldname
  --help, -h           Show this help message

Examples:
  # Move cui → base (default, updates oldName)
  node scripts/move-icons.js packages/vui/src/icons/baseIcons/cui

  # Move with suffix and preserve existing oldName
  node scripts/move-icons.js packages/vui/src/icons/baseIcons/cui cui base Alt --preserve-oldname

  # Move cul → brand, keeping existing oldName values
  node scripts/move-icons.js packages/vui/src/icons/baseIcons/cul cul brand "" --keep-oldname

  # Move specific file without touching oldName
  node scripts/move-icons.js packages/vui/src/icons/baseIcons/cui/cuiCheckbox.ts cui base "" --preserve-oldname
`);
    process.exit(0);
}
console.log(`🚀 Starting icon migration...`);
console.log(`📂 Target: ${targetPath}`);
console.log(`🔄 Transform: ${fromPrefix} → ${toPrefix}${suffix ? ` (suffix: ${suffix})` : ''}`);
console.log(`🔒 Preserve oldName: ${preserveOldName ? 'YES' : 'NO'}`);
processPath(targetPath, fromPrefix, toPrefix, suffix, preserveOldName);
//# sourceMappingURL=move-icons.js.map