# SPDX-FileCopyrightText: 2016-2026 PyThaiNLP Project
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: Apache-2.0
"""Convert values between ancient Thai currency units."""

from __future__ import annotations


def convert_currency(value: float, from_unit: str) -> dict[str, float]:
    """
    Convert a value in an ancient Thai currency unit to other units.

    Supported units:

    * เบี้ย (Bia)
    * อัฐ (At)
    * ไพ (Pi)
    * เฟื้อง (Feuang)
    * สลึง (Saleung)
    * บาท (Bath)
    * ตำลึง (Tamleung)
    * ชั่ง (Chang)

    See more:
    `Thai money <https://en.wikipedia.org/wiki/History_of_Thai_money>`_.

    :param float value: value in the unit given by ``from_unit``
    :param str from_unit: currency unit, one of 'เบี้ย', 'อัฐ', 'ไพ',
        'เฟื้อง', 'สลึง', 'บาท', 'ตำลึง', 'ชั่ง'
    :return: value in each unit, keyed by unit name
    :rtype: dict[str, float]
    :raises NotImplementedError: if ``from_unit`` is not supported

    :Example:

        >>> from pythainlp.ancient import convert_currency
        >>> print(convert_currency(8, "บาท"))
        {'เบี้ย': 51200.0, 'อัฐ': 512.0, 'ไพ': 256.0, 'เฟื้อง': 64.0, 'สลึง': 32.0, 'บาท': 8.0, 'ตำลึง': 2.0, 'ชั่ง': 0.1}

    """
    conversion_factors_to_att = {
        "เบี้ย": 1,
        "อัฐ": 100,  # 1 อัฐ = 100 เบี้ย
        "ไพ": 2 * 100,  # 1 ไพ = 2 อัฐ
        "เฟื้อง": 4 * 2 * 100,  # 1 เฟื้อง = 4 ไพ
        "สลึง": 2 * 4 * 2 * 100,  # 1 สลึง = 2 เฟื้อง
        "บาท": 4 * 2 * 4 * 2 * 100,  # 1 บาท = 4 สลึง
        "ตำลึง": 4 * 4 * 2 * 4 * 2 * 100,  # 1 ตำลึง = 4 บาท
        "ชั่ง": 20 * 4 * 4 * 2 * 4 * 2 * 100,  # 1 ชั่ง = 20 ตำลึง
    }

    if from_unit not in conversion_factors_to_att:
        raise NotImplementedError(
            f"Currency unit '{from_unit}' is not supported."
        )

    # start from 'อัฐ'
    value_in_att = value * conversion_factors_to_att[from_unit]

    # Calculate values in other units
    results: dict[str, float] = {}
    for unit, factor in conversion_factors_to_att.items():
        results[unit] = value_in_att / factor

    return results
