import fs from 'fs';
import path from 'path';

function walk(dir) {
    let results = [];
    const list = fs.readdirSync(dir);
    list.forEach(function (file) {
        file = path.join(dir, file);
        const stat = fs.statSync(file);
        if (stat && stat.isDirectory()) {
            results = results.concat(walk(file));
        } else {
            if (file.endsWith('.vue') || file.endsWith('.js')) {
                results.push(file);
            }
        }
    });
    return results;
}

const frontendSrcPath = path.join('C:', 'Bordales Projects', 'giftwrap - Copy', 'public', 'bdls-mts', 'frontend', 'src');
const files = walk(frontendSrcPath);

let changedFiles = 0;

for (const file of files) {
    let content = fs.readFileSync(file, 'utf8');
    let original = content;

    // Replace standard double quote variants
    content = content.replace(/"\/protected-image\/giftwrap-bg-pattern"/g, '"/protected-image/img.png"');
    // Replace standard single quote variants
    content = content.replace(/'\/protected-image\/giftwrap-bg-pattern'/g, "'/protected-image/img.png'");

    // Do NOT replace the hero pattern usages: protectedImage('giftwrap-bg-pattern')
    // The above replace won't touch protectedImage('giftwrap-bg-pattern') because it lacks the leading slash and path.

    if (content !== original) {
        fs.writeFileSync(file, content, 'utf8');
        changedFiles++;
        console.log(`Updated fallbacks in ${path.basename(file)}`);
    }
}

console.log(`Replaced image fallbacks in ${changedFiles} files!`);
