const fs = require('fs');

let appFinalJs = fs.readFileSync('app_final.js', 'utf8');

// Fix duplicated discount logic
appFinalJs = appFinalJs.replace(
    /if \(line\.discountType === 'percent'\) \{ lineDiscountValue = lineTotalBeforeDiscount \* \(lineDiscountValue \/ 100\); \}\n\s*if \(line\.discountType === 'percent'\) \{ lineDiscountValue = lineTotalBeforeDiscount \* \(lineDiscountValue \/ 100\); \}/g,
    `if (line.discountType === 'percent') { lineDiscountValue = lineTotalBeforeDiscount * (lineDiscountValue / 100); }`
);

fs.writeFileSync('app_final.js', appFinalJs, 'utf8');

let indexHtml = fs.readFileSync('index.html', 'utf8');
indexHtml = indexHtml.replace(/<th>الخصم %<\/th>/g, '<th>الخصم</th>');
fs.writeFileSync('index.html', indexHtml, 'utf8');

console.log('Fixed double discount logic!');
