#!/usr/bin/env python3
import json
import re

# Read the JSON file
with open('XXGPlayKit.bundle/xxpk_a_languages.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

# Read the temp.md file and build Chinese -> Turkish mapping
zh_to_tr = {}
with open('temp.md', 'r', encoding='utf-8') as f:
    lines = f.readlines()

for line in lines[1:]:  # Skip header
    line = line.strip()
    if not line:
        continue
    parts = line.split('\t')
    if len(parts) >= 3:
        zh_text = parts[1].strip()
        tr_text = parts[2].strip()
        zh_to_tr[zh_text] = tr_text

print(f"Found {len(zh_to_tr)} Turkish translations in temp.md")
print()

# For each key in the JSON, find the zh-Hans value and look up the Turkish translation
matched = 0
unmatched = []
for key, translations in data.items():
    zh_hans = translations.get('zh-Hans', '').strip()
    if zh_hans in zh_to_tr:
        translations['tr'] = zh_to_tr[zh_hans]
        matched += 1
    else:
        unmatched.append((key, zh_hans))

print(f"Matched: {matched}")
print(f"Unmatched: {len(unmatched)}")
for key, zh in unmatched:
    print(f"  {key}: '{zh}'")

# Write the updated JSON file
with open('XXGPlayKit.bundle/xxpk_a_languages.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=4)
    f.write('\n')

print("\nDone! Turkish translations added to xxpk_a_languages.json")
