<?php
/**
 * Script to fix broken emoji encoding in app.js
 * Replaces mojibake characters with HTML entity codes
 */

$file = __DIR__ . '/assets/js/app.js';
$content = file_get_contents($file);

if ($content === false) {
    die("ERROR: Cannot read file: $file\n");
}

$original_length = strlen($content);

// Map of broken mojibake -> correct HTML entity replacement
$replacements = [
    // Broken trash can emoji (ðŸ—') -> HTML entity for 🗑
    "\xC3\xB0\xC5\xB8\xE2\x80\x97\xE2\x80\x99" => "&#x1F5D1;",
    
    // Broken mosque emoji (ðŸ•Œ) -> HTML entity for 🏢
    "\xC3\xB0\xC5\xB8\xE2\x80\xA2\xC5\x92" => "&#x1F3E2;",
    
    // Broken person emoji (ðŸ'¤) -> HTML entity for 👤
    "\xC3\xB0\xC5\xB8\xE2\x80\x98\xC2\xA4" => "&#x1F464;",
    
    // Broken satellite emoji (ðŸ"¡) -> HTML entity for 📡
    "\xC3\xB0\xC5\xB8\xE2\x80\x9C\xC2\xA1" => "&#x1F4E1;",
    
    // Broken pencil (âœ) -> HTML entity for ✏️
    "\xC3\xA2\xC5\x93" => "&#x270F;&#xFE0F;",
    
    // Broken square emoji (â¬) -> HTML entity for 📐
    "\xC3\xA2\xC2\xAC" => "&#x1F4D0;",
    
    // Broken m² (mÂ²) -> correct m²
    "m\xC3\x82\xC2\xB2" => "m&sup2;",
    
    // Broken info icon (ⓘ) 
    "\xE2\x93\x98" => "&#x2139;",
    
    // Broken warning icon (âš )
    "\xC3\xA2\xC5\xA1\xC2\xA0" => "&#x26A0;",
    
    // Broken bullet (â€¢)
    "\xC3\xA2\xE2\x80\x9A\xC2\xAC" => "&#x2022;",
];

$count = 0;
foreach ($replacements as $broken => $fixed) {
    $occurrences = substr_count($content, $broken);
    if ($occurrences > 0) {
        $content = str_replace($broken, $fixed, $content);
        echo "Fixed: '$fixed' ($occurrences occurrences)\n";
        $count += $occurrences;
    }
}

if ($count === 0) {
    echo "No broken emoji patterns found. Trying alternative byte patterns...\n";
    
    // Try reading the file as raw bytes and searching for patterns
    $hex = bin2hex($content);
    
    // Let's search for common patterns around "Hapus" text
    $hapus_pos = strpos($content, 'Hapus');
    if ($hapus_pos !== false) {
        // Look at the bytes right before "Hapus" to see what the broken emoji looks like
        $before = substr($content, max(0, $hapus_pos - 20), 20);
        echo "Bytes before first 'Hapus': " . bin2hex($before) . "\n";
        echo "Chars before first 'Hapus': " . $before . "\n";
    }
    
    // Search for "Edit" button text
    $edit_pos = strpos($content, 'Edit</button>');
    if ($edit_pos !== false) {
        $before = substr($content, max(0, $edit_pos - 10), 10);
        echo "Bytes before 'Edit': " . bin2hex($before) . "\n";
    }
    
    // Search for mosque pattern near "item.nama"
    $fp_title = strpos($content, '${item.nama}');
    if ($fp_title !== false) {
        $before = substr($content, max(0, $fp_title - 15), 15);
        echo "Bytes before '\${item.nama}': " . bin2hex($before) . "\n";
    }
}

if ($count > 0) {
    // Backup
    copy($file, $file . '.bak');
    
    // Write fixed content
    file_put_contents($file, $content);
    echo "\nTotal: $count emoji fixes applied.\n";
    echo "Backup saved as: app.js.bak\n";
} else {
    echo "\nNo fixes applied.\n";
}
