<?php
/**
 * Simple translation string extractor for Theme An Phat.
 */

$theme_dir = dirname(dirname(__FILE__));
$languages_dir = dirname(__FILE__);

// Find all PHP files
$directory = new RecursiveDirectoryIterator($theme_dir);
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);

$strings = array();

// Regex pattern to capture the textdomain functions:
// __, _e, esc_html__, esc_html_e, esc_attr__, esc_attr_e, esc_js_e, esc_js__
// with 'theme-anphat' text domain.
$pattern = '/\b(?:__|_e|esc_html__|esc_html_e|esc_attr__|esc_attr_e|esc_js_e|esc_js__)\(\s*(?:\'(.*?)(?<!\\\\)\'|"(.*?)(?<!\\\\)")\s*,\s*[\'"]theme-anphat[\'"]\s*\)/s';

foreach ($regex as $file_path => $object) {
    // Avoid reading the languages directory itself
    if (strpos($file_path, $languages_dir) !== false) {
        continue;
    }
    
    $content = file_get_contents($file_path);
    if (preg_match_all($pattern, $content, $matches)) {
        // Either match group 1 (single quotes) or 2 (double quotes) will be filled
        for ($i = 0; $i < count($matches[0]); $i++) {
            $str = $matches[1][$i] !== '' ? $matches[1][$i] : $matches[2][$i];
            // Normalize slashes
            $str = stripcslashes($str);
            if ($str !== '') {
                $strings[$str] = true;
            }
        }
    }
}

$keys = array_keys($strings);
sort($keys);

echo "Found " . count($keys) . " unique translatable strings.\n";

// Function to format po entries
function format_po_entry($msgid, $msgstr = '') {
    // Escape quotes and backslashes for PO format
    $escaped_id = addcslashes($msgid, "\"\n\r\t\\");
    $escaped_str = addcslashes($msgstr, "\"\n\r\t\\");
    return "msgid \"$escaped_id\"\nmsgstr \"$escaped_str\"\n\n";
}

// 1. Generate vi.po template
$vi_po = "# Giao diện Tiếng Việt cho Theme An Phát\n";
$vi_po .= "msgid \"\"\nmsgstr \"\"\n";
$vi_po .= "\"Project-Id-Version: Theme An Phat\\n\"\n";
$vi_po .= "\"Language: vi\\n\"\n";
$vi_po .= "\"Content-Type: text/plain; charset=UTF-8\\n\"\n";
$vi_po .= "\"Content-Transfer-Encoding: 8bit\\n\"\n\n";

foreach ($keys as $key) {
    // For Vietnamese, target matches source exactly
    $vi_po .= format_po_entry($key, $key);
}

file_put_contents("$languages_dir/vi.po", $vi_po);
echo "Written $languages_dir/vi.po\n";

// 2. Generate en_US.po template
// If there is an existing en_US.po, we merge/preserve translations, otherwise create blank.
$existing_translations = array();
if (file_exists("$languages_dir/en_US.po")) {
    $existing_content = file_get_contents("$languages_dir/en_US.po");
    preg_match_all('/msgid\s+"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"\s+msgstr\s+"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"/s', $existing_content, $existing_matches, PREG_SET_ORDER);
    foreach ($existing_matches as $m) {
        $existing_translations[stripcslashes($m[1])] = stripcslashes($m[2]);
    }
}

$en_po = "# English translation for Theme An Phat\n";
$en_po .= "msgid \"\"\nmsgstr \"\"\n";
$en_po .= "\"Project-Id-Version: Theme An Phat\\n\"\n";
$en_po .= "\"Language: en_US\\n\"\n";
$en_po .= "\"Content-Type: text/plain; charset=UTF-8\\n\"\n";
$en_po .= "\"Content-Transfer-Encoding: 8bit\\n\"\n\n";

foreach ($keys as $key) {
    $translation = isset($existing_translations[$key]) ? $existing_translations[$key] : '';
    $en_po .= format_po_entry($key, $translation);
}

file_put_contents("$languages_dir/en_US.po", $en_po);
echo "Written $languages_dir/en_US.po\n";
