#!/usr/bin/env python3
import argparse
import json
import os
import subprocess
import sys
from typing import List

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))

# check for the venv
from tools.lib import sanity_check

sanity_check.check_venv(__file__)

from scripts.lib.zulip_tools import ENDC, FAIL, WARNING


def find_handlebars(translatable_strings: List[str]) -> List[str]:
    errored = []
    for string in translatable_strings:
        if "{{" in string:
            errored.append(string)
    return errored


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--no-generate", action="store_true", help="Don't run makemessages command."
    )
    args = parser.parse_args()

    if not args.no_generate:
        subprocess.call(["./manage.py", "makemessages", "--locale", "en"], stderr=subprocess.STDOUT)

    with open("locale/en/translations.json") as f:
        data = json.load(f)

    found = find_handlebars(list(data.keys()))
    if not found:
        sys.exit(0)

    print(WARNING + "Translation strings contain handlebars:" + ENDC)
    print("\n".join(found))

    print(
        WARNING
        + "See https://zulip.readthedocs.io/en/latest/translating/translating.html#frontend-translations "
        "on how you can insert variables in the frontend translatable "
        "strings." + ENDC
    )
    print(FAIL + "Failed!" + ENDC)
    sys.exit(1)
