const fs = require('fs');

// 1. Append CSS to style.css to hide spin buttons
const cssAppend = `

/* Hide number input spin buttons */
input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button { 
  -webkit-appearance: none; 
  margin: 0; 
}
input[type=number] {
  -moz-appearance: textfield;
}
`;

if (fs.existsSync('style.css')) {
    let css = fs.readFileSync('style.css', 'utf8');
    if (!css.includes('::-webkit-inner-spin-button')) {
        fs.appendFileSync('style.css', cssAppend, 'utf8');
        console.log('Appended CSS to style.css');
    }
} else if (fs.existsSync('index.html')) {
    // Fallback if style.css isn't the main one
    let html = fs.readFileSync('index.html', 'utf8');
    if (!html.includes('::-webkit-inner-spin-button')) {
        html = html.replace('</head>', `<style>${cssAppend}</style>\n</head>`);
        fs.writeFileSync('index.html', html, 'utf8');
        console.log('Appended CSS to index.html');
    }
}

// 2. Append JS to app_final.js to prevent mouse wheel scrolling on number inputs
const jsAppend = `

// Prevent scrolling on number inputs from changing the value
document.addEventListener("wheel", function(event) {
    if (document.activeElement.type === "number") {
        document.activeElement.blur();
    }
});
`;

if (fs.existsSync('app_final.js')) {
    let js = fs.readFileSync('app_final.js', 'utf8');
    if (!js.includes('document.addEventListener("wheel"')) {
        fs.appendFileSync('app_final.js', jsAppend, 'utf8');
        console.log('Appended wheel listener to app_final.js');
    }
}

// Cache buster
if (fs.existsSync('index.html')) {
    let html = fs.readFileSync('index.html', 'utf8');
    html = html.replace(/v=\d+/g, 'v=' + Date.now());
    fs.writeFileSync('index.html', html, 'utf8');
}
