#! /usr/bin/env python3

# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: MIT

import json
import pprint
import subprocess

BASE = "modules/cldr-json/cldr-json/cldr-core/supplemental/plurals.json"

HEADER = '''# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: MIT

"""
Language data definitions.

This is an automatically generated file, see scripts/generate-language-data

Do not edit, please adjust language definitions in following repository:
https://github.com/WeblateOrg/language-data
"""
# pylint: disable=line-too-long,too-many-lines


'''

result = {}
decimals = {}

with open(BASE) as handle:
    data = json.load(handle)

for locale, rules in data["supplemental"]["plurals-type-cardinal"].items():
    result[locale] = [
        name.replace("pluralRule-count-", "")
        for name, rule in rules.items()
        if "@integer" in rule
    ]
    decimals[locale] = [name.replace("pluralRule-count-", "") for name in rules]


# Write plural tags
with open("weblate_language_data/plural_tags.py", "w") as output:
    output.write(HEADER)
    output.write(f"PLURAL_TAGS = {pprint.pformat(result)}\n\n")
    output.write(f"DECIMAL_PLURAL_TAGS = {pprint.pformat(decimals)}\n")

# Apply coding style
subprocess.run(
    ["pre-commit", "run", "--files", "weblate_language_data/plural_tags.py"],
    check=False,
)
