<?php
$file = 'app.js';
$content = file_get_contents($file);
$orig = $content;

// Each array has [search, replace]
// All string keys are raw UTF-8 as they appear in the file
$fixes = [];

// Product card sold count & add to cart
$fixes[] = [
    "\xE0\xB8\x92\xE0\xB8\x82\xE0\xB9\x80\xE0\xB8\x99\xE0\xB8\x85\xE0\xB9\x80\xE0\xB8\x99\xE0\xB8\x87 \${p.sold} \xE0\xB8\x94\xE0\xB8\x99",
    "ขายแล้ว \${p.sold} ชิ้น"
];
$fixes[] = [
    "\xE0\xB8\x92\xE0\xB8\x82\xE0\xB9\x80\xE0\xB8\x99\xE0\xB8\x85\xE0\xB9\x80\xE0\xB8\x99\xE0\xB8\x87 \${product.sold} \xE0\xB8\x94\xE0\xB8\x99",
    "ขายแล้ว \${product.sold} ชิ้น"
];
// | X รีวิว |
$fixes[] = [
    "| \${product.reviewsCount} \xE0\xB8\xA3\xE0\xB8\xB5\xE0\xB8\xA7\xE0\xB8\x94\xE0\xB8\xA7 |",
    "| \${product.reviewsCount} รีวิว |"
];

// Write each fix
$count = 0;
foreach ($fixes as [$search, $replace]) {
    if (strpos($content, $search) !== false) {
        $content = str_replace($search, $replace, $content);
        $count++;
    }
}

echo "Simple hex fixes: $count\n";

// Now do easy str_replace with correct UTF-8 strings for things that were
// already partially fixed or have simple patterns
// Product card
$content = str_replace(
    '+ ' . "\xE0\xB9\x80\xE0\xB8\x99\xE0\xB8\x94\xE0\xB8\x80\xE0\xB8\x99" . "\x95\xE0\xB8\x82\xE0\xB8\x82\xE0\xB8\x82",
    '+ เพิ่มในตะกร้า',
    $content
);

file_put_contents($file, $content);
echo "Done. Changed " . ($orig === $content ? 0 : 1) . " sections.\n";
