const fs = require('fs');
const path = require('path');

// Function to replace strings in a file
function replaceStringsInFile(filePath) {
    try {
        let content = fs.readFileSync(filePath, 'utf8');
        
        // Replace the strings
        content = content.replace(/greenshiftquery/g, 'gl-page-builder');
        content = content.replace(/greenshift-animation-and-page-builder-blocks/g, 'gl-page-builder');
        
        fs.writeFileSync(filePath, content, 'utf8');
        console.log(`✅ Updated: ${filePath}`);
    } catch (error) {
        console.error(`❌ Error updating ${filePath}:`, error.message);
    }
}

// Function to recursively process files in a directory
function processDirectory(dirPath) {
    try {
        const files = fs.readdirSync(dirPath);
        
        files.forEach(file => {
            const filePath = path.join(dirPath, file);
            const stat = fs.statSync(filePath);
            
            if (stat.isDirectory()) {
                processDirectory(filePath);
            } else if (file.endsWith('.js') || file.endsWith('.css')) {
                replaceStringsInFile(filePath);
            }
        });
    } catch (error) {
        console.error(`❌ Error processing directory ${dirPath}:`, error.message);
    }
}

// Main execution
console.log('�� Starting post-build string replacement...');

const buildDir = path.join(__dirname, '..', 'build');
if (fs.existsSync(buildDir)) {
    processDirectory(buildDir);
    console.log('✅ Post-build string replacement completed!');
} else {
    console.error('❌ Build directory not found:', buildDir);
    process.exit(1);
} 