import os
import re
import shutil

SOURCE_DIR = "docs"
ENGLISH_DIR = "docs/en"

# Rensa tidigare engelska mappar
if os.path.exists(ENGLISH_DIR):
    shutil.rmtree(ENGLISH_DIR)
shutil.copytree(SOURCE_DIR, ENGLISH_DIR)

# --- Inline-hantering ---
def remove_other_language_inline(content, keep_lang):
    def replacer(match):
        lang, text = match.group(1), match.group(2)
        return text if lang == keep_lang else ""
    return re.sub(r"\{(sv|en):(.*?)}", replacer, content)

# --- Block-hantering ---
def remove_other_language_blocks(content, keep_lang):
    content = re.sub(
        r":(sv|en)\n(.*?)\n:::",
        lambda m: m.group(2) if m.group(1) == keep_lang else "",
        content,
        flags=re.DOTALL
    )
    return content

# --- Kombinerad rengöring ---
def clean_file(path, keep_lang):
    with open(path, "r", encoding="utf-8") as f:
        content = f.read()
    content = remove_other_language_blocks(content, keep_lang)
    content = remove_other_language_inline(content, keep_lang)
    with open(path, "w", encoding="utf-8") as f:
        f.write(content.strip() + "\n")

# --- Rensa svenska filer ---
for filename in os.listdir(SOURCE_DIR):
    if filename.endswith(".md"):
        clean_file(os.path.join(SOURCE_DIR, filename), keep_lang="sv")

# --- Rensa engelska filer ---
for filename in os.listdir(ENGLISH_DIR):
    if filename.endswith(".md"):
        clean_file(os.path.join(ENGLISH_DIR, filename), keep_lang="en")
