import os
import re

base_dir = os.path.dirname(os.path.abspath(__file__))
products_path = os.path.join(base_dir, 'preview', 'products-data.js')

with open(products_path, 'r', encoding='utf-8') as f:
    js_content = f.read()

# EME_CATEGORIES ends with `{ slug: 'accesorios', name: 'Accesorios', icon: '✨' },`
# Then the bad injected objects start { id: 9101...
# We just need to replace everything between that Accesorios line and the `];` that closes EME_CATEGORIES

pattern = r"(\{ slug: 'accesorios', name: 'Accesorios', icon: '✨' \},).*?\];"
replacement = r"\1\n];"

js_content = re.sub(pattern, replacement, js_content, flags=re.DOTALL)

with open(products_path, 'w', encoding='utf-8') as f:
    f.write(js_content)

print("FIXED EME_CATEGORIES")
